r8k / ratelimit

Rate Limiter for Go, backed by Redis.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ratelimit

GoDoc

Rate Limiter for Go, backed by Redis.

View the docs.

Dependencies

Features

  • Utilises connection pool from Redigo
  • Handles race conditions
  • Efficient
  • Distributed Store: coming soon

Installation

$ go get github.com/r8k/ratelimit

Example

package main

import "fmt"
import "net"
import "github.com/r8k/ratelimit"

func main() {
    store, err := ratelimit.Init(&net.TCPAddr{Port: 6379})
    if err != nil {
        panic(err)
    }
    defer store.Close()

    // Get `RateLimit` for `identifier` client_ip
    Limit, err := store.Get("client_ip")
    if err != nil {
        panic(err)
    }

    fmt.Printf("Quota: %d\n", Limit.Quota)
    fmt.Printf("Used: %d\n", Limit.Used)
    fmt.Printf("Remaining: %d\n", Limit.Remaining)
    fmt.Printf("Retry After: %s\n", Limit.RetryAfter)
}

Run the above example

❯ go run main.go

Quota: 5000
Used: 1
Remaining: 4999
Retry After: 2014-08-09 17:14:55 +0530 IST
```

## Benchmark

❯ go test -bench=.

PASS BenchmarkGetSequential 10000 190953 ns/op BenchmarkGetParallel 100 19507315 ns/op ok github.com/r8k/ratelimit 3.954s


## License

MIT

About

Rate Limiter for Go, backed by Redis.

License:MIT License


Languages

Language:Go 100.0%