speedbump

A Redis-backed rate limiter in Go

Github星跟踪图

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!
...

主要指标

概览
名称与所有者etcinit/speedbump
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2015-04-18 22:47:36
推送于2022-03-28 19:17:09
最后一次提交2016-07-26 21:47:23
发布数5
最新版本名称v2.0.0 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数116
关注者数5
派生数10
提交数33
已启用问题?
问题数1
打开的问题数1
拉请求数7
打开的拉请求数2
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?