Skip to content

r8k/ratelimit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages