Skip to content

Latest commit

 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Rate Limiter

Token bucket rate limiter for Go with per-client (IP/UserID) support.

Installation

go get github.com/Prasanth-S7/rate-limiter@v1.0.0

Example

package main

import (
	"net/http"
	"time"

	ratelimiter "github.com/Prasanth-S7/rate-limiter"
	"github.com/Prasanth-S7/rate-limiter/middleware"
)

func main() {
	manager := ratelimiter.NewManager(10, 2, 10*time.Minute)
	limiter := middleware.New(manager, false)

	// Use the limiter in your application, for example, as an HTTP middleware
	http.Handle("/api", limiter.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello, World!"))
	})))

	http.ListenAndServe(":8080", nil)
}

Manager Configuration

NewManager(capacity, fillRate, ttl) creates a rate limiter manager with three parameters:

Parameter Type Description
capacity int Maximum tokens a client can accumulate. This is the burst capacity - how many requests a client can make in a short spike before being limited.
fillRate float64 Tokens added per second. The steady-state rate limit. A value of 2 means 2 tokens (requests) are added every second.
ttl time.Duration Time-to-live for inactive client buckets. If a client hasn't made a request in this duration, their bucket is deleted to free memory.

About

a simple token bucket rate-limiter in go

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages