https://github.com/rcdmk/go-ratelimiter
Go Rate Limiter is a Go package that provides rate limiting functionality with middleware implementations for standard lib and common frameworks.
https://github.com/rcdmk/go-ratelimiter
go-lib go-stdlib http-middleware rate-limit
Last synced: 3 months ago
JSON representation
Go Rate Limiter is a Go package that provides rate limiting functionality with middleware implementations for standard lib and common frameworks.
- Host: GitHub
- URL: https://github.com/rcdmk/go-ratelimiter
- Owner: rcdmk
- License: mit
- Created: 2024-05-23T05:50:08.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-03T00:30:50.000Z (5 months ago)
- Last Synced: 2025-01-03T01:28:30.987Z (5 months ago)
- Topics: go-lib, go-stdlib, http-middleware, rate-limit
- Language: Go
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Rate Limiter
Go Rate Limiter is a Go package that provides rate limiting functionality with middleware implementations for standard lib and common frameworks.
## Features
- Simple and easy-to-use rate limiter for Go applications.
- Middleware implementations for standard lib and popular frameworks.
- Configurable rate limiting options.
- In-memory caching for rate-limiter can be replaced by your own implementation.## Installation
Use `go get` to install the package:
```sh
go get github.com/rcdmk/go-ratelimiter
```## Usage
Import the package and use the `New` method to create a new rate limiter instance for use:
```go
import (
// ...
"github.com/rcdmk/go-ratelimiter"
// ...
)// ...
rateLimiter := ratelimiter.New(ratelimiter.Options{
MaxRatePerSecond: 15,
MaxBurst: 15,
})if !rateLimiter.Allow("my-operation-name") {
// over rate limit, deny action and stop execution
return
}// proceed normaly
// ...
```### Middleware
**`StdLib`** is a standard lib compatible middleware implementation for limitting requests served through an HTTP server.
It is also compatible with all frameworks that can use standard lib middleware (eg. [chi](https://github.com/go-chi/chi), [Gorilla](https://github.com/gorilla/mux), etc.).
```go
import (
// ...
"github.com/rcdmk/go-ratelimiter/ratelimitermiddleware"
// ...
)// ...
options := ratelimitermiddleware.Options{
MaxRatePerSecond: 15,
MaxBurst: 10,
SourceHeaderKey: "Authorization",
}// wrap your handler with the middleware
rateLimitedHandler := ratelimitermiddleware.StdLib(handler, options)http.Handle("/my-resource", rateLimitedHandler)
// ...
```