Skip to content

A Go project showcasing three rate-limiting implementations: per-client (IP-based), token bucket, and tollbooth middleware. Each approach is modularized for clarity and efficiency.

Notifications You must be signed in to change notification settings

likhithkp/rate-limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Rate Limiter in Go

This repository demonstrates three different approaches to implementing rate limiting in Go:

  1. Per-Client Rate Limiting (IP-Based)
  2. Token Bucket Algorithm
  3. Tollbooth Middleware

Each implementation is organized into separate folders for clarity and modularity.


1. Per-Client Rate Limiting (IP-Based)

This method limits requests per unique IP address. It maintains a map of clients and applies rate limiting per client using the golang.org/x/time/rate package.

Key Features:

  • Tracks clients based on IP
  • Uses rate.Limiter for rate limiting
  • Cleans up inactive clients after 3 minutes

How to Run:

cd perclientlimit
go run main.go

2. Token Bucket Algorithm

A general rate-limiting technique that allows a burst of requests up to a limit and refills tokens at a fixed rate.

Key Features:

  • Implements a token bucket system
  • Uses rate.Limiter(2, 4), allowing 2 requests per second with a burst capacity of 4

How to Run:

cd tokenbucket
go run main.go

3. Tollbooth Middleware

This method uses the tollbooth package to implement rate limiting as middleware, making it easy to integrate with existing applications.

Key Features:

  • Simple and effective middleware
  • Customizable error messages and response types
  • Limits requests using tollbooth.NewLimiter(1, nil), allowing 1 request per second

How to Run:

cd tollbooth
go run main.go

API Endpoints

Endpoint Description Rate Limit
/pingp IP-based rate limiter 2 req/sec, burst 4
/ping Token bucket rate limiter 2 req/sec, burst 4
/pingtoll Tollbooth middleware limiter 1 req/sec

Installation & Dependencies

Ensure you have Go installed and run:

go mod tidy

Install Tollbooth if needed:

go get -u github.com/didip/tollbooth/v7

Author

Likhith K.P

License

MIT License

About

A Go project showcasing three rate-limiting implementations: per-client (IP-based), token bucket, and tollbooth middleware. Each approach is modularized for clarity and efficiency.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy