Types
🧩 Types
Workers supports two types of worker pool sizes: dynamic and fixed.
The choice between them depends on the nature of the tasks being executed.
- Dynamic pool size is suitable for most cases, offering flexibility with low memory overhead.
- Fixed pool size is preferred for tasks that require significant memory allocation or have heavy initialization costs.
⚙️ Dynamic Pool Size
The dynamic pool is implemented using a wrapper over Go's sync.Pool
.
It is initialized by setting workers.Config.MaxWorkers
to 0
(the default value):
w := workers.New[string](context.Background(), &workers.Config{MaxWorkers: 0})
or simply:
w := workers.New[string](context.Background(), nil)
With a dynamic pool, workers are created and reused automatically as needed, optimizing resource usage without hard limits.
⚙️ Fixed Pool Size
The fixed pool is initialized by setting workers.Config.MaxWorkers
to a positive integer:
w := workers.New[string](context.Background(), &workers.Config{MaxWorkers: 10})
You can also dynamically limit the number of workers to the number of logical CPUs available:
w := workers.New[string](context.Background(), &workers.Config{MaxWorkers: runtime.NumCPU()})
In a fixed pool:
- New workers are added sequentially when tasks are received, up to the configured maximum.
- Once the pool reaches its limit, new tasks wait for an available worker.
- Each worker can execute only one task at a time, ensuring controlled concurrency.
📚 Summary
Type | Description |
---|---|
Dynamic | No hard worker limit; uses sync.Pool ; adapts automatically. |
Fixed | Limits concurrency to a maximum number; better for memory-heavy or resource-bound tasks. |