Skip to content
/ rate Public
forked from wallstreetcn/rate

Golang rate limiter for distributed system

License

Notifications You must be signed in to change notification settings

bigo-sg/rate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
}

About

Golang rate limiter for distributed system

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%