Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/etcinit/speedbump
A Redis-backed rate limiter in Go
https://github.com/etcinit/speedbump
gin go middleware negroni rate-limiting redis throttle
Last synced: 3 months ago
JSON representation
A Redis-backed rate limiter in Go
- Host: GitHub
- URL: https://github.com/etcinit/speedbump
- Owner: etcinit
- License: mit
- Created: 2015-04-18T22:47:36.000Z (almost 10 years ago)
- Default Branch: v2
- Last Pushed: 2022-03-28T19:17:09.000Z (almost 3 years ago)
- Last Synced: 2024-10-31T11:51:32.678Z (3 months ago)
- Topics: gin, go, middleware, negroni, rate-limiting, redis, throttle
- Language: Go
- Size: 24.4 KB
- Stars: 116
- Watchers: 7
- Forks: 10
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# [speedbump](https://github.com/etcinit/speedbump) [![GoDoc](https://godoc.org/github.com/etcinit/speedbump?status.svg)](http://godoc.org/github.com/etcinit/speedbump)
A Redis-backed Rate Limiter for Go
[![wercker status](https://app.wercker.com/status/9832225d9e89d9702d4ce7ca4e8e4285/m/master "wercker status")](https://app.wercker.com/project/bykey/9832225d9e89d9702d4ce7ca4e8e4285)
## Cool stuff
- Backed by Redis, so it keeps track of requests across a cluster
- Extensible timing functions. Includes defaults for tracking requests per
second, minute, and hour
- Works with IPv4, IPv6, or any other unique identifier
- Example middleware included for [Gin](https://github.com/gin-gonic/gin) (See: [ginbump](https://github.com/etcinit/speedbump/blob/master/ginbump)) and
[Negroni](https://github.com/codegangsta/negroni) (See:
[negronibump](https://github.com/etcinit/speedbump/blob/master/negronibump))## Versions
|Branch|Go Get Command|Client Version|-|
|---|---|---|---|
|**v2**|`go get gopkg.in/etcinit/speedbump.v2`|`gopkg.in/redis.v5`|[Link](https://gopkg.in/etcinit/speedbump.v2)|
|**v1**, **master**|`go get gopkg.in/etcinit/speedbump.v1`|`gopkg.in/redis.v3`|[Link](https://gopkg.in/etcinit/speedbump.v1)|
|**v0**|`go get gopkg.in/etcinit/speedbump.v0`|`gopkg.in/redis.v2`|[Link](https://gopkg.in/etcinit/speedbump.v0)|## Usage
- Get a working Redis server
- Go get:```sh
$ go get github.com/etcinit/speedbump
```- Include it in your code
```go
package mainimport (
"fmt"
"time""github.com/etcinit/speedbump"
"gopkg.in/redis.v5"
)func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
hasher := speedbump.PerSecondHasher{}// Here we create a limiter that will only allow 5 requests per second
limiter := speedbump.NewLimiter(client, hasher, 5)for {
// This example has a hardcoded IP, but you would replace it with the IP
// of a client on a real case.
success, err := limiter.Attempt("127.0.0.1")if err != nil {
panic(err)
}if success {
fmt.Println("Successful!")
} else {
fmt.Println("Limited! :(")
}time.Sleep(time.Millisecond * time.Duration(100))
}
}
```- Output:
```
Successful!
Successful!
Successful!
Successful!
Successful!
Successful!
Limited! :(
Limited! :(
Limited! :(
Limited! :(
Limited! :(
Successful!
Successful!
Successful!
Successful!
Successful!
Successful!
Limited! :(
Limited! :(
Limited! :(
Limited! :(
Successful!
Successful!
...
```