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. windows环境下,mysql的root密码丢失后重置方法

    运行窗口输入 services.msc,检查mysql服务是否启动,如果启动手动停止或输入 net stop mysql 停止msyql服务.   打开cmd命令行,使用cd命令进入mysql 的bi ...

  2. #7 div2 B Layer Cake 造蛋糕 智商题+1

    B - Layer Cake Time Limit:6000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u Sub ...

  3. IE 不兼容 console 关键字

    如果在JS文件中写了console.log()方法,在IE下打开 开发者工具就没问题,不打开就有问题js 报错 不执行等等 ...... (IE这都是什么鬼!!),百度过后的解决方法如下: 好吧,这其 ...

  4. web 新能优化

    网上的东西太多了都是搜来的东西 留着自己看吧! 摘自 :http://www.cnblogs.com/50614090/archive/2011/08/19/2145620.html 打开网站慢现状分 ...

  5. OUC_Summer Training_ DIV2_#5

    这是做的最好的一次了一共做了4道题  嘻嘻~ A - Game Outcome Time Limit:2000MS     Memory Limit:262144KB     64bit IO For ...

  6. 2.1 MATLAB的数据类型

    2.1 MATLAB的数据类型 每种数据类型都是以矩阵的形式存在的 数据类型:数值型.字符型.元胞型.结构体.函数句柄 数值型包含:双精度类型.单精度类型.整型 支持不同数据的转换 2.1.1 变量与 ...

  7. 报错:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin' for key 'username'

    在提交注册信息的时候报错:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin' for key ' ...

  8. mysq乱码问题

    不乱码的思想 liunx字符集→linux客户端字符集(例如:ssh)→mysql客户端字符集→mysql服务端字符集→库的字符集→表的字符集→程序字符集统一 mysql表跟库,库跟服务端字符集 li ...

  9. 把java项目打包成jar包并可以直接运行【我】

    首先创建一个maven的jar项目,然后代码写好后,在项目右键,导出: 选择java下面的可运行的jar文件: 下一步: 要注意的是: launch configuration 此选项是指定选中要导出 ...

  10. Mysql密码忘记,修改密码方法

    1.set password for ‘root’@’localhost’ = password(‘czllss’); -- czllss为新密码