Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shareed2k/go_limiter
Rate limiting with few algorithms
https://github.com/shareed2k/go_limiter
gcra go-redis golang http leaky-bucket middleware rate-limiting redis sliding-window
Last synced: 8 days ago
JSON representation
Rate limiting with few algorithms
- Host: GitHub
- URL: https://github.com/shareed2k/go_limiter
- Owner: Shareed2k
- License: bsd-2-clause
- Created: 2020-03-23T08:46:22.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-02-29T13:10:48.000Z (9 months ago)
- Last Synced: 2024-10-26T03:32:08.191Z (20 days ago)
- Topics: gcra, go-redis, golang, http, leaky-bucket, middleware, rate-limiting, redis, sliding-window
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 20
- Watchers: 2
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rate limiting with few algorithms (Sliding Window, Leaky Bucket)
[![Build Status](https://travis-ci.org/Shareed2k/go_limiter.svg?branch=master)](github.com/shareed2k/go_limiter)
This package is based on [go-redis/redis_rate](https://github.com/go-redis/redis_rate) and implements GCRA (aka leaky bucket) for rate limiting based on Redis. The code requires Redis version 3.2 or newer since it relies on replicate_commands feature.
## Installation
go_limiter requires a Go version with [Modules](https://github.com/golang/go/wiki/Modules) support and uses import versioning. So please make sure to initialize a Go module before installing go_limiter:
```shell
go get github.com/shareed2k/go_limiter
```Import:
```go
import "github.com/shareed2k/go_limiter"
```## Examplle
```go
import (
"log"
"time""github.com/redis/go-redis/v9"
"github.com/shareed2k/go_limiter"
)func main() {
option, err := redis.ParseURL("redis://127.0.0.1:6379/0")
if err != nil {
log.Fatal(err)
}
client := redis.NewClient(option)
_ = client.FlushDB().Err()limiter := go_limiter.NewLimiter(client)
res, err := limiter.Allow("api_gateway_cache:klu4ik", &go_limiter.Limit{
// or you can use go_limiter.SlidingWindowAlgorithm
Algorithm: go_limiter.GCRAAlgorithm,
Rate: 10,
Period: 2 * time.Minute,
Burst: 10,
})if err != nil {
log.Fatal(err)
}log.Println("===> ", res.Allowed, res.Remaining)
// Output: true 1
}
```