Skip to content

Add idle timeout for WebWorkerPool#13

Merged
paberr merged 1 commit intomainfrom
feature/idle-cleanup
Mar 8, 2026
Merged

Add idle timeout for WebWorkerPool#13
paberr merged 1 commit intomainfrom
feature/idle-cleanup

Conversation

@paberr
Copy link
Owner

@paberr paberr commented Mar 8, 2026

Summary

  • Adds idle_timeout_ms option to WorkerPoolOptions — workers with no pending tasks are terminated after the configured duration and transparently recreated on demand
  • Uses per-slot RefCell<WorkerSlot> so the idle checker and task execution can borrow independent slots without conflict
  • Adds last_active() tracking to WebWorker, num_active_workers() to WebWorkerPool, and integration tests for idle cleanup

Test plan

  • cargo +nightly fmt --all -- --check passes
  • cargo +nightly clippy --all-features passes (no warnings)
  • cargo test --doc --all-features passes
  • cd test && npm ci && npm test (4/4 integration tests pass)

@paberr paberr linked an issue Mar 8, 2026 that may be closed by this pull request
@paberr paberr added the enhancement New feature or request label Mar 8, 2026
@paberr paberr requested a review from Copilot March 8, 2026 15:51
@paberr paberr self-assigned this Mar 8, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds an idle-timeout feature to WebWorkerPool so that inactive web workers can be terminated after a configurable duration and recreated on-demand, with supporting scheduler updates and integration coverage in the test crate.

Changes:

  • Add idle_timeout_ms to WorkerPoolOptions and an interval-based idle reaper that transitions workers to an Empty slot state.
  • Refactor the pool from Vec<WebWorker> to per-slot RefCell<WorkerSlot> and update scheduling to operate on per-slot load snapshots.
  • Add WebWorker::last_active() tracking and an integration test validating idle termination and transparent recreation.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/pool/mod.rs Introduces idle-timeout configuration, WorkerSlot state machine, slot-based worker management, recreation logic, and active-worker metrics.
src/pool/scheduler.rs Updates scheduler API to choose from Option<load> slots and return None when no active workers exist.
src/webworker/worker.rs Adds last_active timestamp tracking updated on task completion for idle-timeout decisions.
test/src/raw.rs Adds a JS sleep_ms helper and a new integration test for idle cleanup + recreation.
test/src/lib.rs Wires the new idle-timeout test into the test runner.
test/Cargo.toml Adds js-sys dependency and enables web-sys Window feature for timer usage.
Cargo.lock Updates lockfile for the new js-sys dependency in the test crate.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Workers with no pending tasks are automatically terminated after a
configurable idle_timeout_ms duration and transparently recreated when
new tasks arrive. Uses per-slot RefCell<WorkerSlot> instead of a single
RefCell around the whole Vec, allowing independent borrowing so the idle
checker and task execution never conflict.
@paberr paberr force-pushed the feature/idle-cleanup branch from b3c1adf to 84929d7 Compare March 8, 2026 16:13
@paberr paberr merged commit 2c7ef20 into main Mar 8, 2026
8 checks passed
@paberr paberr deleted the feature/idle-cleanup branch March 8, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

idle timeout

2 participants