Giter Club home page Giter Club logo

go-ratelimiter's Introduction

Go-RateLimiter

Simple rate limiter in Golang

How it works

This rate limiter stores a hashmap of API keys mapped to request timestamps. When a new request is made, the rate limiter will remove any request timestamps older than n seconds. Before the request is processed, the current timestamp is added to the map for the API key. The rate limiter then enforces that the number of requests the API caller has sent in the past n seconds is equal to or less than m.

How to run

go run .\rateLimiter.go

Output

Rate limit config
10 max requests per 10 seconds

Running test for APIKeyTest123
[1706145684] [APIKeyTest123] API call # 1 successful
[1706145684] [APIKeyTest123] API call # 2 successful
[1706145684] [APIKeyTest123] API call # 3 successful
[1706145695] [APIKeyTest123] API call # 4 successful
[1706145695] [APIKeyTest123] API call # 5 successful
[1706145695] [APIKeyTest123] API call # 6 successful
[1706145695] [APIKeyTest123] API call # 7 successful
[1706145702] [APIKeyTest123] API call # 8 successful
[1706145702] [APIKeyTest123] API call # 9 successful
[1706145702] [APIKeyTest123] API call # 10 successful
[1706145702] [APIKeyTest123] API call # 11 successful
[1706145705] [APIKeyTest123] API call # 12 successful
[1706145705] [APIKeyTest123] API call # 13 successful
[1706145705] [APIKeyTest123] Error 429: Too many requests. API call # 14 rate limited

Running test for RateLimiterTest2024
[1706145705] [RateLimiterTest2024] API call # 1 successful
[1706145705] [RateLimiterTest2024] API call # 2 successful
[1706145705] [RateLimiterTest2024] API call # 3 successful
[1706145705] [RateLimiterTest2024] API call # 4 successful
[1706145705] [RateLimiterTest2024] API call # 5 successful
[1706145705] [RateLimiterTest2024] API call # 6 successful
[1706145705] [RateLimiterTest2024] API call # 7 successful
[1706145705] [RateLimiterTest2024] API call # 8 successful
[1706145705] [RateLimiterTest2024] API call # 9 successful
[1706145705] [RateLimiterTest2024] API call # 10 successful
[1706145705] [RateLimiterTest2024] Error 429: Too many requests. API call # 11 rate limited

go-ratelimiter's People

Contributors

vdallco avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.