Engineering platform~35 min2s · 256 MBtarget O(n) time, O(tenants) spacemap-design · hashing
A software platform limits how fast each customer, called a tenant, can call its API. Every tenant has a bucket of tokens. A request spends tokens, and the bucket refills with time, a fixed number of tokens each second. The bucket has a size, called the burst: a tenant that has been quiet can send a short burst up to that size.
Tenants can upgrade or downgrade their plan, which changes their burst size. The platform needs one small service that answers three kinds of commands for any number of tenants.
Write run_limiter(rate, default_burst, commands, args).
default_burst tokens. The bucket refills rate tokens each second, up to its burst size."allow" with args[i] = [tenant, time, cost]: refill the tenant's bucket up to time. If it holds at least cost tokens, spend them and answer 1. Otherwise answer 0 and spend nothing."burst" with args[i] = [tenant, size, time]: refill the bucket up to time. Then set the burst size of the tenant to size, and cut the tokens down to size if there are more. It gives no answer."peek" with args[i] = [tenant, time]: refill the bucket up to time and answer the number of tokens."allow" and "peek", in order.Input: rate = 2, default_burst = 10, commands = ["allow", "allow", "allow", "peek", "burst", "peek", "allow", "peek"], args = [[1, 0, 6], [1, 1, 6], [1, 2, 3], [1, 4], [1, 4, 4], [1, 4], [2, 4, 10], [2, 4]]
Output: [1, 1, 0, 6, 4, 1, 0]The trace shows each tenant's tokens after the refill, and after the command. The rate is 2 and the default burst is 10.
| Command | Tenant | Time | Tokens after the refill | Tokens after the command | Answer |
|---|---|---|---|---|---|
| allow cost 6 | 1 | 0 | 10 | 4 | 1 |
| allow cost 6 | 1 | 1 | 6 | 0 | 1 |
| allow cost 3 | 1 | 2 | 2 | 2 | 0 |
| peek | 1 | 4 | 6 | 6 | 6 |
| burst to 4 | 1 | 4 | 6 | 4 | none |
| peek | 1 | 4 | 4 | 4 | 4 |
| allow cost 10 | 2 | 4 | 10 | 0 | 1 |
| peek | 2 | 4 | 0 | 0 | 0 |
The output is [1, 1, 0, 6, 4, 1, 0]. The request of cost 3 is denied at second 2, and tenant 1 keeps its 2 tokens.
Input: rate = 1, default_burst = 5, commands = ["allow", "allow", "peek"], args = [[7, 0, 5], [7, 1, 2], [7, 3]]
Output: [1, 0, 3]Tenant 7 spends all 5 tokens at second 0. At second 1 it has 1 token, so the cost of 2 is denied. By second 3 it has refilled to 3.
Input: rate = 5, default_burst = 3, commands = ["allow", "peek"], args = [[4, 0, 4], [9, 0]]
Output: [0, 3]A request that costs more than the burst size is never allowed. A peek of a new tenant shows a full bucket.
1 ≤ rate ≤ 1,000 (tokens a second), 1 ≤ default_burst ≤ 1,000,0001 ≤ commands.length ≤ 100,000; args has the same length"allow" (3 numbers), "peek" (2 numbers) or "burst" (3 numbers). Tenants, times, costs and burst sizes are between 0 and 1,000,000,000, and times never go down.Simulating every second for every tenant would cost the number of tenants times the number of seconds, which can be 10^11. A bucket only needs to be brought up to date when a command arrives, and the refill is one multiplication for all the seconds in between. So each command costs one map lookup and a few numbers.