Skip to content

Santhoshkumar044/Distributed-Rate-limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

A distributed rate limiter written in Go, built to understand how real-world systems handle traffic at scale.


Features

  • Token Bucket algorithm (time-based)
  • Sliding Window Log
  • Per-user rate limiting
  • Distributed state using Redis
  • Atomic updates via Lua scripting

Prerequisites

  • Redis server
  • Refer cmd/demo/main.go for implementation reference

Usage

Installation

go get github.com/Santhoshkumar044/distributedRatelimiter

Import

import (
	"github.com/Santhoshkumar044/distributedRatelimiter/ratelimiter"
)

Middleware Example

func RateLimitMiddleware(limiter ratelimiter.RateLimiter) func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

			user := r.RemoteAddr // identify user (IP / API key)

			res, err := limiter.Allow(user)
			if err != nil {
				http.Error(w, "Internal Server Error", http.StatusInternalServerError)
				return
			}

			// optional headers
			w.Header().Set("X-RateLimit-Remaining", fmt.Sprintf("%.2f", res.TokensLeft))
			w.Header().Set("Retry-After", fmt.Sprintf("%.0f", res.TryAfter))

			if !res.Allowed {
				w.WriteHeader(http.StatusTooManyRequests)
				w.Write([]byte("Too Many Requests\n"))
				return
			}

			next.ServeHTTP(w, r)
		})
	}
}

Direct Usage

rds := redis.NewClient(&redis.Options{
	Addr: "localhost:6379",
})
// Token Bucket
limiter := ratelimiter.NewRedisTokenBucket(rds, 5, 1)
// Sliding Window 
limiter := ratelimiter.NewSlidingWindowLog(rds,5,10000) // 5 request per 10 seconds 
res, err := limiter.Allow("user-123")
if err != nil {
	panic(err)
}

if res.Allowed {
	fmt.Println("Request allowed")
} else {
	fmt.Printf("Rejected. Try after %.2f seconds\n", res.TryAfter)
}

LICENSE

MIT

About

Distributed Rate Limiter written in Go

Resources

Stars

Watchers

Forks

Contributors