← 返回 snowflake 的题目列表Design a Concurrent Rate Limiter with Deferred Requests
类型:online_judge
Design a thread-safe rate limiter with the following requirements:
Start with a single rate-limit rule, then extend the design to support multiple rules applied simultaneously.
A rule can be represented as: at most limit requests may pass within a time window of length window.
When a request is subject to multiple rules, it may execute only if every applicable rule allows it.
A request handler may throw an exception. If handler execution fails, the request must not consume quota from any rule.
Rate-limited requests must not be dropped. They should enter a waiting queue and be processed when capacity becomes available.
Ensure correctness under concurrency: no rule may exceed its limit due to races, and no request may execute more than once.
Discuss and address:
Atomicity across checking and reserving quota for multiple rules;
Quota rollback after handler failure;
Queue fairness and head-of-line blocking;
Out-of-order request completion;
Window boundaries, monotonic clocks, and cleanup.
Describe the core data structures, concurrency-control approach, request lifecycle, and time/space complexity.