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.

 public class RateLimiter {
private final Map<String, LinkedList<Long>> clientHitMap = new HashMap<>();
private final int REQUEST_LIMIT = ;
private final long TIME_LIMIT = 1000L; public boolean isAllow(String client_id) {
LinkedList<Long> list = clientHitMap.get(client_id);
long curTime = System.currentTimeMillis();
if (list == null) {
clientHitMap.put(client_id, new LinkedList<Long>());
}
while (!list.isEmpty() && curTime - list.peek() >= TIME_LIMIT) {
list.poll();
}
if (list.size() < REQUEST_LIMIT) {
list.offer(curTime);
return true;
}
return false;
}
}

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. BZOJ1968: [Ahoi2005]COMMON 约数研究 线性筛

    按照积性函数的定义筛一下这个积性函数即可. #include <cstdio> #include <algorithm> #define N 1000004 #define s ...

  2. vue整合adminLTE

    前端框架AdminLTE 中文教程 如何用vue整合adminlte模板 1.adminlte 下载地址 : https://github.com/almasaeed2010/AdminLTE/rel ...

  3. js基础----数组

    1.数组如何定义 //第一种定义方法 var arr=[1,2,3,4]; //第二种定义方法 var arr=new Array(1,2,3,4); 两者没有任何区别,[]的性能可能略高,因为代码短 ...

  4. apt-get build-dep命令详解

    apt-get build-dep命令详解 - 星星之火的Blog - CSDN博客  https://blog.csdn.net/starflame/article/details/7416311 ...

  5. 用python进行服务器的监控

    用python进行服务器的监控 在linux服务器中,一切皆为文件,就是说,服务器运行的个中信息,其实是可以从某些文件中查询得到的:百度后,你会知道,在Linux系统中,有一个/proc的虚拟文件系统 ...

  6. Into Blocks (easy version)

    G1 - Into Blocks (easy version) 参考:Codeforces Round #584 - Dasha Code Championship - Elimination Rou ...

  7. SQL语句中 NOT IN 子句的“正确打开方式”

    在写SQL语句的时候,若where条件是判断用户不在某个集合当中,我们习惯使用 where 列名 not in (集合) 子句,这种写法本身没有问题,但实践过程中却发现很多人在写类似的SQL语句时,写 ...

  8. 浏览器是如何处理页面元素的Download?

    首先,浏览器对于script的下载是避免并行进行的.HTTP/1.1协议中规定浏览器和同一host之间只建立最多两个连接,也就是说允许的最 大并行度为2(当然,对IE和Firefox来说,你都可以通过 ...

  9. Centos7 yum install chrome

    一.配置 yun 源 vim /etc/yum.repos.d/google-chrome.repo [google-chrome] name=google-chrome baseurl=http:/ ...

  10. 在Excel中,已知身份证号码,如何批量计算其实际年龄?

    昨天,上司问我:..,你会在Excel中计算年龄吗?当时,一下促住了.说真的,还真不会.今天研究了一下,写下来,方便日后查看. 首先,得有一张已知姓名和相应身份证号的原表吧. 在这张表上再加上三列:出 ...