feat: cap concurrent connections with semaphore

Add max_connections config (default 256) with -m/--max-connections CLI
flag. Server wraps on_client in asyncio.Semaphore to prevent fd
exhaustion under load. Value reloads on SIGHUP; active connections
drain normally. Also adds pool_size/pool_max_idle config fields and
first_hop_pool wiring in server.py (used by next commits), and fixes
asyncio.TimeoutError -> TimeoutError lint warnings.
This commit is contained in:
user
2026-02-15 17:55:50 +01:00
parent 076213a830
commit 714e8efb3d
5 changed files with 61 additions and 7 deletions

View File

@@ -2,7 +2,7 @@
import pytest
from s5p.config import ChainHop, Config, parse_proxy_url
from s5p.config import ChainHop, Config, load_config, parse_proxy_url
class TestParseProxyUrl:
@@ -74,3 +74,19 @@ class TestConfig:
assert c.listen_port == 1080
assert c.chain == []
assert c.timeout == 10.0
assert c.max_connections == 256
assert c.pool_size == 0
assert c.pool_max_idle == 30.0
def test_max_connections_from_yaml(self, tmp_path):
cfg_file = tmp_path / "test.yaml"
cfg_file.write_text("max_connections: 512\n")
c = load_config(cfg_file)
assert c.max_connections == 512
def test_pool_size_from_yaml(self, tmp_path):
cfg_file = tmp_path / "test.yaml"
cfg_file.write_text("pool_size: 16\npool_max_idle: 45.0\n")
c = load_config(cfg_file)
assert c.pool_size == 16
assert c.pool_max_idle == 45.0