https://github.com/twiny/ratelimit
A Golang blocking leaky-bucket rate limiter
https://github.com/twiny/ratelimit
Last synced: 12 months ago
JSON representation
A Golang blocking leaky-bucket rate limiter
- Host: GitHub
- URL: https://github.com/twiny/ratelimit
- Owner: twiny
- License: mit
- Created: 2022-05-09T15:28:25.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-09T16:34:37.000Z (about 4 years ago)
- Last Synced: 2025-04-05T02:33:30.461Z (over 1 year ago)
- Language: Go
- Size: 1.95 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Leaky bucket rate limiter
A Go implementation of the leaky-bucket rate limit algorithm.
## Install
`go get github.com/twiny/ratelimit`
## API
```go
Take() time.Time
Rate() int
Duration() time.Duration
String() string
```
## Example
```go
package main
import (
"fmt"
"time"
"github.com/twiny/ratelimit"
)
// main
func main() {
limiter := ratelimit.NewLimiter(10, 1*time.Second) // 10 request per second
prev := time.Now()
for i := 0; i < 10; i++ {
now := limiter.Take()
fmt.Println(i, now.Sub(prev))
prev = now
}
}
// output:
// 0 239ns
// 1 100ms
// 2 100ms
// 3 100ms
// 4 100ms
// 5 100ms
// 6 100ms
// 7 100ms
// 8 100ms
// 9 100ms
```