Skip to content

refactor(worker): split worker.rs into focused modules#1096

Closed
slin1237 wants to merge 1 commit intomainfrom
refactor/split-worker-rs
Closed

refactor(worker): split worker.rs into focused modules#1096
slin1237 wants to merge 1 commit intomainfrom
refactor/split-worker-rs

Conversation

@slin1237
Copy link
Copy Markdown
Member

@slin1237 slin1237 commented Apr 11, 2026

Description

Problem

worker.rs is a 1,834-line monolith containing the Worker trait (40+ methods), BasicWorker impl, HealthChecker, WorkerMetadata, WorkerRoutingKeyLoad, WorkerLoadGuard, AttachedBody, constants, and 850 lines of tests — all in a single file with a #[expect(clippy::module_inception)] suppression.

Solution

Split into five focused modules with clear responsibilities. Pure code extraction — no behavioral changes.

Changes

New file Lines Contents
traits.rs 357 Worker trait, ConnectionModeExt, WorkerTypeExt, worker_to_info(), re-exports
basic.rs 1220 BasicWorker struct/impl, constants, WORKER_CLIENT static, all tests
metadata.rs 138 WorkerMetadata, WorkerRoutingKeyLoad
body.rs 94 WorkerLoadGuard, AttachedBody
health_checker.rs 51 HealthChecker handle (pub(crate))

Deleted: worker.rs (1,834 lines), removed #[expect(clippy::module_inception)]

Updated imports in 10 files: worker::worker::X → direct re-exports or worker::traits::X / worker::basic::X / worker::metadata::X

Minimal visibility change: WorkerRoutingKeyLoad.active_routing_keys widened from private to pub(crate) so tests in basic.rs can access it (no public API change).

Test Plan

  • cargo check --package smg — clean, no warnings
  • cargo test --package smg --lib worker:: — all 133 tests pass
  • cargo test --package smg --test api_tests — all 100 integration tests pass
  • No behavioral changes — pure code extraction

Refs: worker-module-deep-refactor plan (PR 3)

Checklist
  • Documentation updated (module doc comments)
  • (Optional) Please join us on Slack #sig-smg to discuss, review, and merge PRs

Summary by CodeRabbit

  • Refactor
    • Reorganized worker subsystem for clearer separation of metadata, health monitoring, load tracking, and request handling.
    • Introduced more robust background health-check handling and load-guarding tied to response lifecycles to improve stability and resource accounting.
    • Added routing-key-specific load tracking and safer request/body lifecycle management to reduce resource leaks and improve streaming reliability.

@github-actions github-actions bot added model-gateway Model gateway crate changes realtime-api Realtime API related changes openai OpenAI router changes labels Apr 11, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 11, 2026

📝 Walkthrough

Walkthrough

A refactor splits the monolithic worker module into focused submodules (traits, body, metadata, health_checker, basic), moves many worker-related types and helpers into those modules, and updates import paths across the codebase. No runtime logic or public APIs were functionally changed.

Changes

Cohort / File(s) Summary
Core Module Reorganization
model_gateway/src/worker/mod.rs, model_gateway/src/worker/traits.rs, model_gateway/src/worker/body.rs, model_gateway/src/worker/metadata.rs, model_gateway/src/worker/health_checker.rs
Split the prior worker module into submodules. Added traits (Worker trait, worker_to_info, protocol re-exports), body (WorkerLoadGuard, AttachedBody), metadata (WorkerMetadata, WorkerRoutingKeyLoad), and health_checker (HealthChecker). Updated re-exports accordingly.
Worker Basic Module
model_gateway/src/worker/basic.rs, model_gateway/src/worker/builder.rs
Removed in-file definitions now relocated to new submodules; basic.rs now imports those types. builder.rs imports adjusted to use basic, metadata, and traits.
Registry & Service Imports
model_gateway/src/worker/registry.rs, model_gateway/src/worker/service.rs
Updated import paths to pull HealthChecker, RuntimeType, WorkerType, and worker_to_info from the new submodules / re-exports.
Routers & Server Imports
model_gateway/src/routers/openai/realtime/rest.rs, model_gateway/src/server.rs
Simplified import paths for WorkerLoadGuard and WorkerType to reference new module layout (direct imports rather than nested worker::worker::...).
Workflow Steps Imports
model_gateway/src/workflow/steps/...
classify.rs, external/create_workers.rs, local/create_worker.rs, local/remove_from_worker_registry.rs, shared/register.rs
Adjusted use/import paths to reference crate::worker re-exports or crate::worker::traits for RuntimeType, WorkerType, ConnectionModeExt, and WorkerTypeExt.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

