Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bsm/ratelimit

Simple, thread-safe Go rate-limiter
https://github.com/bsm/ratelimit

Last synced: 3 days ago
JSON representation

Simple, thread-safe Go rate-limiter

Awesome Lists containing this project

README

        

# RateLimit

[![GoDoc](https://pkg.go.dev/badge/github.com/bsm/ratelimit)](https://pkg.go.dev/github.com/bsm/ratelimit)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

Simple, thread-safe Go rate-limiter.
Inspired by Antti Huima's algorithm on http://stackoverflow.com/a/668327

### Example

```go
package main

import (
"github.com/bsm/ratelimit/v3"
"log"
)

func main() {
// Create a new rate-limiter, allowing up-to 10 calls
// per second
rl := ratelimit.New(10, time.Second)

for i:=0; i<20; i++ {
if rl.Limit() {
fmt.Println("Doh! Over limit!")
} else {
fmt.Println("OK")
}
}
}
```

### Documentation

Full documentation is available on [pkg.go.dev](https://pkg.go.dev/github.com/bsm/ratelimit).