Skip to content

wallstreetcn/rate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

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

Install

go get "github.com/wallstreetcn/rate"

Usage

import (
    rate "github.com/wallstreetcn/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
}

Releases

No releases published

Packages

No packages published

Languages