Whenever you expose a web service / api endpoint, you need to implement a rate limiter to prevent abuse of the service (DOS attacks).

Implement a RateLimiter Class with an isAllow method. Every request comes in with a unique clientID, deny a request if that client has made more than 100 requests in the past second.

  1. public class RateLimiter {
  2. private final Map<String, LinkedList<Long>> clientHitMap = new HashMap<>();
  3. private final int REQUEST_LIMIT = ;
  4. private final long TIME_LIMIT = 1000L;
  5.  
  6. public boolean isAllow(String client_id) {
  7. LinkedList<Long> list = clientHitMap.get(client_id);
  8. long curTime = System.currentTimeMillis();
  9. if (list == null) {
  10. clientHitMap.put(client_id, new LinkedList<Long>());
  11. }
  12. while (!list.isEmpty() && curTime - list.peek() >= TIME_LIMIT) {
  13. list.poll();
  14. }
  15. if (list.size() < REQUEST_LIMIT) {
  16. list.offer(curTime);
  17. return true;
  18. }
  19. return false;
  20. }
  21. }

Rate Limiter的更多相关文章

  1. rate limiter - system design

    1 问题 Whenever you expose a web service / api endpoint, you need to implement a rate limiter to preve ...

  2. 359. Logger Rate Limiter

    /* * 359. Logger Rate Limiter * 2016-7-14 by Mingyang * 很简单的HashMap,不详谈 */ class Logger { HashMap< ...

  3. RocksDB Rate Limiter源码解析

    这次的项目我们重点关注RocksDB中的一个环节:Rate Limiter.其实Rate Limiter的思想在很多其他系统中也很常用. 在RocksDB中,后台会实时运行compaction和flu ...

  4. [LeetCode] Logger Rate Limiter 记录速率限制器

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

  5. LeetCode 359 Logger Rate Limiter

    Problem: Design a logger system that receive stream of messages along with its timestamps, each mess ...

  6. LeetCode Logger Rate Limiter

    原题链接在这里:https://leetcode.com/problems/logger-rate-limiter/ 题目: Design a logger system that receive s ...

  7. Rate Limiter设计

    先存着,以后再写 http://iamzhongyong.iteye.com/blog/1982113 http://baike.baidu.com/view/2530454.htm https:// ...

  8. Logger Rate Limiter 十秒限时计数器

    [抄题]: Design a logger system that receive stream of messages along with its timestamps, each message ...

  9. Logger Rate Limiter -- LeetCode

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

随机推荐

  1. BZOJ 2651 城市改建 树形DP+模拟?

    题意 给一颗树,删除一条边再加一条边,使它仍为一颗树且任意两点间的距离的最大值最小. 题目数据范围描述有问题,n为1或重建不能使任意两点距离最大值变小,可以输出任意答案. 分析 删除一条边后会使它变成 ...

  2. 关于web前端开发,区别用户使用的终端设备代码

    对web前端开发,熟知不同的浏览器,的解析是基本技能,下面,我就向大家展示一下,如何获得当前用户所用的终端设备.车子一发动,请准备上车................... <script> ...

  3. C++入门经典-例7.1-对象之访问类成员

    1:建立一个类CPerson. (1)在person.h文件中代码: class CPerson { public: //数据成员 int m_iIndex; ]; short m_shAge; do ...

  4. mysql java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone.

    连接数据库时报错: The server time zone value '?й???????' is unrecognized or represents more than one time zo ...

  5. 【软件工程】Alpha冲刺(2/6)

    链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 了解了如何根据系统获取的实际情况进行后端任务的调整 网易云音乐推荐算法的分析 ...

  6. Python全栈开发第5天作业

    作业一:1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) 2) 将用户信息数据库文件和用户密码数据库文件纵向合并为一个文件/2.txt(追加) 3) 将/1.txt. ...

  7. slab分配object

    在numa架构下,slab分配object: 3192static __always_inline void * 3193__do_cache_alloc(struct kmem_cache *cac ...

  8. python调用系统命令的方法

    1.os模块 (1)system()方法 这个方法是直接调用标准C的system() 函数,在一个子终端运行系统命令 (2)poen()方法 这个方法执行命令后通过一个管道文件将结果返回 3.subp ...

  9. LC 687. Longest Univalue Path

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  10. 阶段3 3.SpringMVC·_07.SSM整合案例_03ssm整合之编写Spring框架

    做整合要保证每个框架单独使用 先搭建Spring的框架,然后再整合别的框架.Spring是业务层的框架 spring的配置文件 这就表示是spring的配置文件 默认的约束不够,需要修改. <b ...