A simple, efficient rate limiting library written in Go, designed to control the rate of requests an entity can make within a certain time frame. It uses a token bucket algorithm to allow or deny requests based on the available tokens, providing a straightforward way to implement rate limiting in Go applications.
- Easy to integrate and use in any Go application.
- Configurable rate limiting options to suit different use cases.
- Supports custom time intervals for more flexible rate control.
- Thread-safe implementation using Go's concurrency primitives.
To start using ratelimiter, install Go and run go get:
go get github.com/SashaBokov/ratelimiterThe ratelimiter library can be applied in various scenarios to ensure that the rate of actions does not exceed the defined limits. Here are some practical examples:
To limit a user from sending more than 5 messages per second:
rl := ratelimiter.New(5, 5, time.Second)
// Simulating message sending
for i := 0; i < 5; i++ {
if rl.IsAllow() {
// Message allowed
}
}
// The next message within the same second will be blocked
if !rl.IsAllow() {
// Message blocked
}To prevent an IP address from making more than 10,000 requests per minute:
rl := ratelimiter.New(10000, 10000, time.Minute)
// Simulating requests
for i := 0; i < 10000; i++ {
if rl.IsAllow() {
// Request allowed
}
}
// The next request within the same minute will be blocked
if !rl.IsAllow() {
// Request blocked
}To allow a user to have no more than 3 failed card transactions per day:
rl := ratelimiter.New(3, 3, 24*time.Hour)
// Simulating transactions
for i := 0; i < 3; i++ {
if rl.IsAllow() {
// Transaction allowed
}
}
// The next transaction within the same day will be blocked
if !rl.IsAllow() {
// Transaction blocked
}