Add --reuse-port option to the server for horizontal scaling - #4
Merged
Conversation
The server socket now always sets SO_REUSEADDR explicitly (asyncio already enables it on Unix by default) and can optionally set SO_REUSEPORT via the new --reuse-port CLI option, also configurable as reuse_port in the YAML config. With SO_REUSEPORT multiple server instances can listen on the same address and the kernel load-balances connections across them, letting the otherwise single-threaded server use more CPU cores (e.g. in Docker with --network=host). Server creation was extracted into create_server() to make it testable. Tests cover both the configuration plumbing and the actual two-instance binding behavior, and the scaling approach is documented in the README. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a --reuse-port / reuse_port: true option to enable SO_REUSEPORT so multiple server processes can bind the same host:port and let the kernel distribute incoming connections, plus tests and documentation for the behavior.
Changes:
- Added
--reuse-portCLI flag andreuse_portYAML config plumbing. - Refactored server startup into
create_server(conf)and extended socket bind options. - Added smoke/config/subprocess tests and documented horizontal scaling in
README.md.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| server/logline_server/main.py | Adds --reuse-port flag and refactors server creation/binding behavior. |
| server/logline_server/configuration.py | Adds reuse_port config parsing from CLI/YAML. |
| server/tests/test_configuration.py | Unit tests for reuse_port config behavior. |
| server/tests/test_reuse_port_subprocess.py | Subprocess behavioral tests for binding with/without --reuse-port. |
| server/tests/test_smoke.py | Smoke test ensuring python -m logline_server --help works. |
| README.md | Documents how to scale the server using --reuse-port. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The reuse-port subprocess tests now react to the server's "Listening on ..." log line (via a ServerProcess wrapper that drains stderr in a background thread) rather than waiting a fixed amount of time to infer success. This makes the positive test assert directly that both instances bound, turns the checks event-driven, and cuts the suite runtime from ~2.2s to ~0.2s. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Passing reuse_address=True unconditionally also enabled SO_REUSEADDR on Windows, where it has different semantics and would let another socket hijack an address that is already actively in use. asyncio already enables SO_REUSEADDR by default only on POSIX (not on Windows/Cygwin), so dropping the explicit flag keeps the safe behavior on every platform. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Author
|
Toto asi pak bude lepší squashnout, mít commity jednotlivě nepřidá do projektou žádnou hodnotu navíc. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for running multiple server instances behind the same address for horizontal scaling.
--reuse-portCLI option (also configurable asreuse_port: truein the YAML config) setsSO_REUSEPORT. With it, multiple server instances can listen on the samehost:portand the kernel load-balances incoming connections across them — useful since a single server instance is single-threaded. In Docker, run the instances with--network=host.--reuse-portis requested on a platform withoutSO_REUSEPORT, the server fails fast with a clearConfigurationErrormessage (consistent with the other configuration validation errors) instead of an opaque socket error.SO_REUSEADDRis left to asyncio's per-platform default (enabled on POSIX, not on Windows/Cygwin), so the socket behavior stays safe on every platform.async_mainintocreate_server(conf)to make the binding behavior directly testable.SO_REUSEPORTsame-effective-user requirement.Tests
test_configuration.py— unit tests for thereuse_portconfig plumbing (CLI and YAML).test_reuse_port_subprocess.py— behavioral tests that start the server as a subprocess: two instances bind together with--reuse-port, and a second instance fails to bind without it (skipped on platforms withoutSO_REUSEPORT). Readiness is detected from the server'sListening on ...log line (event-driven), so the tests don't rely on fixed sleeps.test_smoke.py— added a--helpsmoke test for the server.All 19 server tests pass locally (~0.2s).
🤖 Generated with Claude Code