https://github.com/tourze/workerman-rate-limit-protocol
Workerman rate limit protocol
https://github.com/tourze/workerman-rate-limit-protocol
protocol workerman
Last synced: about 1 month ago
JSON representation
Workerman rate limit protocol
- Host: GitHub
- URL: https://github.com/tourze/workerman-rate-limit-protocol
- Owner: tourze
- License: mit
- Created: 2025-04-04T16:55:45.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2025-10-31T07:58:05.000Z (4 months ago)
- Last Synced: 2025-10-31T09:31:57.491Z (4 months ago)
- Topics: protocol, workerman
- Language: PHP
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Workerman Rate Limit Protocol
[English](README.md) | [中文](README.zh-CN.md)
[](https://packagist.org/packages/tourze/workerman-rate-limit-protocol)
[](https://packagist.org/packages/tourze/workerman-rate-limit-protocol)
[](https://packagist.org/packages/tourze/workerman-rate-limit-protocol)
[](https://github.com/tourze/workerman-rate-limit-protocol/blob/master/LICENSE)
A rate limiting protocol implementation integrated with Workerman.
## Features
- Provides traffic-based rate limiting protocol (bytes/second).
- Provides packet-based rate limiting protocol (packets/second).
- Supports setting default rate limits globally and overriding limits for specific connections.
- Non-intrusive usage, acts as a wrapper around existing Workerman protocols (defaults to raw TCP/UDP handling if no inner protocol specified).
- Uses `WeakMap` to store connection context for automatic memory management (no manual cleanup needed on connection close).
- **TCP Connections**: Pauses receiving data (`pauseRecv`) instead of closing when the rate limit is exceeded. Resumes automatically after 1 second.
- **UDP Connections**: Directly discards packets when the rate limit is exceeded.
- **Important Note**: Rate limiting is currently implemented **per-process**. In a multi-process Workerman setup, the limit applies independently to each worker process, not globally across all processes.
## Installation
```bash
composer require tourze/workerman-rate-limit-protocol
```
## Quick Start
### Traffic-based Rate Limiting (Bytes/Second)
```php
protocol = TrafficRateLimitProtocol::class;
TrafficRateLimitProtocol::setDefaultLimit(1024 * 1024); // 1MB/s
$worker->onConnect = function(TcpConnection $connection) { // Type hint connection for clarity
echo "New connection from {$connection->getRemoteAddress()}\n";
// Optional: Set a different rate limit for this specific connection
TrafficRateLimitProtocol::setConnectionLimit($connection, 2 * 1024 * 1024); // 2MB/s for this one
echo "Set connection-specific limit to 2MB/s for {$connection->id}\n";
};
$worker->onMessage = function(TcpConnection $connection, $data) {
// The protocol handles the rate limiting check in its input/encode methods.
// Your application logic receives data only if it's within the limit.
echo "Received message from {$connection->id}: {$data}\n";
$connection->send('Server received: ' . $data);
};
$worker->onClose = function(TcpConnection $connection) {
echo "Connection closed from {$connection->getRemoteAddress()}\n";
};
Worker::runAll();
```
### Packet-based Rate Limiting (Packets/Second)
```php
protocol = PacketRateLimitProtocol::class;
PacketRateLimitProtocol::setDefaultLimit(100); // 100 packets/s
$worker->onConnect = function(TcpConnection $connection) {
echo "New connection on 8081 from {$connection->getRemoteAddress()}\n";
// Optional: Set a different packet rate limit for this connection
PacketRateLimitProtocol::setConnectionLimit($connection, 200); // 200 packets/s for this one
echo "Set connection-specific packet limit to 200/s for {$connection->id}\n";
};
$worker->onMessage = function(TcpConnection $connection, $data) {
echo "Received message on 8081 from {$connection->id}: {$data}\n";
$connection->send('Server received packet: ' . $data);
};
$worker->onClose = function(TcpConnection $connection) {
echo "Connection closed on 8081 from {$connection->getRemoteAddress()}\n";
};
Worker::runAll();
```
## Notes
- Statistics are calculated per second, and counters reset every second for each connection independently.
- The core limiting logic happens within the `input` and `encode` methods of the protocol classes.
## Contributing
Please refer to the main project contribution guidelines if available. Issues and Pull Requests are welcome.
## License
The MIT License (MIT). Please see the [LICENSE](LICENSE) file for more information.