A Redis-backed rate limiter in Go
Switch branches/tags
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
ginbump Use redis.v5 client Nov 2, 2016
negronibump Use redis.v5 client Nov 2, 2016
.gitignore Initial commit Apr 18, 2015
CHANGELOG.md Add a CHANGELOG Nov 7, 2015
LICENSE Initial commit Apr 18, 2015
README.md docs(README): Add versions table Dec 27, 2016
hashers.go Improve documentation Apr 22, 2015
hashers_test.go Improve documentation Apr 22, 2015
speedbump.go style(speedbump): Clean up and simplify somethings Dec 26, 2016
speedbump_test.go Use redis.v5 client Nov 2, 2016
wercker.yml Add wercker setup Apr 18, 2015

README.md

speedbump GoDoc

A Redis-backed Rate Limiter for Go

wercker status

Cool stuff

  • Backed by Redis, so it keeps track of requests across a cluster
  • Extensible timing functions. Includes defaults for tracking requests per second, minute, and hour
  • Works with IPv4, IPv6, or any other unique identifier
  • Example middleware included for Gin (See: ginbump) and Negroni (See: negronibump)

Versions

Branch Go Get Command Client Version -
v2 go get gopkg.in/etcinit/speedbump.v2 gopkg.in/redis.v5 Link
v1, master go get gopkg.in/etcinit/speedbump.v1 gopkg.in/redis.v3 Link
v0 go get gopkg.in/etcinit/speedbump.v0 gopkg.in/redis.v2 Link

Usage

  • Get a working Redis server
  • Go get:
$ go get github.com/etcinit/speedbump
  • Include it in your code
package main

import (
	"fmt"
	"time"

	"github.com/etcinit/speedbump"
	"gopkg.in/redis.v5"
)

func main() {
	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})
	hasher := speedbump.PerSecondHasher{}

	// Here we create a limiter that will only allow 5 requests per second
	limiter := speedbump.NewLimiter(client, hasher, 5)

	for {
		// This example has a hardcoded IP, but you would replace it with the IP
		// of a client on a real case.
		success, err := limiter.Attempt("127.0.0.1")

		if err != nil {
			panic(err)
		}

		if success {
			fmt.Println("Successful!")
		} else {
			fmt.Println("Limited! :(")
		}

		time.Sleep(time.Millisecond * time.Duration(100))
	}
}
  • Output:
Successful!
Successful!
Successful!
Successful!
Successful!
Successful!
Limited! :(
Limited! :(
Limited! :(
Limited! :(
Limited! :(
Successful!
Successful!
Successful!
Successful!
Successful!
Successful!
Limited! :(
Limited! :(
Limited! :(
Limited! :(
Successful!
Successful!
...