rate

module
v0.0.0-...-f58d277 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2020 License: MIT

README

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/bynf/rate"

Usage

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

Directories

Path Synopsis
Package redis provides a rate limiter based on redis.
Package redis provides a rate limiter based on redis.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL