如何实现Perl Web服务的服务器端速率限制? [英] How can I implement server-side rate limiting for a Perl web service?

查看:75
本文介绍了如何实现Perl Web服务的服务器端速率限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Perl的CGI / Fast CGI Web服务,并希望通过IP地址对客户端进行速率限制,以阻止激进的客户端引起太多的工作。

I have a Perl-based CGI/Fast CGI web service and want to rate-limit clients by IP address to stop aggressive clients causing too much work.

已经查看了一些代码,并在CPAN中找到了 Algorithm :: TokenBucket ,但这仅用于客户要求;它没有持久性,也没有按用户配置,因此对于服务器端速率限制不是真正有用。

I have looked around for some code and found Algorithm::TokenBucket in CPAN but that is for client requests; it has no persistence and has no per-user config so is not really useful for server-side rate limiting.

我正在寻找一些已经存在的建议,否则我需要基于一些简单的持久性(例如,与-a绑定到 DB_File ) IP地址和一些执行令牌管理的批处理作业。

I am looking for suggestions for something that already exists, otherwise I'll need to roll my own based on some simple persistence such as tie to DB_File per-IP address and some batch job that does the token management.

推荐答案

我已经使用过 Cache :: FastMmap 通过跟踪每个IP地址的点击来进行速率限制。这是一个缓存,因此数据会随着时间的推移而过期,但是如果您设置大小和正确的过期时间,这应该不是问题。

I've used Cache::FastMmap for rate-limiting by tracking hits per IP address. It's a cache so data will expire over time, but if you set the size and expire time right, this shouldn't be an issue.

IP地址是哈希值键,哈希值是一个时间戳数组。我有第二个数据结构(也由 Cache :: FastMMap 支持)禁止的IP地址的哈希值,根据第一个结构中的数据进行更新。

The IP address is the hash key and the hash value is an array of timestamps. I have a second data structure (also backed by Cache::FastMMap) which is a hash of banned IP addresses, updated according to the data from the first structure.

这篇关于如何实现Perl Web服务的服务器端速率限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