Skip to content

Latest commit

 

History

History
47 lines (40 loc) · 1.62 KB

README.md

File metadata and controls

47 lines (40 loc) · 1.62 KB

Golang rate limiter for distributed system

Build Status Coverage Status Go Report Card license

Implementation

According to Stripe's rate-limiters practice, use Redis Server & Lua Script to implement a rate limiter based on token bucket algorithm. 在github.com/wallstreetcn/rate的基础上适配了codis和最新版本的redis客户端。

Install

go get "github.com/wallstreetcn/rate"

go get "github.com/bigo-sg/rate"

Usage

import (
    rate "github.com/bigo-sg/rate/redis"
)

// initialize redis.
rate.SetRedis(&rate.ConfigRedis{
    Host: "127.0.0.1",
    Port: 6379,
    Auth: "",
}

// setup a 1 ops/s rate limiter.
limiter := rate.NewLimiter(Every(time.Second), 1, "a-sample-operation")
if limiter.Allow() {
    // serve the user request
} else {
    // reject the user request
}

// setup a 1000 ops/s rate limiter.
limiter := rate.NewLimiter(Every(time.Second/time.Duration(1000)), 1000, "a-sample-operation")
if limiter.Allow() {
    // serve the user request
} else {
    // reject the user request
}