承接 tal7aouy/api-rate-limiter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tal7aouy/api-rate-limiter

最新稳定版本:v1.0.0

Composer 安装命令:

composer require tal7aouy/api-rate-limiter

包简介

API Rate Limiting Library for PHP

README 文档

README

This library provides rate limiting functionality for PHP applications, using Redis for storage. It supports multiple rate limiting strategies and can be integrated with PSR-15 middleware.

Installation

To use the library, you need to have the following Composer packages, which are already included in the library's composer.json file:

  1. predis/predis - A Redis client library.
  2. laminas/laminas-diactoros - Provides PSR-7 (HTTP message) and PSR-15 (HTTP server) implementations.
  3. psr/http-server-handler - Provides the RequestHandlerInterface for handling HTTP requests.
  4. psr/http-server-middleware - Provides the MiddlewareInterface for HTTP middleware.
  5. laminas/laminas-httphandlerrunner - Provides the SapiEmitter class for emitting HTTP responses.

In addition, for development and testing, the library includes:

  1. phpstan/phpstan - For static analysis.
  2. pestphp/pest - For testing.

Install Library and Dependencies

Run the following Composer command to install all required packages:

composer require tal7aouy/api-rate-limiter

Ensure Redis is installed and running on your system.

Usage

Basic Setup

  1. Initialize Redis Connection:

    Create a RedisConnection instance to handle the connection to the Redis server.

    use Tal7aouy\ApiRateLimiter\RedisConnection;
    
    $redisConnection = new RedisConnection();
  2. Initialize Redis Rate Limiter:

    Create a RedisRateLimiter instance using the Redis connection.

    use Tal7aouy\ApiRateLimiter\RedisRateLimiter;
    
    $redisRateLimiter = new RedisRateLimiter($redisConnection->getClient());
  3. Initialize Rate Limiter Middleware:

    Create a RateLimiterMiddleware instance with the desired rate limit and time window.

    use Tal7aouy\ApiRateLimiter\RateLimiterMiddleware;
    
    $rateLimiterMiddleware = new RateLimiterMiddleware($redisRateLimiter, 10, 60); // Limit: 10 requests per minute
  4. Create a Request Handler:

    Implement a simple request handler to handle incoming requests.

    use Laminas\Diactoros\Response;
    use Psr\Http\Server\RequestHandlerInterface;
    use Psr\Http\Message\ServerRequestInterface;
    use Psr\Http\Message\ResponseInterface;
    
    class SimpleHandler implements RequestHandlerInterface {
        public function handle(ServerRequestInterface $request): ResponseInterface {
            $response = new Response();
            $response->getBody()->write('Hello, World!');
            return $response;
        }
    }
  5. Create Server Request and Process Through Middleware:

    Create a server request and process it through the rate limiter middleware.

    use Laminas\Diactoros\ServerRequestFactory;
    
    $serverParams = ['REMOTE_ADDR' => '127.0.0.1'];
    $request = ServerRequestFactory::fromGlobals($serverParams, [], [], [], []);
    
    $response = $rateLimiterMiddleware->process($request, new SimpleHandler());
    
    // Emit Response
    use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
    $emitter = new SapiEmitter();
    $emitter->emit($response);

Advanced Configuration

Custom Redis Configuration

You can configure the Redis connection by passing a custom configuration array to the RedisConnection class.

$redisConnection = new RedisConnection([
    'scheme' => 'tcp',
    'host'   => 'localhost',
    'port'   => 6379,
]);

Custom Rate Limiting Strategies

The library supports multiple rate limiting strategies. You can implement your own strategy by extending the RateLimiter interface.

use Tal7aouy\ApiRateLimiter\RateLimiter;

class CustomRateLimiter implements RateLimiter {
    public function isAllowed(string $identifier, int $limit, int $window): bool {
        // Custom rate limiting logic
    }
}

Then use the custom rate limiter with the middleware.

$customRateLimiter = new CustomRateLimiter();
$rateLimiterMiddleware = new RateLimiterMiddleware($customRateLimiter, 10, 60);

Testing

Unit tests are provided for each rate limiting strategy and Redis integration. To run the tests, use PestPHP.

composer test

Documentation

For detailed documentation and examples, please refer to the source code and comments within the library.

In the examples/index.php file, you can see a complete example of how to use the library.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

License

This software licensed under the MIT license.

tal7aouy/api-rate-limiter 适用场景与选型建议

tal7aouy/api-rate-limiter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tal7aouy/api-rate-limiter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tal7aouy/api-rate-limiter 我们能提供哪些服务?
定制开发 / 二次开发

基于 tal7aouy/api-rate-limiter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-30