This article describes a solution for limiting request throughput to a web server. I needed to restrict processing to a maximum of 10 items per second after discovering that my firewall was dropping connections opened too frequently.
The Problem
Using Parallel.ForEach, the .NET framework automatically scales worker threads based on performance detection. However, this approach created unpredictable throughput dependent on system load and hardware capabilities. The solution required explicit throttling of work item processing.
The Solution
Inspired by the token bucket algorithm concept, I developed a simple Throttle class that:
- Tracks items processed within a time window
- Resets the counter when the duration expires
- Blocks threads via
Thread.Sleepwhen the limit is reached
Code Implementation
The core class uses synchronized access with a lock statement:
public class Throttle
{
private readonly object syncRoot = new object();
private readonly int limit;
private readonly TimeSpan duration;
private int currentItems;
private DateTime next = DateTime.MinValue;
public Throttle(TimeSpan duration, int limit) { }
public void Wait() { /* implementation */ }
} Example Usage
Processing 1,000 items at 10 per second should take approximately 100 seconds.
Assessment
I acknowledged trade-offs: all is synchronized by the lock statement and blocking, but found it good enough for my current purpose.