Skip to content

sadfun/limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rate Limiter

  • Custom storage (Map, Redis, KeyDB, etc)
  • Very precise algorithm (Sliding window log)
  • Generics-driven (store limits by any comparable type)
  • No external dependencies

Performance considerations

Please keep in mind that sliding window log is very accurate, but can use up to O(nm) memory, where m is max limit and n is count of connected clients. This is a trade-off between precision and performance. Thus, this limiter is recommended for counting very costly operations, such as credit card transactions, account creations, AI model calls etc.

Installation

go get -u github.com/sadfun/limiter

Usage

package main

import (
    "fmt"
    "time"

    "github.com/sadfun/limiter"
    "github.com/sadfun/limiter/storage"
)

func main() {
    limiter := limiter.NewLimiter[string](&limiter.Config[string]{
        Limit: 10,
        Duration: time.Second,
        Storage: storage.NewMapStorage[string](),
    })

    if !limiter.Use("key") {
        panic("Limit exceeded")
    }
}

Testing

go test -v .

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages