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. 导出excel 各 cvs 的方法

    public function orderExcelExport($data,$filename='simple.xls'){ ini_set('max_execution_time', '0'); ...

  2. sh_19_字符串拆分和拼接

    sh_19_字符串拆分和拼接 # 假设:以下内容是从网络上抓取的 # 要求: # 1. 将字符串中的空白字符全部去掉 # 2. 再使用 " " 作为分隔符,拼接成一个整齐的字符串 ...

  3. http状态码301和302详解及区别——辛酸的探索之路

    原文链接:https://blog.csdn.net/grandPang/article/details/47448395 一直对http状态码301和302的理解比较模糊,在遇到实际的问题和翻阅各种 ...

  4. R-ets()

    前情需知 指数预测模型 指数模型是用来预测时序未来值的最常用模型.这类模型相对比较简单,但是实践证明它们的短期预测能力较好.不同指数模型建模时选用的因子可能不同.比如 单指数模型(simple/sin ...

  5. C++入门经典-类成员的可访问性,继承后的可访问性

    1:关键字public.private.protected说明类成员是共有的.私有的,还是保护的.这3个关键字将类划分为3个区域,在public区域的类成员可以在类作用域外被访问,而private区域 ...

  6. FinalCutPro快捷键

    FinalCutPro快捷键使用 FinalCutPro的快捷键使用十分有用,特对一些基本的快捷键进行了总结 1)i:截取片段开始Initial 2)o: 截取片段结束Over i和o可以在一个素材片 ...

  7. D建立app项目(mui)

    参考 http://dev.dcloud.net.cn/mui/getting-started/ 1.ios需要下载iTunes,确保手机能连上电脑 2.mui可参考手册 http://dev.dcl ...

  8. php 获取域名

    echo 'SERVER_NAME:'.$_SERVER['SERVER_NAME'];  //获取当前域名(不含端口号) echo '<p>';   echo 'HTTP_HOST:'. ...

  9. yarn application命令介绍

    yarn application 1.-list     列出所有 application 信息    示例:yarn  application -list 2.-appStates <Stat ...

  10. yum源问题

    配置本地yum源 1.使用工具将iso文件上传到操作系统,或者直接挂载iso文件 2.配置yum #cd /etc/yum.repos.d/ 删除多余的repo文件 # vi /etc/yum.rep ...