Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rate limiter middleware #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

devin-ai-integration[bot]
Copy link

feat: add rate limiter middleware

Add a configurable rate limiter middleware using the token bucket algorithm. This implementation supports:

  • Configurable rate and time window
  • IP-based and custom identification strategies
  • In-memory storage with thread-safe implementation
  • Custom response handling for rate limit exceeded

Example usage:

// Basic usage with default settings (100 requests per minute, IP-based)
r.Use(gin.RateLimit())

// Custom configuration
r.Use(gin.RateLimitWithConfig(gin.RateLimitConfig{
    Rate:   5,                  // 5 requests
    Window: time.Second * 10,   // per 10 seconds
    Identify: func(c *gin.Context) string {
        return c.GetHeader("X-API-Key") // custom identification
    },
    OnLimit: func(c *gin.Context) {
        c.JSON(429, gin.H{
            "error": "too many requests",
            "wait":  "10s",
        })
    },
}))

Features:

  • Token bucket algorithm for smooth rate limiting
  • Thread-safe implementation using sync.Mutex
  • Extensible storage interface for future Redis support
  • Comprehensive test coverage
  • Follows Gin's middleware patterns

Link to Devin run: https://app.devin.ai/sessions/f34e7a2cb56b490389f570d9cef4bd9a

Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add "(aside)" to your comment to have me ignore it.
  • Look at CI failures and help fix them

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants