jeroenvisser101/leakybucket
Composer 安装命令:
composer require jeroenvisser101/leakybucket
包简介
An implementation of the Leaky Bucket algorithm.
README 文档
README
Leaky Bucket is an algorithm which works as follows:
- There is a bucket.
- The bucket has a defined leak and defined capacity.
- The bucket leaks at a constant rate.
- Overflows when full, will not add other drops to the bucket.
Usage
Basic usage
<?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>'; } // ...
Other functionality
You can also do great stuff with it through the methods that LeakyBucket provides.
// 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();
Contributing
You can contribute by forking the repo and creating pull requests. You can also create issues or feature requests.
Checklist
- Your code complies with the PSR-2 standards. (check using the php-cs-checker)
- Your code is fully tested and PHPUnit tests are also supplied.
- Your code passes in TravisCI build.
- By contributing your code, you agree to license your contribution under the MIT license.
Boring legal stuff
This project is licensed under the MIT license. LICENSE file can be found in this repository.
jeroenvisser101/leakybucket 适用场景与选型建议
jeroenvisser101/leakybucket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 117.69k 次下载、GitHub Stars 达 44, 最近一次更新时间为 2014 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「firewall」 「throttling」 「limits」 「leakybucket」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jeroenvisser101/leakybucket 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jeroenvisser101/leakybucket 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jeroenvisser101/leakybucket 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A throttler for symfony/http-client to handle rate limits
Simple PHP execution throttler.
PHP Basic Firewall
A laravel package to manage and limit usages/seats by plan, users, or other models
Throttle notifications on a per-channel basis
PHP rate limiting library with Token Bucket and Leaky Bucket Algorithms, based on palepurple/rate-limit, grandson to touhonoob/rate-limit, and jeroenvisser101/LeakyBucket
统计信息
- 总下载量: 117.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 45
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-07-06