Back
+30 XP
Concept

Gauntlet 10: Leaky Bucket Traffic Shaper

Token and leaky bucket algorithms smooth out bursty network traffic into a steady, predictable output rate. Arriving tokens or request units accumulate up to a maximum bucket capacity, steadily leaking at a configured rate per second to protect downstream APIs from load spikes.

python
# Leaky bucket leak calculation:
elapsed = time.time() - last_leak
water = max(0.0, water - elapsed * leak_rate)

# Gauntlet 10: Leaky Bucket Traffic Shaper & Egress Throttler When an upstream user submits a batch of 500 requests at the exact same millisecond, sending all 500 requests directly to a third-party payment gateway or external webhook will trigger rate-limit blocks (`429 Too Many Requests`). 🌊🛑 Traffic shaping algorithms smooth out bursty traffic: - **Bucket Capacity ($B$)**: Maximum surge/burst volume allowed. - **Leak Rate ($R$)**: The steady rate at which requests drain out (e.g. 10 requests per second). - **The Algorithm**: 1. On each incoming request, calculate time elapsed since the last tick: $\Delta t = \text{now} - \text{last\_time}$. 2. Drain water: $\text{water} = \max(0.0, \text{water} - \Delta t \times R)$. 3. If $\text{water} + \text{cost} \le B$: accept request, update $\text{water} += \text{cost}$, and return `True`. 4. Otherwise: reject or queue request, returning `False`. ```python import time # Continuous time-based drain: water = max(0.0, water - elapsed * leak_rate) ``` ## Pop Quiz What is the primary difference between a Token Bucket and a Leaky Bucket traffic shaper? - [ ] Token buckets permit short bursts of traffic up to bucket capacity, whereas leaky buckets output packets at a strictly constant rate. - [ ] Token buckets only work over UDP while leaky buckets operate over TCP. - [ ] Leaky buckets require separate hardware clocks on network interface cards. - [ ] Because token buckets encrypt network payloads before transmission. ## Real-World Challenge Implement `LeakyBucket(capacity: float, leak_rate_per_sec: float)`: - `allow(cost: float = 1.0) -> bool`: Computes continuous drainage based on `time.time()`. Accepts if `water + cost <= capacity` and increments water level; otherwise returns `False`. - `current_level -> float`: Returns current water level after applying time drainage. <details> <summary>Solution</summary> ```python import time class LeakyBucket: def __init__(self, capacity: float, leak_rate_per_sec: float): if capacity <= 0 or leak_rate_per_sec <= 0: raise ValueError("Capacity and leak rate must be positive") self.capacity = float(capacity) self.leak_rate = float(leak_rate_per_sec) self._water = 0.0 self._last_leak = time.time() def _drain(self, now: float): elapsed = now - self._last_leak if elapsed > 0: self._water = max(0.0, self._water - elapsed * self.leak_rate) self._last_leak = now @property def current_level(self) -> float: self._drain(time.time()) return self._water def allow(self, cost: float = 1.0) -> bool: now = time.time() self._drain(now) if self._water + cost <= self.capacity: self._water += cost return True return False # Verification bucket = LeakyBucket(capacity=3.0, leak_rate_per_sec=10.0) # 1. Burst up to capacity (3 units allowed) assert bucket.allow(1.0) is True assert bucket.allow(1.0) is True assert bucket.allow(1.0) is True # 2. Exceeding capacity rejected assert bucket.allow(1.0) is False # 3. Wait 0.15s (drains 1.5 units) -> allows new requests time.sleep(0.15) assert bucket.allow(1.0) is True ``` </details>