Skip to content

refactor(worker): group event-loop state and avoid per-deferral threads #37

Description

@lemtoc

As of v0.3.0 (c73dc6c).

Summary

Two small internal cleanups in src/worker/mod.rs, bundled because they touch the same event loop:

  1. handle_request_chunk takes 9 arguments and carries a #[allow(clippy::too_many_arguments)] (src/worker/mod.rs:174). The warned_config_error / warned_config_warnings flags are conceptually part of ConfigState, and decoder/cache/response/active_prompt are loop-lifetime state. Absorbing the warning flags into ConfigState and holding the rest in a WorkerLoop struct with methods removes the allow and makes the data flow explicit.
  2. defer_job_result spawns a thread per deferred result (src/worker/mod.rs:293) to honor the min_loading_ms floor. With MAX_CONCURRENCY = 2 this is harmless in practice, but the event loop could instead keep a small queue of (deadline, JobResult) and use recv_timeout until the earliest deadline, eliminating ad-hoc thread creation.

Proposed fix

  • Move warned_config_error and warned_config_warnings into ConfigState.
  • Introduce a WorkerLoop (or similar) struct owning decoder, cache, response, active_prompt, job_pool, and config_state; convert the free functions into methods.
  • Replace defer_job_result's thread::spawn + sleep with deadline-aware recv_timeout in the main loop.

No behavior change intended; existing worker tests should pass as-is.


🤖 Generated with Claude Code — Claude Fable 5

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low priority or proposalarea:workerComponent: workerenhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions