refactor(server): reduce duplication in server, tls, pubsub, and main - #164
Merged
Conversation
server.rs
- extract `setup_tls_listener` to replace the 8-line TLS bind block
that was copy-pasted verbatim into both `run` and `run_concurrent`
pubsub.rs
- replace `Vec<char>` with byte-slice iteration in `glob_match_inner`
and `match_char_class`. all metacharacters are ASCII so byte comparison
is correct for Redis-compatible pub/sub patterns. eliminates two heap
allocations per pattern match on the hot pub/sub path.
- add doc comments explaining the backtracking algorithm
tls.rs
- extract `require_file(path, make_err)` to replace three near-identical
"if !path.exists() { return Err(...) }" blocks
main.rs
- extract `exit_err(msg) -> !` to normalize the eprintln+exit(1) pattern
used across all argument validation failures. call sites become one-liners.
kacy
added a commit
that referenced
this pull request
Feb 19, 2026
…#164) server.rs - extract `setup_tls_listener` to replace the 8-line TLS bind block that was copy-pasted verbatim into both `run` and `run_concurrent` pubsub.rs - replace `Vec<char>` with byte-slice iteration in `glob_match_inner` and `match_char_class`. all metacharacters are ASCII so byte comparison is correct for Redis-compatible pub/sub patterns. eliminates two heap allocations per pattern match on the hot pub/sub path. - add doc comments explaining the backtracking algorithm tls.rs - extract `require_file(path, make_err)` to replace three near-identical "if !path.exists() { return Err(...) }" blocks main.rs - extract `exit_err(msg) -> !` to normalize the eprintln+exit(1) pattern used across all argument validation failures. call sites become one-liners.
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
four focused changes across ember-server, targeting repeated patterns that would need to be updated in sync if the underlying logic ever changes.
server.rs— extractsetup_tls_listenerthe 8-line TLS listener bind block (bind port, load acceptor, log) was duplicated verbatim in both
runandrun_concurrent. extracted intoasync fn setup_tls_listener(tls: Option<(SocketAddr, TlsConfig)>) -> Result<...>. both run functions now call it in one line. any future change (e.g. adding SNI, changing the log message) only needs to happen in one place.pubsub.rs— byte-slice glob matchingglob_matchwas converting both pattern and input toVec<char>on every call. under heavy pub/sub load (many pattern subscribers, high publish rate) this was allocating and GC-ing two vecs per match. since all pattern metacharacters (*,?,[,\) are ASCII, byte-slice comparison is equivalent and avoids both allocations entirely. the backtracking algorithm is unchanged. added doc comments explaining the algorithm.tls.rs— extractrequire_filethree near-identical blocks:
extracted to
fn require_file(path, make_err). each call site is now a one-liner. adding a fourth file check in the future is a single line.main.rs— extractexit_errarg validation failures all followed the pattern
eprintln!(...); std::process::exit(1). extracted tofn exit_err(msg: impl Display) -> !. this makes the!return explicit (the compiler can verify call sites don't need to produce a value) and reduces the visual noise of exit handling throughoutmain.what was tested
cargo build -p ember-server— cleancargo test -p ember-server— 43 tests pass, 0 failurescargo fmt --check -p ember-server— no formatting issuesdesign considerations
the
setup_tls_listenerextraction handles the simplest clearly-duplicated code. the accept loop itself differs betweenrunandrun_concurrentin the spawned task body (different handler function, different captured state) — extracting that would require boxing futures or trait objects, which adds complexity. left as a follow-up if warranted.