tests

Suggested reviewers

  • CatherineSue
  • key4ng
  • whybeyoung

Poem

🐰 I hopped through code with tiny paws,

Split modules clean without a pause.
Traits, bodies, metadata in a row,
The rabbit tidied—now imports flow.
🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'refactor(worker): split worker.rs into focused modules' accurately describes the main change: a refactoring that splits a monolithic worker.rs file into five focused modules (traits, basic, metadata, body, health_checker).
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/split-worker-rs

Comment @coderabbitai help to get the list of available commands and usage tips.


pub struct WorkerRoutingKeyLoad {
url: String,
pub(crate) active_routing_keys: dashmap::DashMap<String, usize>,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Nit: active_routing_keys was private before the split and is now widened to pub(crate) solely to satisfy test assertions in basic.rs. The tests (test_worker_routing_key_load_cleanup_on_zero, test_worker_routing_key_load_multiple_requests_same_key) use load.active_routing_keys.len(), but that's identical to the existing public load.value() method. Switching the tests to use value() would let this field stay private.

Suggested change
pub(crate) active_routing_keys: dashmap::DashMap<String, usize>,
active_routing_keys: dashmap::DashMap<String, usize>,

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4bcd6f3613

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@@ -1,36 +1,36 @@
//! Worker domain — identity, registry, health, resilience, monitoring, service.

pub mod basic;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep backward-compatible worker module exports

This refactor removes the public worker submodule from smg::worker (the old pub mod worker; path), but downstream code in this repo still imports that API shape (for example bindings/golang/src/policy.rs uses smg::worker::worker::{RuntimeType, WorkerMetadata, WorkerRoutingKeyLoad}). As a result, building smg-golang (and workspace builds that include it) will fail with unresolved imports unless this commit also provides a compatibility shim or updates all downstream imports in the same change.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the worker module by decomposing the large worker.rs file into several focused modules: basic.rs, body.rs, health_checker.rs, metadata.rs, and traits.rs. This reorganization improves maintainability and code structure while updating all internal references to use the new module paths. I have no feedback to provide.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@model_gateway/src/worker/metadata.rs`:
- Around line 11-14: The struct WorkerRoutingKeyLoad exposes mutable state via
the pub(crate) field active_routing_keys which lets internal code mutate
counters without going through update_metrics(); make active_routing_keys
private (remove pub(crate)) and add narrow, controlled accessors on
WorkerRoutingKeyLoad such as get_count(&self, key: &str) -> Option<usize> and
increment/decrement/update methods (or a test-only accessor behind cfg(test))
that perform mutations and call update_metrics() so all changes are tracked
centrally; update any callers to use these new methods rather than accessing the
field directly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 002acb19-c59b-48f0-93f0-412bf9f97af1

📥 Commits

Reviewing files that changed from the base of the PR and between f9367c7 and 4bcd6f3.

📒 Files selected for processing (16)
  • model_gateway/src/routers/openai/realtime/rest.rs
  • model_gateway/src/server.rs
  • model_gateway/src/worker/basic.rs
  • model_gateway/src/worker/body.rs
  • model_gateway/src/worker/builder.rs
  • model_gateway/src/worker/health_checker.rs
  • model_gateway/src/worker/metadata.rs
  • model_gateway/src/worker/mod.rs
  • model_gateway/src/worker/registry.rs
  • model_gateway/src/worker/service.rs
  • model_gateway/src/worker/traits.rs
  • model_gateway/src/workflow/steps/classify.rs
  • model_gateway/src/workflow/steps/external/create_workers.rs
  • model_gateway/src/workflow/steps/local/create_worker.rs
  • model_gateway/src/workflow/steps/local/remove_from_worker_registry.rs
  • model_gateway/src/workflow/steps/shared/register.rs

Comment on lines +11 to +14
pub struct WorkerRoutingKeyLoad {
url: String,
pub(crate) active_routing_keys: dashmap::DashMap<String, usize>,
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Keep active_routing_keys encapsulated to protect metric consistency.

Line 13 exposes mutable state as pub(crate), so internal callers can mutate counters without update_metrics(). Prefer private storage plus narrow accessor(s) (or test-only accessor) to avoid metric drift.

♻️ Proposed refactor
 pub struct WorkerRoutingKeyLoad {
     url: String,
-    pub(crate) active_routing_keys: dashmap::DashMap<String, usize>,
+    active_routing_keys: dashmap::DashMap<String, usize>,
 }
+
+#[cfg(test)]
+impl WorkerRoutingKeyLoad {
+    pub(crate) fn routing_key_count(&self, routing_key: &str) -> usize {
+        self.active_routing_keys
+            .get(routing_key)
+            .map(|v| *v)
+            .unwrap_or(0)
+    }
+}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@model_gateway/src/worker/metadata.rs` around lines 11 - 14, The struct
WorkerRoutingKeyLoad exposes mutable state via the pub(crate) field
active_routing_keys which lets internal code mutate counters without going
through update_metrics(); make active_routing_keys private (remove pub(crate))
and add narrow, controlled accessors on WorkerRoutingKeyLoad such as
get_count(&self, key: &str) -> Option<usize> and increment/decrement/update
methods (or a test-only accessor behind cfg(test)) that perform mutations and
call update_metrics() so all changes are tracked centrally; update any callers
to use these new methods rather than accessing the field directly.

Split the 1,834-line worker.rs monolith into five focused modules:

What changed:
- traits.rs (357 lines): Worker trait (40+ methods), ConnectionModeExt
  and WorkerTypeExt extension traits, worker_to_info() helper, and
  re-exports of ConnectionMode/RuntimeType/WorkerType
- basic.rs (1220 lines): BasicWorker struct, Debug impl, Worker impl,
  constants (DEFAULT_BOOTSTRAP_PORT, MOONCAKE_CONNECTOR), WORKER_CLIENT
  static, and all 850 lines of tests (moved from worker.rs inline)
- metadata.rs (138 lines): WorkerMetadata struct with model matching
  methods, WorkerRoutingKeyLoad per-key load tracking
- body.rs (94 lines): WorkerLoadGuard RAII guard, AttachedBody response
  body wrapper
- health_checker.rs (51 lines): HealthChecker handle with graceful
  shutdown (pub(crate))
- Deleted worker.rs, removed #[expect(clippy::module_inception)]
- Updated mod.rs re-exports to route through new module paths
- Updated 10 files with import path changes (worker::worker::X → X
  or worker::traits::X / worker::basic::X / worker::metadata::X)

Why:
worker.rs was a 1,834-line monolith containing the Worker trait,
BasicWorker impl, HealthChecker, WorkerMetadata, WorkerRoutingKeyLoad,
WorkerLoadGuard, AttachedBody, constants, and 850 lines of tests.
This split is prerequisite for the worker module deep refactor — later
PRs need to modify Worker trait, BasicWorker, and HealthChecker
independently.

How:
Pure code extraction — no behavioral changes. Each section moved to
its natural module. Tests moved to basic.rs with updated imports.
active_routing_keys field widened from private to pub(crate) so tests
in basic.rs can access WorkerRoutingKeyLoad internals. All 133 worker
unit tests and 100 integration tests pass identically.

Refs: worker-module-deep-refactor plan (PR 3)
Signed-off-by: Simo Lin <linsimo.mark@gmail.com>
@slin1237 slin1237 force-pushed the refactor/split-worker-rs branch from 4bcd6f3 to 16b80e6 Compare April 11, 2026 00:55
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
model_gateway/src/worker/metadata.rs (1)

11-14: 🛠️ Refactor suggestion | 🟠 Major

Keep active_routing_keys private.

Line 13 widens the mutation surface for routing-key counters. Any crate-internal caller can now bypass increment() / decrement(), which also means bypassing update_metrics() and drifting worker_routing_keys_active. A narrow #[cfg(test)] accessor is safer than exposing the DashMap itself.

♻️ Proposed refactor
 pub struct WorkerRoutingKeyLoad {
     url: String,
-    pub(crate) active_routing_keys: dashmap::DashMap<String, usize>,
+    active_routing_keys: dashmap::DashMap<String, usize>,
 }
+
+#[cfg(test)]
+impl WorkerRoutingKeyLoad {
+    pub(crate) fn routing_key_count(&self, routing_key: &str) -> usize {
+        self.active_routing_keys
+            .get(routing_key)
+            .map(|count| *count)
+            .unwrap_or(0)
+    }
+}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@model_gateway/src/worker/metadata.rs` around lines 11 - 14, The field
active_routing_keys on WorkerRoutingKeyLoad should be made private (remove
pub(crate)) to prevent crate-internal mutation; keep and use the existing
increment()/decrement() methods (and their call to update_metrics() /
worker_routing_keys_active) as the sole mutation API, and add a narrow
#[cfg(test)] accessor (e.g., a fn active_routing_keys_snapshot(&self) ->
HashMap<String, usize> or similar) so tests can read the map without mutating it
directly; update any tests to use that accessor instead of accessing the DashMap
field.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@model_gateway/src/worker/metadata.rs`:
- Around line 11-14: The field active_routing_keys on WorkerRoutingKeyLoad
should be made private (remove pub(crate)) to prevent crate-internal mutation;
keep and use the existing increment()/decrement() methods (and their call to
update_metrics() / worker_routing_keys_active) as the sole mutation API, and add
a narrow #[cfg(test)] accessor (e.g., a fn active_routing_keys_snapshot(&self)
-> HashMap<String, usize> or similar) so tests can read the map without mutating
it directly; update any tests to use that accessor instead of accessing the
DashMap field.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: ea020578-b138-48aa-9877-26ffd3496458

