Documentation ¶
Overview ¶
Package leaky provides a simple implementation of the leaky bucket algorithm It can be used as a middleware or called manually depending on requirements
At the moment, the leaky-bucket and handler middleware are tightly coupled, this should be cleaned up to allow usage in scenarios where the handler middleware is not required, or not desired.
At the moment, the middleware and the leaky-bucket are tightly coupled to Redis as a cache, although a Redis failure is non-fatal (fail-open), the dependency might not be required or desired, and should be decoupled appropriately.
Package leaky implements a leaky bucket rate limiting Middleware
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
Bucket is the instance of a leaky bucket
type Handler ¶
type Handler func(w http.ResponseWriter, r *http.Request)
Handler creates a new rate limiting leaky bucket handler
type ThrottleManager ¶
type ThrottleManager struct {
// contains filtered or unexported fields
}
ThrottleManager manages leaky buckets
func NewThrottleManager ¶
func NewThrottleManager(redis *redis.Client) *ThrottleManager
NewThrottleManager creates a new instance of bucket manager it requires a Redis client for storing state
func (*ThrottleManager) ThrottlingHandler ¶
func (m *ThrottleManager) ThrottlingHandler(handler Handler, size int, rate int, keyFunc KeyFunc, bucketName string) *Bucket
ThrottlingHandler creates a new handler wrapper for use as an HTTP middleware
Directories ¶
Path | Synopsis |
---|---|
Example of using the throttling rate limiter Start a Redis instance in Docker like this: docker run -itd --name redis -p 6379:6379 redis:alpine Run this example with: go run throttling_api.go Make some requests and watch them throttle: curl http://localhost:7777/api
|
Example of using the throttling rate limiter Start a Redis instance in Docker like this: docker run -itd --name redis -p 6379:6379 redis:alpine Run this example with: go run throttling_api.go Make some requests and watch them throttle: curl http://localhost:7777/api |