wassimbj / gorl

redis-based rate limiter written in go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go rate limiter

Features

  • Simple
  • Fast
  • Distributed
  • Uses Token bucket algorithm
  • Redis Based
  • Flexible
  • Can handle concurrent requests

Install

go get github.com/wassimbj/gorl

Usage

import "github.com/wassimbj/gorl"


type Result struct {
	AttemptsLeft int
	Used         int           // used attempts
	TimeLeft     int           // in ms, time left until the attempts gets renewed
	Block        bool          // should the user get blocked
}

// in this example, the user has the right to make only 50 requests in 5 hours.
result, err := gorl.RateLimiter(context.Background(), gorl.RLOpts{
   Attempts: 50, // requests limit
   Prefix:   "login",
   Duration: time.Hour * 5, // limit duration
   Id:       "USER_IP",
   BlockDuration: time.Hour,
   RedisClient: redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	}),
})

Note: the bucket(redis) is refilled with limit each time the duration ends, im not saving the last refill timestamp here maybe in near future.

Full example

Go to example.md

TODO

✅ Add block duration

About

redis-based rate limiter written in go


Languages

Language:Go 99.1%Language:Makefile 0.9%