Rate Limiter empowers you to control the rate of incoming requests in your Go applications, safeguarding against excessive traffic and potential overload. It offers a flexible configuration to tailor its behavior to your specific needs.
Customizable threshold
: Define the maximum number of requests permitted within a specified duration.Customizable duration
: Set the time window within which the threshold is applied.Reset
: Clear the rate limiter's internal state, enabling a fresh start.
To integrate Rate Limiter into your project, execute the following command in your terminal:
go get github.com/bitsexplained/ratelimiter
- Here's a practical example showcasing Rate Limiter's usage:
package main
import (
"fmt"
"github.com/bitsexplained/ratelimiter"
"time"
)
func main() {
// Create a new rate limiter with a threshold of 500 requests per second
rl := ratelimiter.NewRateLimiter(
ratelimiter.WithThreshold(500),
ratelimiter.WithDuration(time.Second),
)
// Allow a request
if rl.Allow() {
// Process the request
fmt.Println("Request allowed")
} else {
// Reject the request
fmt.Println("Request rejected")
}
}
Rate Limiter exposes two essential methods for interaction:
-
go Allow() bool
: Determines whether a request is permissible based on the configured threshold and duration. -
Reset()
: Clears the rate limiter's internal state, effectively starting anew.
-
Customize Rate Limiter's behavior using these configuration options:
-
WithThreshold(threshold int)
: Sets the maximum allowable requests within the specified duration. -
WithDuration(duration time.Duration)
: Establishes the time window for applying the threshold.
Construct a Rate Limiter instance using the NewRateLimiter
function, seamlessly chaining configuration options:
rl := ratelimiter.NewRateLimiter(
ratelimiter.WithThreshold(500),
ratelimiter.WithDuration(time.Second),
)
To clear the rate limiter's internal state, employ the Reset() method:
rl.Reset()
Your contributions to Rate Limiter are warmly welcomed! If you encounter bugs, have feature requests, or wish to contribute code, please create an issue or submit a pull request to the GitHub repository.
rate limiter library is released under the MIT License. See LICENSE for details.