Skip to content

Simple thread-safe go ratelimiter library with redis backend

License

Notifications You must be signed in to change notification settings

ofw/go-redis-ratelimiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-redis-ratelimiter

Simple go ratelimiter library with redis backend

Docs

https://godoc.org/github.com/oeegor/go-redis-ratelimiter

Usage

import (
	"github.com/garyburd/redigo/redis"
    "github.com/oeegor/go-redis-ratelimiter"
)

func main() {

    // initialize redis pool
    redisPool := redis.NewPool(func() (redis.Conn, error) {
		c, err := redis.Dial("tcp", "your-redis-address")
		if err != nil {
			return nil, err
		}
		return c, err
	}, 100)  // also set max connections to 100

    // increment rate limit usage for given key that is allowed 10 requests per second with one try
    // Ratelimiter will try to acquire limit N tries using time.Sleep between tries
    limitCtx, err := ratelimiter.Incr(redisPool, "mykey", 10, time.Second, 1)

    if err != nil {
        // do something
    }

    // limitCtx contains all necessary data for ratelimiter state
    if limitCtx.Reached() {
        // code to handle over the limit logic
    }
}

About

Simple thread-safe go ratelimiter library with redis backend

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages