Efficient token bucket rate limiter for your proxies.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
_examples
.editorconfig
.gitignore
.travis.yml
History.md
LICENSE
README.md
ratelimit.go
ratelimit_test.go
version.go

README.md

ratelimit Build Status GoDoc Coverage Status Go Report Card

Simple, efficient token bucket rate limiter for your proxies.

Supports filters and whitelist exceptions to determine when to apply the rate limiter.

Installation

go get -u gopkg.in/vinxi/ratelimit.v0

API

See godoc reference.

Example

Rate limit based on time window

package main

import (
  "fmt"
  "time"
  "gopkg.in/vinxi/ratelimit.v0"
  "gopkg.in/vinxi/vinxi.v0"
)

const port = 3100

func main() {
  // Create a new vinxi proxy
  vs := vinxi.NewServer(vinxi.ServerOptions{Port: port})
  
  // Attach the rate limit middleware for 100 req/min
  vs.Use(ratelimit.NewTimeLimiter(time.Minute, 100))
  
  // Target server to forward
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", port)
  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

Limit requests per second

package main

import (
  "fmt"
  "gopkg.in/vinxi/ratelimit.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "time"
)

const port = 3100

func main() {
  // Create a new vinxi proxy
  vs := vinxi.NewServer(vinxi.ServerOptions{Port: port})

  // Attach the ratelimit middleware for 10 req/second
  vs.Use(ratelimit.NewRateLimiter(10, 10))

  // Target server to forward
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", port)
  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

License

MIT