惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
L
LangChain Blog
C
Check Point Blog
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
美团技术团队
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
腾讯CDC
B
Blog
G
Google Developers Blog
The Cloudflare Blog
P
Proofpoint News Feed

博客园 - 北叶青藤

2096. Step-By-Step Directions From a Binary Tree Node to Another Find path from root to a target node in Binary Tree When Dijkstra Algorithm Should be Use? 1188. Design Bounded Blocking Queue 1115. Print FooBar Alternately 1114. Print in Order 1242. Web Crawler Multithreaded Python Multi-threading bot ip Same Word of HTML Labels Most Frequent Call Chain remove prefix in a words list 1102. Path With Maximum Minimum Value Property Booking Optimizer minimum number 755. Pour Water Keyword Tagging in Reviews with Overlapping Matches Retryer Function Implementation 1125. Smallest Sufficient Team Print the terrain Split stay Task scheduling problem 滑雪问题 845. Longest Mountain in Array 723. Candy Crush 1539. Kth Missing Positive Number 1650. Lowest Common Ancestor of a Binary Tree III 424. Longest Repeating Character Replacement 843. Guess the Word 551. Student Attendance Record I
Log Rate Limiter
北叶青藤 · 2026-04-06 · via 博客园 - 北叶青藤

Problem: Storefront Log Rate Limiter

You are given a stream of log events. Each event contains at least:

  • timestamp: arrival time (assume integer seconds or milliseconds)
  • store_id: storefront identifier
  • (optional) event_type / message

Implement a rate limiter that decides whether each log should be accepted and written downstream.

Requirements

  1. Rate limit independently per store_id.
  2. In any sliding time window of length window, allow at most limit logs per store_id; extra logs must be rejected.
  3. Output an ALLOW/REJECT decision for every input event.

I/O (conceptual)

  • Input: events (timestamp, store_id) in arrival order, plus window and limit.
  • Output: ALLOW or REJECT per event.

Example

window = 10slimit = 3

  1. (1, A) -> ALLOW
  2. (2, A) -> ALLOW
  3. (3, A) -> ALLOW
  4. (4, A) -> REJECT
  5. (12, A) -> ALLOW

Constraints

  • High-throughput stream processing.
  • Aim for amortized ~O(1) per event.
 1 from collections import deque, defaultdict
 2 
 3 class StorefrontRateLimiter:
 4     def __init__(self, window, limit):
 5         self.window = window
 6         self.limit = limit
 7         # Automatically creates a new deque when a store_id is accessed for the first time
 8         self.store_history = defaultdict(deque)
 9 
10     def process_event(self, timestamp, store_id):
11         history = self.store_history[store_id]
12 
13         # Cleanup: Remove timestamps outside the sliding window
14         while history and history[0] <= timestamp - self.window:
15             history.popleft()
16 
17         # Rate Limit Logic
18         if len(history) < self.limit:
19             history.append(timestamp)
20             return "ALLOW"
21         
22         return "REJECT"