当前位置: 首页 > news >正文

Log Rate Limiter

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"

 

http://www.jsqmd.com/news/596520/

相关文章:

  • 解决vue-quill-editor保存后莫名多空行问题(附实测有效CSS方案)
  • 【金蝶云星空】应付做账-单到补差(有发票模块)
  • Windows缩略图预加载终极解决方案:彻底告别文件夹浏览卡顿
  • Yolov8_OBB斜框数据集制作与训练全流程实战指南
  • 解锁音乐自由:从NCM加密困扰到全格式播放的高效转换方案
  • AI量化平台Qlib从入门到精通:构建智能投资策略的完整指南
  • 文脉定序系统处理操作系统日志:故障信息智能归类与排序
  • Android Studio开发加速:集成Qwen3.5-2B生成UI代码与处理逻辑
  • TortoiseGit解决冲突代码实战
  • Windows 11性能优化终极指南:Win11Debloat让你的系统提速51%的完整方案
  • Mac玩金铲铲开黑没声音?手把手教你用终端+Python3修复PlayCover麦克风权限
  • OpenClaw定时任务:利用SecGPT-14B实现夜间自动化安全巡检
  • 别再为并行计算环境发愁了!手把手教你用VS2022搞定OpenMP和MPI(Windows版)
  • 3个维度解析VoiceFixer:让受损语音重获新生的开源解决方案
  • DS4Windows终极教程:3分钟让PlayStation手柄完美兼容Windows游戏
  • RuoYi+Vue.js实战:如何用开源框架快速搭建汽车4S店进销存系统(附完整代码)
  • JAVA无人共享健身房预约小程序源码实现方案及开源代码片段
  • OpenCore Legacy Patcher焕新体验:老旧Mac系统升级全攻略
  • 2026届最火的十大降重复率方案横评
  • 高效完成毕业论文答辩:10大AI工具(含爱毕业aibiye)及模板使用指南
  • Skills 技能扩展——怎么给你的虾装上新的钳子|卷卷养虾记 · 第六篇
  • 让业务人员直接“问“数据库:Spring AI Alibaba NL2SQL 实战指南
  • 芯模振动制管设备的安装难度大吗
  • 4步实现HMCL数据无缝迁移:从诊断到优化的全流程指南
  • 39、【Agent】【OpenCode】本地代理分析(三)
  • AutoUnipus学习效率工具:提升在线学习体验的智能辅助方案
  • seo竞价排名优化需要定期调整和优化的主要原因是什么_seo竞价排名优化的基本概念是什么
  • 毕业论文答辩新选择:10款AI辅助工具(含爱毕业aibiye)与模板测评
  • 40、【Agent】【OpenCode】本地代理分析(四)
  • 3大理由告诉你为什么7-Zip是Windows文件压缩的最佳选择