Rate Limiting with ThrottleX

Neel Patel - Oct 17 - - Dev Community

Quick Links:

Introduction:

Hey there, open-source enthusiasts and Go aficionados! I'm back with a quick overview of my open-source project, ThrottleX, a distributed rate limiter for APIs. I'm still pretty new to this open-source world, so your advice is always welcome! 🤝

ThrottleX is built to help you manage API traffic effectively, keeping your system smooth and fair. Whether it's preventing abuse or handling high load, ThrottleX has you covered. Let's explore some of the rate limiting algorithms that make ThrottleX powerful. 💪

Rate Limiting Algorithms Explained 👨‍💻

ThrottleX comes packed with three core algorithms to help you manage your API traffic:

1. Fixed Window Rate Limiting

How It Works: Imagine dividing time into fixed intervals. During each interval, you allow a fixed number of requests — say, 100 requests per minute. Once the window closes, the counter resets.

Use Case: Great for predictable traffic patterns but be wary of the "boundary issue," where many requests near the window's end can bypass the intended rate limit.

2. Sliding Window Rate Limiting

How It Works: It's like a moving average — instead of resetting completely at the end of each window, the rate limit "slides" across time, providing smoother control.

Use Case: Perfect for avoiding spikes and distributing requests more evenly.

3. Token Bucket Rate Limiting

How It Works: Picture a bucket that fills up with tokens at a steady rate. Requests consume tokens, and if the bucket is empty, requests are blocked until it refills.

Use Case: Ideal for allowing sudden bursts of traffic if tokens are saved up.

Example Usage 🛠️

Here's a simple example using the Fixed Window rate limiter:

package main

import (
    "github.com/neelp03/throttlex/ratelimiter"
    "github.com/neelp03/throttlex/store"
    "time"
    "fmt"
)

func main() {
    // Initialize an in-memory store and a Fixed Window rate limiter
    memStore := store.NewMemoryStore()
    limiter, err := ratelimiter.NewFixedWindowLimiter(memStore, 10, time.Minute)
    if err != nil {
        fmt.Println("Failed to create limiter:", err)
        return
    }

    // Simulate API requests
    key := "user1"
    for i := 0; i < 15; i++ {
        allowed, err := limiter.Allow(key)
        if err != nil {
            fmt.Println("Error:", err)
            continue
        }
        if allowed {
            fmt.Printf("Request %d allowed\n", i+1)
        } else {
            fmt.Printf("Request %d blocked\n", i+1)
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Expected Output:

Request 1 allowed
Request 2 allowed
... (up to 10 allowed)
Request 11 blocked
Request 12 blocked
... (up to 15 blocked)
Enter fullscreen mode Exit fullscreen mode

What's Next for ThrottleX? 🚀

  • Prioritized Rate Limiting: Different priorities for different users.
  • Dynamic Rate Limiting: Adjust limits based on server load.
  • Concurrency Limit: Manage simultaneous requests.

Stay tuned for these future updates that will make ThrottleX even more powerful!

How You Can Contribute 🥇

I'm still new to open source, and I'd love for you to be part of this journey! Contributions are always welcome — whether it's bug fixes, suggestions, or documentation improvements.

Check out the repo: ThrottleX GitHub Repo and give it a star if you find it useful. Feel free to open issues or pull requests — every bit helps!

Final Thoughts

ThrottleX is my attempt to make API rate limiting more accessible and efficient. Let's make it fun (and less of a pain)! 🎉

. . . . . . . . . . . . . . . .
Terabox Video Player