Skip to content

Instantly share code, notes, and snippets.

@LasTshaMAN
LasTshaMAN / limiter.go
Created October 2, 2020 18:14
Sliding window rate limiter
// We'll have a slot for each millisecond.
const slotCnt = int(time.Second / time.Millisecond)
type RateLimiter struct {
// rpsBufferMs is a ring buffer that contains a slot per millisecond.
rpsBufferMs [slotCnt]int
lastRpsValue int
lastSlot int
lastTime time.Time