jeroenvisser101 / leakybucket
Leaky Bucket 算法的实现。
0.1.5
2017-05-03 10:21 UTC
Requires
- php: >=5.4.0
- predis/predis: >=0.8.0
Requires (Dev)
- phpunit/phpunit: 4.1.*
This package is not auto-updated.
Last update: 2024-09-14 16:18:49 UTC
README
Leaky Bucket 是一种算法,其工作原理如下
- 有一个桶。
- 该桶具有定义的泄漏速率和容量。
- 桶以恒定速率泄漏。
- 当桶满时,将不会向桶中添加其他水滴。
用法
基本用法
<?php use LeakyBucket\LeakyBucket; use LeakyBucket\Storage\RedisStorage; // Define which storage to use $storage = new RedisStorage(); // Define the bucket $settings = [ 'capacity' => 10, 'leak' => 1 ]; // Create the bucket $bucket = new LeakyBucket('example-bucket', $storage, $settings); // Fill the bucket $bucket->fill(); // Check if it's full if ($bucket->isFull()) { header('HTTP/1.1 429 Too Many Requests'); exit '<!doctype html><html><body><h1>429 Too Many Requests</h1><p>You seem to be doing a lot of requests. You\'re now cooling down.</p></body></html>'; } // ...
其他功能
您也可以通过 LeakyBucket 提供的方法进行许多其他操作。
// Get capacity information $capacityTotal = $bucket->getCapacity(); $capacityLeft = $bucket->getCapacityLeft(); $capacityUsed = $bucket->getCapacityUsed(); // Get the drops/second that the bucket leaks $leakPerSecond = $bucket->getLeak(); // Get the last timestamp from when the bucket was updated $timestamp = $bucket->getLastTimestamp(); // Set additional data $bucket->setData( [ 'timeout' => 3600 ] ); // Get additional data $data = $bucket->getData(); // Update the bucket with the leaked drops $bucket->leak(); // Remove excess drops $bucket->overflow(); // Update the bucket's timestamp manually $bucket->touch(); // Fill the bucket with one drop $bucket->fill(); // Fill the bucket with 5 drops $bucket->fill(5); // Spill one drop from the bucket $bucket->spill(); // Spill 5 drops from the bucket $bucket->spill(5); // Remove the bucket's content $bucket->reset(); // Force save $bucket->save();
贡献
您可以通过在仓库上进行分支并创建 pull request 来贡献。您还可以创建问题或功能请求。
清单
- 您的代码符合 PSR-2 标准。(使用 php-cs-checker 检查)
- 您的代码经过完全测试,并提供了 PHPUnit 测试。
- 您的代码在 TravisCI 构建中通过。
- 通过贡献您的代码,您同意在 MIT 许可证下许可您的贡献。
无聊的法律事务
本项目采用 MIT 许可证。可以在本存储库中找到 LICENSE
文件。