Skip to content

Auto-Configuration

Auto-configuration (added in v0.2.0) sets min and max workers, queue size and the scale-up threshold for you, from a workload profile and the resources available to the process.

pool, err := adaptivepool.New(
adaptivepool.WithAutoConfig(adaptivepool.ProfileAPIServer),
)
ProfileUse caseMin workersMax workersQueue sizeScale-up threshold
ProfileAPIServerI/O-bound API servers2x CPUs10x CPUs20x max workers0.6
ProfileCPUIntensiveCPU-bound tasks1x CPUs2x CPUs5x max workers0.8
ProfileBatchProcessorBatch processing2x CPUs4x CPUs50x max workers0.7

CPUs are counted with GOMAXPROCS, which follows container CPU limits on Linux: a pod limited to 2 CPUs is sized for 2, not for every core on the node.

For more control, describe the workload instead of picking a profile:

pool, err := adaptivepool.New(
adaptivepool.WithSystemAwareConfig(adaptivepool.SystemAwareConfig{
WorkloadType: adaptivepool.IOBound, // or CPUBound, Mixed
TargetLatencyMs: 200,
AvgJobMemoryBytes: 50 * 1024, // memory held by one queued job
MemoryLimitPercent: 0.2, // queue may use 20% of the memory limit
}),
)
Workload typeMin workersMax workersDefault queue sizeScale-up threshold
IOBound2x CPUs8x CPUs20x max workers0.6
CPUBound1x CPUs2x CPUs5x max workers0.8
Mixed1x CPUs4x CPUs10x max workers0.7
  • Latency target: below 100ms lowers the scale-up threshold to 0.5, and below 500ms to 0.6, so workers are added sooner.
  • Memory: with AvgJobMemoryBytes set, the queue holds as many jobs as fit in MemoryLimitPercent of the process memory limit (default 0.2), kept between 5x and 100x max workers. The limit is GOMEMLIMIT when set, otherwise the container’s cgroup memory limit on Linux. Without either, the default queue size above is used.

Auto-config only sets base values. Explicit options override them, wherever they appear in the list:

pool, err := adaptivepool.New(
adaptivepool.WithAutoConfig(adaptivepool.ProfileAPIServer),
adaptivepool.WithQueueSize(5000),
)

The overridden values still have to be valid together: for example, WithMinWorkers can’t exceed the profile’s max workers, or New returns an error wrapping ErrInvalidConfig.

If you pass both WithAutoConfig and WithSystemAwareConfig, the last one wins.

The pool logs the final values once at startup, after your overrides:

level=INFO msg="auto-config applied" min_workers=4 max_workers=20 queue_size=400 scale_up_threshold=0.6

To inspect the values without creating a pool, use SuggestConfig:

c := adaptivepool.SuggestConfig(adaptivepool.IOBound)
fmt.Println(c.MinWorkers(), c.MaxWorkers(), c.QueueSize())

See examples/autoconfig for a runnable example.