定制 mehr-it/lara-token-bucket 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mehr-it/lara-token-bucket

Composer 安装命令:

composer require mehr-it/lara-token-bucket

包简介

Implements the token bucket algorithm using laravel cache

README 文档

README

Implements the "Token bucket" algorithm using laravel's cache repositories.

Note: This implementation uses a clock resolution of one second. This means, new tokens are added every second at most.

Install

composer require mehr-it/lara-token-bucket

This package uses Laravel's package auto-discovery, so the service provider will be loaded automatically.

Usage

Use the TokenBucket facade to access a token bucket:

// Create a token bucket with rate of 5 tokens per second
// and burst size of 20 tokens.
$bucket = \TokenBucket::bucket('myBucket', 5.0, 20);

// try to take 2 tokens from the bucket
$success = $bucket->tryTake(2);

All token bucket instances with the same name use the same token store, as long as the use the same underlying cache.

Estimate time until tokens are available

Sometimes, it might be helpful to estimate the duration until new tokens become available. The tryTake() method returns the estimated time to the second parameter if given. If you just want to check the estimated availability without taking tokens out, the estimateAvailability() method is what you are looking for.

// $secUntilAvailableNext is filled with the duration until 
// another 2 tokens are available after an eventually
// successful taking
$bucket->tryTake(2, $secUntilAvailableNext);

// returns the duration until 2 tokens are available
$secUntilAvailableNext = $bucket->estimateAvailability(3);

Putting tokens back

Sometimes, you might have taken a token which you didn't need. You can give tokens back to buckets without affecting the time-based filling:

// give back 2 tokens
$bucket->putTokens(2);

Predefined buckets

If you want to predefine token buckets and use them later by resolving the bucket name, the registerBucket() method is what you need:

// register
\TokenBucket::registerBucket('myBucket', 5.0, 20);

// resolve
$bucket = \TokenBucket::resolveBucket('myBucket');

Edge cases

By default, buckets are empty when no data exists. This happens the first time a bucket is requested or after a cache flush.

You may define the initial number of tokens a bucket holds, when creating a bucket instance:

// create a token bucket with initial token number of 3
$bucket = \TokenBucket::bucket('myBucket', 5.0, 20, 3);

统计信息

  • 总下载量: 73
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固