{"id":36987948,"url":"https://github.com/tourze/workerman-rate-limit-protocol","last_synced_at":"2026-01-13T23:15:53.414Z","repository":{"id":286159972,"uuid":"960567651","full_name":"tourze/workerman-rate-limit-protocol","owner":"tourze","description":"Workerman rate limit protocol","archived":false,"fork":false,"pushed_at":"2025-10-31T07:58:05.000Z","size":31,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-31T09:31:57.491Z","etag":null,"topics":["protocol","workerman"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tourze.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-04-04T16:55:45.000Z","updated_at":"2025-10-31T07:58:09.000Z","dependencies_parsed_at":"2025-04-25T11:33:02.409Z","dependency_job_id":"61a985d4-4de5-42e8-baa5-e239ec923a8c","html_url":"https://github.com/tourze/workerman-rate-limit-protocol","commit_stats":null,"previous_names":["tourze/workerman-rate-limit-protocol"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/tourze/workerman-rate-limit-protocol","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tourze%2Fworkerman-rate-limit-protocol","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tourze%2Fworkerman-rate-limit-protocol/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tourze%2Fworkerman-rate-limit-protocol/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tourze%2Fworkerman-rate-limit-protocol/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tourze","download_url":"https://codeload.github.com/tourze/workerman-rate-limit-protocol/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tourze%2Fworkerman-rate-limit-protocol/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28405097,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["protocol","workerman"],"created_at":"2026-01-13T23:15:52.668Z","updated_at":"2026-01-13T23:15:53.406Z","avatar_url":"https://github.com/tourze.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Workerman Rate Limit Protocol\n\n[English](README.md) | [中文](README.zh-CN.md)\n\n[![Latest Version](https://img.shields.io/packagist/v/tourze/workerman-rate-limit-protocol.svg?style=flat-square)](https://packagist.org/packages/tourze/workerman-rate-limit-protocol)\n[![Total Downloads](https://img.shields.io/packagist/dt/tourze/workerman-rate-limit-protocol.svg?style=flat-square)](https://packagist.org/packages/tourze/workerman-rate-limit-protocol)\n[![PHP Version](https://img.shields.io/packagist/php-v/tourze/workerman-rate-limit-protocol.svg?style=flat-square)](https://packagist.org/packages/tourze/workerman-rate-limit-protocol)\n[![License](https://img.shields.io/packagist/l/tourze/workerman-rate-limit-protocol.svg?style=flat-square)](https://github.com/tourze/workerman-rate-limit-protocol/blob/master/LICENSE)\n\u003c!-- Add other relevant badges like build status, quality score if applicable --\u003e\n\u003c!-- [![Build Status](https://img.shields.io/travis/tourze/workerman-rate-limit-protocol/master.svg?style=flat-square)](https://travis-ci.org/tourze/workerman-rate-limit-protocol) --\u003e\n\u003c!-- [![Quality Score](https://img.shields.io/scrutinizer/g/tourze/workerman-rate-limit-protocol.svg?style=flat-square)](https://scrutinizer-ci.com/g/tourze/workerman-rate-limit-protocol) --\u003e\n\nA rate limiting protocol implementation integrated with Workerman.\n\n## Features\n\n- Provides traffic-based rate limiting protocol (bytes/second).\n- Provides packet-based rate limiting protocol (packets/second).\n- Supports setting default rate limits globally and overriding limits for specific connections.\n- Non-intrusive usage, acts as a wrapper around existing Workerman protocols (defaults to raw TCP/UDP handling if no inner protocol specified).\n- Uses `WeakMap` to store connection context for automatic memory management (no manual cleanup needed on connection close).\n- **TCP Connections**: Pauses receiving data (`pauseRecv`) instead of closing when the rate limit is exceeded. Resumes automatically after 1 second.\n- **UDP Connections**: Directly discards packets when the rate limit is exceeded.\n- **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.\n\n## Installation\n\n```bash\ncomposer require tourze/workerman-rate-limit-protocol\n```\n\n## Quick Start\n\n### Traffic-based Rate Limiting (Bytes/Second)\n\n```php\n\u003c?php\n\nuse Tourze\\Workerman\\RateLimitProtocol\\TrafficRateLimitProtocol;\nuse Workerman\\Worker;\nuse Workerman\\Connection\\TcpConnection; // Ensure TcpConnection is imported if used\n\nrequire_once __DIR__ . '/vendor/autoload.php'; // Adjust path if needed\n\n// Create a Worker listening on port 8080\n// Apply traffic-based rate limiting: Limit each connection to 1MB/s by default\n$worker = new Worker('tcp://0.0.0.0:8080');\n$worker-\u003eprotocol = TrafficRateLimitProtocol::class;\nTrafficRateLimitProtocol::setDefaultLimit(1024 * 1024); // 1MB/s\n\n$worker-\u003eonConnect = function(TcpConnection $connection) { // Type hint connection for clarity\n    echo \"New connection from {$connection-\u003egetRemoteAddress()}\\n\";\n\n    // Optional: Set a different rate limit for this specific connection\n    TrafficRateLimitProtocol::setConnectionLimit($connection, 2 * 1024 * 1024); // 2MB/s for this one\n    echo \"Set connection-specific limit to 2MB/s for {$connection-\u003eid}\\n\";\n};\n\n$worker-\u003eonMessage = function(TcpConnection $connection, $data) {\n    // The protocol handles the rate limiting check in its input/encode methods.\n    // Your application logic receives data only if it's within the limit.\n    echo \"Received message from {$connection-\u003eid}: {$data}\\n\";\n    $connection-\u003esend('Server received: ' . $data);\n};\n\n$worker-\u003eonClose = function(TcpConnection $connection) {\n    echo \"Connection closed from {$connection-\u003egetRemoteAddress()}\\n\";\n};\n\nWorker::runAll();\n\n```\n\n### Packet-based Rate Limiting (Packets/Second)\n\n```php\n\u003c?php\n\nuse Tourze\\Workerman\\RateLimitProtocol\\PacketRateLimitProtocol;\nuse Workerman\\Worker;\nuse Workerman\\Connection\\TcpConnection;\n\nrequire_once __DIR__ . '/vendor/autoload.php';\n\n// Create a Worker listening on port 8081\n// Apply packet-based rate limiting: Limit each connection to 100 packets/s by default\n$worker = new Worker('tcp://0.0.0.0:8081');\n$worker-\u003eprotocol = PacketRateLimitProtocol::class;\nPacketRateLimitProtocol::setDefaultLimit(100); // 100 packets/s\n\n$worker-\u003eonConnect = function(TcpConnection $connection) {\n    echo \"New connection on 8081 from {$connection-\u003egetRemoteAddress()}\\n\";\n\n    // Optional: Set a different packet rate limit for this connection\n    PacketRateLimitProtocol::setConnectionLimit($connection, 200); // 200 packets/s for this one\n    echo \"Set connection-specific packet limit to 200/s for {$connection-\u003eid}\\n\";\n};\n\n$worker-\u003eonMessage = function(TcpConnection $connection, $data) {\n    echo \"Received message on 8081 from {$connection-\u003eid}: {$data}\\n\";\n    $connection-\u003esend('Server received packet: ' . $data);\n};\n\n$worker-\u003eonClose = function(TcpConnection $connection) {\n    echo \"Connection closed on 8081 from {$connection-\u003egetRemoteAddress()}\\n\";\n};\n\nWorker::runAll();\n\n```\n\n## Notes\n\n- Statistics are calculated per second, and counters reset every second for each connection independently.\n- The core limiting logic happens within the `input` and `encode` methods of the protocol classes.\n\n## Contributing\n\nPlease refer to the main project contribution guidelines if available. Issues and Pull Requests are welcome.\n\n## License\n\nThe MIT License (MIT). Please see the [LICENSE](LICENSE) file for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftourze%2Fworkerman-rate-limit-protocol","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftourze%2Fworkerman-rate-limit-protocol","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftourze%2Fworkerman-rate-limit-protocol/lists"}