panjf2000/ants · error

invalid load-balancing strategy

Error message

invalid load-balancing strategy

What it means

ErrInvalidLoadBalancingStrategy is returned by NewMultiPool, NewMultiPoolWithFunc and NewMultiPoolWithFuncGeneric when the load-balancing argument is not one of the supported strategies (ants.RoundRobin or ants.LeastTasks). Passing an arbitrary integer yields this error instead of silently mis-dispatching tasks.

Source

Thrown at ants.go:85

	ErrInvalidPoolExpiry = errors.New("invalid expiry for pool")

	// ErrPoolClosed will be returned when submitting task to a closed pool.
	ErrPoolClosed = errors.New("this pool has been closed")

	// ErrPoolOverload will be returned when the pool is full and no workers available.
	ErrPoolOverload = errors.New("too many goroutines blocked on submit or Nonblocking is set")

	// ErrInvalidPreAllocSize will be returned when trying to set up a negative capacity under PreAlloc mode.
	ErrInvalidPreAllocSize = errors.New("can not set up a negative capacity under PreAlloc mode")

	// ErrTimeout will be returned after the operations timed out.
	ErrTimeout = errors.New("operation timed out")

	// ErrInvalidPoolIndex will be returned when trying to retrieve a pool with an invalid index.
	ErrInvalidPoolIndex = errors.New("invalid pool index")

	// ErrInvalidLoadBalancingStrategy will be returned when trying to create a MultiPool with an invalid load-balancing strategy.
	ErrInvalidLoadBalancingStrategy = errors.New("invalid load-balancing strategy")

	// ErrInvalidMultiPoolSize  will be returned when trying to create a MultiPool with an invalid size.
	ErrInvalidMultiPoolSize = errors.New("invalid size for multiple pool")

	// workerChanCap determines whether the channel of a worker should be a buffered channel
	// to get the best performance. Inspired by fasthttp at
	// https://github.com/valyala/fasthttp/blob/master/workerpool.go#L139
	workerChanCap = func() int {
		// Use blocking channel if GOMAXPROCS=1.
		// This switches context from sender to receiver immediately,
		// which results in higher performance (under go1.5 at least).
		if runtime.GOMAXPROCS(0) == 1 {
			return 0
		}

		// Use non-blocking workerChan if GOMAXPROCS>1,
		// since otherwise the sender might be dragged down if the receiver is CPU-bound.
		return 1

View on GitHub (pinned to 107e376781)

Solutions

  1. Pass ants.RoundRobin or ants.LeastTasks explicitly as the strategy argument.
  2. Check argument order: NewMultiPool(size, div, strategy, options...) — verify the strategy slot.
  3. Validate any config-sourced strategy against the two allowed constants before constructing.

Example fix

// before
mp, err := ants.NewMultiPool(10, -1, 8)
// after
mp, err := ants.NewMultiPool(10, -1, ants.RoundRobin)
Defensive patterns

Strategy: validation

Validate before calling

func validStrategy(s ants.LoadBalancingStrategy) bool {
    return s == ants.RoundRobin || s == ants.LeastTasks
}
if !validStrategy(strategy) {
    strategy = ants.RoundRobin
}

Prevention

When it happens

Trigger: ants.NewMultiPool(10, -1, 8) — here 8 is the strategy slot and is invalid; any construction with a strategy value other than RoundRobin or LeastTasks constants.

Common situations: Swapped arguments — passing the task-arg type or a size where the strategy belongs; custom numeric strategies assumed to be valid; refactors renaming constants.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of panjf2000/ants@107e376781 (2026-09-06). Data as JSON: /api/errors/3cc02bfb7daa377a. Report an issue: GitHub.