定制 twinh/rate-limiter 二次开发

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

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

twinh/rate-limiter

最新稳定版本:v1.0.0

Composer 安装命令:

composer require twinh/rate-limiter

包简介

A flexible and scalable Rate Limiter, supporting various common strategies and storage methods.

README 文档

README

GitHub Workflow Status Coverage Status License

A flexible and scalable Rate Limiter, supporting various common strategies and storage methods.

Getting started

use RateLimiter\FixedWindow;
use RateLimiter\FixedWindow\MemoryStorage;

$limiter = new FixedWindow(new MemoryStorage(), windowSize: 10, limit: 1);
if (!$limiter->attempt('user_id')) {
    echo 'Too many requests';
}

Feature

  • ✨ Supports multiple strategies: FixedWindow, SlidingWindow, LeakyBucket, and TokenBucket (not yet).
  • 🖥️ Compatible with Redis, Memory, and Database (not yet) storage methods.
  • 💪 Robust and scalable architecture with atomic operations for high traffic and large user bases.
  • ⚙️ Flexible rate limit and window size configuration.
  • 🚪 Clear API for attempting, getting remaining attempts, and clearing rate limits.
  • 🌐 Supports global, and user-specific rate limits.

Install

composer require twinh/rate-limiter

Document

Initialize the FixedWindow Rate Limiter

use RateLimiter\FixedWindow;
use RateLimiter\FixedWindow\MemoryStorage;

$limiter = new FixedWindow(new MemoryStorage(), windowSize: 60, limit: 1);

Initialize the SlidingWindow Rate Limiter

use RateLimiter\SlidingWindow;
use RateLimiter\SlidingWindow\MemoryStorage;

$limiter = new SlidingWindow(new MemoryStorage(), windowSize: 60, limit: 1);

Initialize the LeakyBucket Rate Limiter

use RateLimiter\LeakyBucket;
use RateLimiter\LeakyBucket\MemoryStorage;

$limiter = new LeakyBucket(new LeakyMemoryStorage(), rate: 10, limit: 1);

Set Redis Storage

use RateLimiter\FixedWindow;
use RateLimiter\FixedWindow\RedisStorage;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$limiter = new FixedWindow(new RedisStorage($redis), windowSize: 10, limit: 1);

Attempts to execute a rate limited operation

if (!$limiter->attempt('user_id')) {
    echo 'Too many requests';
}

Attempts to execute a rate limited operation with global scope

$limiter->attempt();

Attempts to execute a rate limited operation with client IP

$limiter->attempt($_SERVER['REMOTE_ADDR'] ?? 'unknown');

Attempts to execute a rate limited operation with server IP

$limiter->attempt($_SERVER['SERVER_ADDR'] ?? 'unknown');

Get the remaining attempts

$remaining = $limiter->getRemaining('user_id');

Clear the request limit

$limiter->clear('user_id');

Extend more storages and rate limiters

Add Storage

  1. Create a new class that implements the RateLimiter\Xxx\StorageInterface interface of the rate limiter.
  2. Implement the methods of the storage class.
  3. Create the rate limiter object with the new storage object.

Add Rate Limiter

  1. Create a new class that implements the RateLimiter\RateLimiterInterface.
  2. Implement the methods of the rate limiter class.
  3. Create a StorageInterface for the rate limiter class.
  4. Implement storage object.

Class Diagram

Class Diagram

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-10-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固