Skip to content

SpeedBump is a Go library providing efficient and scalable rate limiting using Cloudflare's sliding window counter algorithm, with support for both standalone and Redis-backed distributed environments.

Notifications You must be signed in to change notification settings

prophet7821/speedBump

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SpeedBump

SpeedBump is a powerful and flexible rate limiting library for Go, designed to protect your web applications and APIs from overuse and to ensure equitable resource access across all users. At its core, SpeedBump utilizes Cloudflare's sliding window counter algorithm, offering a sophisticated approach to rate limiting that balances fairness and efficiency. This method ensures that request limits are enforced smoothly over time, preventing bursts of traffic from unfairly consuming resources.

Installation

go get github.com/prophet7821/speedBump.git

Usage

SpeedBump provides a suite of functionalities to easily integrate rate limiting into your Go applications. Below is a table summarizing the available functions:

Function Description Example
Limit Applies rate limits with customizable options.
 router.Use(
speedBump.Limit(
100,
1*time.Minute))
LimitAll Applies a global rate limit across all requests.
router.Use(
speedBump.LimitAll(
100,
1*time.Minute))
LimitByIP Limits requests based on the client's IP address.
router.Use(
speedBump.LimitByIP(
100,
1*time.Minute))
WithKeyFuncs Allows setting custom key functions for rate limiting.
router.Use(
speedBump.Limit(
100,
1*time.Minute,
speedBump.WithKeyFuncs(speedBump.KeyByIP, func (r *http.Request) (string, error) {
return r.Header.Get("X-Custom-Header"), nil
}),))
KeyByIP/EndPoint Provides built-in key functions for identifying requests.
router.Use(
speedBump.Limit(
100,
1*time.Minute,
speedBump.WithKeyFuncs(speedBump.KeyByIP, speedBump.KeyByEndPoint)
))
WithLimitHandler Customizes the response for rate-limited requests.
router.Use(
speedBump.Limit(
100,
1*time.Minute,
speedBump.WithLimitHandler(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte("Custom limit exceeded message"))
})))
WithLimitCounter Enables the use of a custom limit counter. See below for integrating with Redis
WithRedisLimitCounter Integrates Redis for distributed rate limiting.
router.Use(
speedBump.WithRedisLimitCounter(
&speedBump.Config{
Host: "localhost",
Port: 6379,Password: "",
// Optional: Your Redis password, if any.DBIndex: 0,
// Optional: The Redis database index.
}))

Contributing

We welcome contributions to the SpeedBump project! Whether it's adding new features, fixing bugs, improving documentation, or sharing feedback, your collaboration is highly appreciated. Please feel free to submit pull requests, report issues, or suggest improvements. Let's work together to make SpeedBump even better for the community.

About

SpeedBump is a Go library providing efficient and scalable rate limiting using Cloudflare's sliding window counter algorithm, with support for both standalone and Redis-backed distributed environments.

Topics

Resources

Stars

Watchers

Forks

Languages