Scalable golang ratelimiter using the sliding window algorithm. Currently supports only Redis.

Overview

go-ratelimiter

Scalable golang ratelimiter using the sliding window algorithm. Currently supports only Redis.

Example usage

	client := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})
  
	rl := ratelimiter.NewRateLimiter(&ratelimiter.Options{
		Name:           "test",
		Datastore:      ratelimiter.NewRedisDatastore(client),
		LimitPerMinute: 5,
		WindowInterval: 1 * time.Minute,
		BucketInterval: 5 * time.Second,
		WriteHeaders:   false,
		DevMode:        false,
	})

	throttled, err := rl.ShouldThrottle(context.Background(), nil, ratelimiter.Field{
		Scope:      "user",
		Identifier: "127.0.0.1",
	})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(throttled)

Headers

Optionally sets HTTP Headers to response. https://tools.ietf.org/id/draft-polli-ratelimit-headers-00.html

Design

All Redis operations are atomic. Fit for distributed environments.

Window interval is divided into smaller buckets. Each bucket is saved as hash key in Redis. Inside of each bucket, we have field and value. Field is the unique identifier in the format of "ratelimiter_name:scope:value" to avoid collisions, eg: "test_limiter:ip:127.0.0.1". Value is incremented on each request in the bucket.

alt text

When we want to figure out if the user is above limit:

alt text

You might also like...
Flock is a project which provides a Go solution for system level file locks for all platforms Golang supports.

Flock is a project which provides a Go solution for system level file locks for all platforms Golang supports.

A simple thread-safe, fixed size LRU written in Go. Based on dominictarr's Hashlru Algorithm. 🔃

go-hashlru A simple thread-safe, fixed size LRU written in Go. Based on dominictarr's Hashlru Algorithm. 🔃 Uses map[interface{}]interface{} to allow

A faster RWLock primitive in Go, 2-3 times faster than RWMutex. A Go implementation of concurrency control algorithm in paper Left-Right - A Concurrency Control Technique with Wait-Free Population Oblivious Reads

Go Left Right Concurrency A Go implementation of the left-right concurrency control algorithm in paper Left-Right - A Concurrency Control Technique w

EVM-compatible chain secured by the Lachesis consensus algorithm.

ICICB-Galaxy EVM-compatible chain secured by the Lachesis consensus algorithm. Building the source Building galaxy requires both a Go (version 1.14 or

Identify containers at runtime and observe them. No container runtime required. Read only access to the kernel.

Linux Telemetry The Double Slit Experiment Taken from an interesting physics anomaly where the behavior of a physical system mutates simply by being o

Grabs the IP adress, discord tokens and Windows NT Users in the machine. Only for educational purpuses!!

go-malware Grabs the IP adress, discord tokens and Windows NT Users in the machine. Only for educational purpuses!! Edit the main file and put your we

Execute a binary from memory, without touching the disk. Linux only.
Execute a binary from memory, without touching the disk. Linux only.

Memit Execute a binary from memory, without touching the disk. Linux only. Available as both a Go module and a binary. Using the Go module The Command

📋 cross-platform clipboard package that supports accessing text and image in Go (macOS/Linux/Windows/Android/iOS)

clipboard Cross platform (macOS/Linux/Windows/Android/iOS) clipboard package in Go import "golang.design/x/clipboard" Features Cross platform supports

Goterators - A util library that Supports aggregate & transforms functions Go. Such as filter, map, reduce, find, exist
Goterators - A util library that Supports aggregate & transforms functions Go. Such as filter, map, reduce, find, exist

Goterators Goterators is util library that Supports aggregate & transforms functions Go, including: for-each find exist reduce filter map API and func

Owner
null
Sliding window counters Redis rate limiting implementation for Golang

Sliding window counters Redis rate limiting implementation for Golang (Based on the Figma API rate limit algorithm)

Barnaby Keene 5 Apr 4, 2022
A simple business indicator tool that uses a sliding window to detect whether the indicator exceeds the threshold

melon A simple business indicator tool that uses a sliding window to detect whether the indicator exceeds the threshold Usage //create the metric //th

believe 4 Jul 11, 2021
Sentiment Analysis Pipeline + API written in Golang (currently processing Twitter tweets).

Go Sentiment Analysis Components Config: config module based in JSON (enter twitter credentials for use) Controllers: handle the API db call/logic for

Joseph Moussa 1 Mar 22, 2022
Sentiment Analysis Pipeline + API written in Golang (currently processing Twitter tweets).

Go Sentiment Analysis Components Config: config module based in JSON (enter twitter credentials for use) Controllers: handle the API db call/logic for

Joseph Moussa 1 Mar 22, 2022
redis-util business-friendly encapsulation of redis operations, such as the common cache set get operation

redis-util 方便业务使用的redis操作封装,比如常见的缓存set get操作, 一行代码搞定,不像开源库需要写好多行 使用方法

connor 1 Oct 22, 2021
A program to create assembly 8086 strings to print without using any printing/strings related function but only mov-xchg-int and loops

Assembly String builder tool A program to create assembly 8086 strings to print without using any printing/strings related function but only mov-xchg-

Reg 2 Feb 1, 2022
Fast and scalable pseudorandom generator for Go

fastrand Fast pseudorandom number generator. Features Optimized for speed. Performance scales on multiple CPUs. How does it work? It abuses sync.Pool

Aliaksandr Valialkin 178 Nov 17, 2022
A simple API for computing diffs of your documents over the time built on a scalable technology stack.

Diffme API WIP - this is an API to compute diffs between documents. It serves as a way to easily create audit logs for documents in your system, think

diffme 11 Sep 8, 2021
Fast, scalable pseudo random number generator based on xxh3

XXH3-Based Pseudorandom Number Generator This package contains an experimental implementation of a noise based pseudorandom number generator that scal

Roman Atachiants 10 Nov 24, 2022
golang implement gossip algorithm

golang implement gossip algorithm

Gabriella Munger 3 Jan 14, 2022