Worker Pool v2: Dynamic Scaling with Lazy Initialization #14

Closed
opened 2026-02-09 21:22:42 +03:00 by NiXTheDev · 3 comments
NiXTheDev commented 2026-02-09 21:22:42 +03:00 (Migrated from github.com)

Overview

Implement a v2 worker pool with dynamic scaling capabilities to optimize resource usage and improve performance under varying loads.

Goals

  • Start with minimal/zero workers instead of pre-allocating
  • Scale up workers on-demand when queue grows
  • Scale down idle workers after configurable timeout
  • Maintain backward compatibility with existing timeout/error handling

Technical Requirements

Core Features

  • Design WorkerPool interface supporting both current and v2 implementations
  • Implement lazy initialization (start with 0 or minimal workers)
  • Add MAX_WORKER_POOL_SIZE configuration cap
  • Spawn workers on-demand up to the cap when queue grows
  • Track per-worker queue length or load metrics Architecture changed to one fifo queue
  • Dispatch tasks to least-loaded worker Architecture changed. workers consume from one queue, post back to it in case sed commands are chained

Idle Management

  • Track last-active time per worker Global timer
  • Implement idle scale-down after configurable period (default: 15 minutes)
  • Terminate excess workers down to minimal pool size
  • Preserve existing WORKER_TIMEOUT_MS semantics

Testing

  • Add tests for scaling up under load
  • Add tests for scaling down on idle
  • Test correct ordering when many sed commands are queued
  • Test behavior when pool is at hard cap

Notes

  • This is a larger change - consider doing in a separate feature branch
  • Preserve all existing error messages and timeout behavior
  • Ensure graceful shutdown still works correctly

Priority: Medium-High
Estimated Effort: Large (separate branch recommended)

## Overview Implement a v2 worker pool with dynamic scaling capabilities to optimize resource usage and improve performance under varying loads. ## Goals - Start with minimal/zero workers instead of pre-allocating - Scale up workers on-demand when queue grows - Scale down idle workers after configurable timeout - Maintain backward compatibility with existing timeout/error handling ## Technical Requirements ### Core Features - [x] Design WorkerPool interface supporting both current and v2 implementations - [x] Implement lazy initialization (start with 0 or minimal workers) - [x] Add MAX_WORKER_POOL_SIZE configuration cap - [x] Spawn workers on-demand up to the cap when queue grows - ~~Track per-worker queue length or load metrics~~ Architecture changed to one fifo queue - ~~Dispatch tasks to least-loaded worker~~ Architecture changed. workers consume from one queue, post back to it in case sed commands are chained ### Idle Management - ~~Track last-active time per worker~~ Global timer - [x] Implement idle scale-down after configurable period (default: 15 minutes) - [x] Terminate excess workers down to minimal pool size - [x] Preserve existing WORKER_TIMEOUT_MS semantics ### Testing - [x] Add tests for scaling up under load - [x] Add tests for scaling down on idle - [x] Test correct ordering when many sed commands are queued - [x] Test behavior when pool is at hard cap ## Notes - This is a larger change - consider doing in a separate feature branch - Preserve all existing error messages and timeout behavior - Ensure graceful shutdown still works correctly **Priority:** Medium-High **Estimated Effort:** Large (separate branch recommended)
NiXTheDev commented 2026-02-09 21:30:28 +03:00 (Migrated from github.com)

Additional Task from TODO item 9

When Worker Pool v2 is completed, update the changelog with an entry explaining:

  • The new dynamic scaling architecture
  • Performance improvements
  • Migration notes for any breaking changes
  • Configuration changes (new env vars, etc.)

This ensures future contributors understand the evolution of the worker pool system.

## Additional Task from [TODO item 9](https://github.com/NiXTheDev/regexYbot/commit/6d75d7fc456e5f96c6b071009c6030b82328687f#diff-5c6a1301c6b59b30a040d747d065e861d3dd98bde0e5a4356d92d594e9835986L124) When Worker Pool v2 is completed, update the changelog with an entry explaining: - The new dynamic scaling architecture - Performance improvements - Migration notes for any breaking changes - Configuration changes (new env vars, etc.) This ensures future contributors understand the evolution of the worker pool system.
NiXTheDev commented 2026-02-12 16:13:24 +03:00 (Migrated from github.com)

Implementation Complete

All WorkerPoolV2 features have been implemented and merged to dev branch:

Core Features

  • Dynamic scaling - Workers spawn on-demand up to maxWorkers, terminate when idle
  • Lazy initialization - Starts with initialWorkers (default: 1), scales as needed
  • Centralized FIFO queue - Single queue for all tasks instead of per-worker queues
  • Configurable limits - minWorkers, maxWorkers, idle timeout, check intervals

Shutdown Enhancements

  • Scale beyond maxWorkers during shutdown - When drainTasks: true, pool spawns workers equal to queue size (capped at 20) to process remaining tasks quickly
  • Batch worker spawning - Efficiently spawns multiple workers in single call
  • Graceful draining - Processes all queued tasks before terminating

Implementation Details

  • spawnWorker(count = 1) - Batch spawn workers, returns count spawned
  • isShuttingDown state - Automatically bypasses maxWorkers limit during shutdown
  • unref() on idle check interval - Prevents keeping process alive
  • Full error handling preserved

Tests Added (9 tests)

  1. Basic task execution
  2. Scale up when tasks queue
  3. Scale down idle workers
  4. Respects max workers limit
  5. Handles invalid regex errors
  6. Pool continues working after errors
  7. Returns pool statistics
  8. Rejects new tasks during shutdown
  9. Scales past maxWorkers during shutdown with draining

All 77 tests passing

Commit: 2c8404b

## ✅ Implementation Complete All WorkerPoolV2 features have been implemented and merged to `dev` branch: ### Core Features - **Dynamic scaling** - Workers spawn on-demand up to maxWorkers, terminate when idle - **Lazy initialization** - Starts with `initialWorkers` (default: 1), scales as needed - **Centralized FIFO queue** - Single queue for all tasks instead of per-worker queues - **Configurable limits** - minWorkers, maxWorkers, idle timeout, check intervals ### Shutdown Enhancements - **Scale beyond maxWorkers during shutdown** - When `drainTasks: true`, pool spawns workers equal to queue size (capped at 20) to process remaining tasks quickly - **Batch worker spawning** - Efficiently spawns multiple workers in single call - **Graceful draining** - Processes all queued tasks before terminating ### Implementation Details - `spawnWorker(count = 1)` - Batch spawn workers, returns count spawned - `isShuttingDown` state - Automatically bypasses maxWorkers limit during shutdown - `unref()` on idle check interval - Prevents keeping process alive - Full error handling preserved ### Tests Added (9 tests) 1. Basic task execution 2. Scale up when tasks queue 3. Scale down idle workers 4. Respects max workers limit 5. Handles invalid regex errors 6. Pool continues working after errors 7. Returns pool statistics 8. Rejects new tasks during shutdown 9. Scales past maxWorkers during shutdown with draining **All 77 tests passing** ✅ Commit: `2c8404b`
NiXTheDev commented 2026-02-12 16:13:24 +03:00 (Migrated from github.com)

All tasks completed. WorkerPoolV2 fully implemented with dynamic scaling, lazy initialization, and shutdown scaling. Ready for production use.

All tasks completed. WorkerPoolV2 fully implemented with dynamic scaling, lazy initialization, and shutdown scaling. Ready for production use.
Sign in to join this conversation.
No description provided.