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