Skip to content

refactor(server): reduce duplication in server, tls, pubsub, and main - #164

Merged
kacy merged 1 commit into
mainfrom
refactor/idiomatic-rust-pass-pr2
Feb 17, 2026
Merged

refactor(server): reduce duplication in server, tls, pubsub, and main#164
kacy merged 1 commit into
mainfrom
refactor/idiomatic-rust-pass-pr2

Conversation

@kacy

@kacy kacy commented Feb 17, 2026

Copy link
Copy Markdown
Owner

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 — extract setup_tls_listener

the 8-line TLS listener bind block (bind port, load acceptor, log) was duplicated verbatim in both run and run_concurrent. extracted into async 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 matching

glob_match was converting both pattern and input to Vec<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 — extract require_file

three near-identical blocks:

if !path.exists() {
    return Err(TlsError::CertFileNotFound(config.cert_file.clone()));
}

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 — extract exit_err

arg validation failures all followed the pattern eprintln!(...); std::process::exit(1). extracted to fn 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 throughout main.

what was tested

  • cargo build -p ember-server — clean
  • cargo test -p ember-server — 43 tests pass, 0 failures
  • cargo fmt --check -p ember-server — no formatting issues
  • manual smoke test: server starts, redis-cli PING returns PONG, PSUBSCRIBE + PUBLISH delivers to pattern subscriber

design considerations

the setup_tls_listener extraction handles the simplest clearly-duplicated code. the accept loop itself differs between run and run_concurrent in 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.

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
kacy merged commit 83e1927 into main Feb 17, 2026
7 checks passed
@kacy
kacy deleted the refactor/idiomatic-rust-pass-pr2 branch February 17, 2026 23:32
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant