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;
}
}

最新文章

  1. Linux进程间通信(七):消息队列 msgget()、msgsend()、msgrcv()、msgctl()
  2. java 反射实践
  3. 搭建LNAMP环境(五)- PHP7源码安装Redis和Redis拓展
  4. JavaScript学习笔记-实例详解-类(一)
  5. oracle 常见恢复
  6. python 版本问题大全
  7. Communication - 01.Foreword
  8. Oracle PL/SQL中的循环处理(sql for循环)
  9. ubuntu的目录结构
  10. 部署与管理ZooKeeper(转)
  11. HTML5学习笔记简明版(1):HTML5介绍与语法
  12. jsp页面转发到servlet
  13. FZYZOJ-1880 【UFO】水管
  14. Android图片框架---Glide
  15. js自执行函数写法
  16. 移动前端的html5 head 头标签
  17. js坚持不懈之16:使用js向HTML元素分配事件
  18. 如何在Linux中轻松删除源安装的软件包?
  19. Go语言里的slice
  20. Bytom BIP-32协议和BIP-44协议

热门文章

  1. Ubuntu中安装MySQL
  2. 博弈dp入门 POJ - 1678 HDU - 4597
  3. nu.random.seed()如何理解
  4. 一、基本的bash shell命令(基于Ubuntu实现)
  5. [c++] C++中public、protected、private的区别
  6. python-matplotlib-ERROR
  7. R语言:时间的转化
  8. js字符串常用函数
  9. 关于ansbile
  10. netcore kafka操作