fustundag/tokenbucket
最新稳定版本:1.0.0
Composer 安装命令:
composer require fustundag/tokenbucket
包简介
PHP TokenBucket implementation for rate limit
README 文档
README
TokenBucket is an algorithm for rate limit
You can check algorithm from http://en.wikipedia.org/wiki/Token_bucket
Usage
Basic usage
<?php use TokenBucket\TokenBucket; use TokenBucket\Storage\Memcached as MemcachedStorage; $storage = new MemcachedStorage(); // Define the bucket $options = array( 'capacity' => 20, 'fillRate' => 5 ); // Create the bucket $bucket = new TokenBucket('key-for-bucket', $storage, $options); // Check if token is avaible if ($bucket->consume()===false) { //Not allowed!! exit; } // ...
Options
capacity : Max token count of bucket.
fillRate : Token count to fill per second. For example if capacity is 20 and fillRate is 5, 5 tokens will added to bucket every second. But, total token cannot be exceed 20.
ttl : Time to live for bucket in seconds. If not given or zero given, it will be calculated automatically according to capacity and fillRate. ttl can be used to reset bucket with capacity. For example: if capacity is 100 and fillRate is zero and ttl is 300, 100 token can be consumed at 300 seconds. After 300 seconds, bucket will be reset to capacity.
Rate Limit Http Headers
You can get http headers for rate limit with getRateLimitHttpHeaders method:
X-RateLimit-Limit : gives capacity of bucket
X-RateLimit-Remaining : allowed token count of bucket
X-RateLimit-Reset : time to live of bucket
Contributing
You can contribute by forking the repo and creating pull requests. You can also create issues or feature requests.
License
This project is licensed under the MIT license. LICENSE file can be found in this repository.
统计信息
- 总下载量: 659.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-12