davedevelopment/stiphle
Composer 安装命令:
composer require davedevelopment/stiphle
包简介
Simple rate limiting/throttling for php
README 文档
README
Install via Composer
composer require davedevelopment/stiphle
What is it?
Stiphle is a little library to try and provide an easy way of throttling/rate limit requests, for those without fancy hardware etc.
How does it work?
You create a throttle, and ask it how long you should wait. For example, given that $identifier is some means of identifying whatever it is you're throttling, and you want to throttle it to 5 requests per second:
<?php $throttle = new Stiphle\Throttle\LeakyBucket; $identifier = 'dave'; while(true) { // the throttle method returns the amount of milliseconds it slept for echo $throttle->throttle($identifier, 5, 1000); } # 0 0 0 0 0 200 200....
Use combinations of values to provide bursting etc, though use carefully as it screws with your mind
<?php $throttle = new Stiphle\Throttle\LeakyBucket; $identifier = 'dave'; for(;;) { /** * Allow upto 5 per second, but limit to 20 a minute - I think **/ echo "a:" . $throttle->throttle($identifier, 5, 1000); echo " b:" . $throttle->throttle($identifier, 20, 60000); echo "\n"; } #a:0 b:0 #a:0 b:0 #a:0 b:0 #a:0 b:0 #a:0 b:0 #a:199 b:0 #a:200 b:0 #a:199 b:0 #a:200 b:0 #a:200 b:0 #a:199 b:0 #a:200 b:0 #a:199 b:0 #a:200 b:0 #a:200 b:0 #a:199 b:0 #a:200 b:0 #a:200 b:0 #a:199 b:0 #a:200 b:0 #a:199 b:0 #a:200 b:2600 #a:0 b:3000 #a:0 b:2999
Throttle Strategies
There are currently two types of throttles, Leaky Bucket and a simple fixed time window.
/** * Throttle to 1000 per *rolling* 24 hours, e.g. the counter will not reset at * midnight */ $throttle = new Stiphle\Throttle\LeakyBucket; $throttle->throttle('api.request', 1000, 86400000); /** * Throttle to 1000 per calendar day, counter will reset at midnight */ $throttle = new Stiphle\Throttle\TimeWindow; $throttle->throttle('api.request', 1000, 86400000);
NB: The current implementation of the TimeWindow throttle will only work on 64-bit architectures!
Storage
Stiphle currently ships with 5 storage engines
- In process
- APC
- Memcached
- Doctrine Cache
- Redis
Stiphle uses the in process storage by default. A different storage engine can be injected after object creation.
$throttle = new Stiphle\Throttle\LeakyBucket(); $storage = new \Stiphle\Storage\Memcached(new \Memcached()); $throttle->setStorage($storage);
Todo
- More Tests!
- Decent Unit tests
- More throttling methods
- More storage adapters, the current ones are a little volatile, Mongo, Cassandra, MemcacheDB etc
Copyright
Copyright (c) 2011 Dave Marshall. See LICENCE for further details
davedevelopment/stiphle 适用场景与选型建议
davedevelopment/stiphle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.25M 次下载、GitHub Stars 达 253, 最近一次更新时间为 2012 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「throttle」 「throttling」 「rate limiting」 「rate limit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 davedevelopment/stiphle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 davedevelopment/stiphle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 davedevelopment/stiphle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A throttler for symfony/http-client to handle rate limits
Simple PHP execution throttler.
A Laravel Pulse card to track users & URL's that are being throttled by your Rate Limiters
A/B Testing of content elements
Throttle notifications on a per-channel basis
hyperf cache counter rate limiter
统计信息
- 总下载量: 8.25M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 256
- 点击次数: 33
- 依赖项目数: 12
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2012-02-24