gofr icon indicating copy to clipboard operation
gofr copied to clipboard

En/rate limiter

Open Umang01-hash opened this issue 3 months ago • 2 comments

Add Rate Limiting Support for HTTP Services

This PR adds rate limiting capabilities to # Add Rate Limiting Support for HTTP Services

This PR adds rate limiting capabilities to GoFr's HTTP service clients using the token bucket algorithm. It provides both local (in-memory) and distributed (Redis-based) implementations to suit different deployment scenarios.

Features

  • Two Implementation Strategies:

    • Local (In-Memory): For single-instance deployments
    • Distributed (Redis): For multi-instance production deployments using atomic Lua scripts
  • Core Features:

    • Configurable requests per second (RPS) and burst limits
    • Custom service key extraction support

Usage Example

// For distributed rate limiting (multi-instance)
a.AddHTTPService("payment-api", "http://localhost:9005",
    	&service.RateLimiterConfig{
			Requests: 5,
			Window:   time.Minute,
			Burst:    10,
			KeyFunc: func(req *http.Request) string {
				if req == nil {
					return "unknown"
				}

				return req.URL.Host // Use path as key
			},
			Store: service.NewRedisRateLimiterStore(rc),
		},
)

// For local rate limiting (single instance)
a.AddHTTPService("payment-api", "http://localhost:9005",
    &service.RateLimiterConfig{
        RequestsPerSecond: 1,    // 1 request per second
        Burst:            3,     // Allow bursts up to 3 requests
    },
) 

Usage Screenshots:

  • Logs Screenshot 2025-09-12 at 11 17 31 AM

Checklist:

  • [x] I have formatted my code using goimport and golangci-lint.
  • [x] All new code is covered by unit tests.
  • [x] This PR does not decrease the overall code coverage.
  • [x] I have reviewed the code comments and documentation for clarity.

Thank you for your contribution!

Umang01-hash avatar Sep 12 '25 05:09 Umang01-hash

@Umang01-hash We should consider having support for generic time window, i.e. not limit to per second. Most systems have per minute/hour limits.

akshat-kumar-singhal avatar Sep 22 '25 10:09 akshat-kumar-singhal

@akshat-kumar-singhal sure fixed the same

Users can use it like:

// Per-second rate limiting
app.AddHTTPService("fast-api", "https://fast.api.com",
    &service.RateLimiterConfig{
        Requests: 10,           // 10 requests per second
        Window:   time.Second,  // Explicit time window
        Burst:    15,
    },
)

// Per-hour rate limiting
app.AddHTTPService("batch-api", "https://batch.api.com",
    &service.RateLimiterConfig{
        Requests: 1000,        // 1000 requests per hour
        Window:   time.Hour,   // Hourly window
        Burst:    1200,
    },
)

Umang01-hash avatar Sep 25 '25 06:09 Umang01-hash