📥 Commits

Reviewing files that changed from the base of the PR and between 4bcd6f3 and 16b80e6.

📒 Files selected for processing (16)
  • model_gateway/src/routers/openai/realtime/rest.rs
  • model_gateway/src/server.rs
  • model_gateway/src/worker/basic.rs
  • model_gateway/src/worker/body.rs
  • model_gateway/src/worker/builder.rs
  • model_gateway/src/worker/health_checker.rs
  • model_gateway/src/worker/metadata.rs
  • model_gateway/src/worker/mod.rs
  • model_gateway/src/worker/registry.rs
  • model_gateway/src/worker/service.rs
  • model_gateway/src/worker/traits.rs
  • model_gateway/src/workflow/steps/classify.rs
  • model_gateway/src/workflow/steps/external/create_workers.rs
  • model_gateway/src/workflow/steps/local/create_worker.rs
  • model_gateway/src/workflow/steps/local/remove_from_worker_registry.rs
  • model_gateway/src/workflow/steps/shared/register.rs

@slin1237
Copy link
Copy Markdown
Member Author

Closing — the split was premature and the file names didn't communicate their contents. worker.rs stays as-is; any splits will happen organically when later PRs (WorkerManager extraction, status state machine) actually need the separation.

@slin1237 slin1237 closed this Apr 11, 2026
@slin1237 slin1237 deleted the refactor/split-worker-rs branch April 11, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model-gateway Model gateway crate changes openai OpenAI router changes realtime-api Realtime API related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant