GithubHelp home page GithubHelp logo

prophet7821 / speedbump Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 22 KB

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.

Go 100.00%
go golang microservices rate-limiter redis

speedbump's Introduction

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.

speedbump's People

Contributors

prophet7821 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.