-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Description
The errgroup package will currently spawn a new goroutine for each invocation of Group.Go. This is usually fine, but extremely high cardinality fanout can exhaust memory or other resources. It would be neat if the errgroup interface allowed users to specify the maximum number of concurrent goroutines they want the errgroup to spawn.
Proposal
type Group struct {
N int
// contains etc
}N would be copied to an unexported on the first invocation of Go, so that subsequent modification has no effect. This preserves the validity and the behavior of the empty Group.
When calling Go, if the number of functions running is > N then Go would block until the number was <= N.
The behavior of Go is not otherwise modified; if a subtask returns an error, then subsequent tasks will still be executed, and callers would rely on subtasks handling context cancellation to fall through to the Wait() call and then return, if WithContext was called.
Alternatives considered
An alternative interface would be that Go never block, but enqueue instead. This is an unbounded queue and I'm not a fan.
Another alternative is that the group is context-aware, and that Go return immediately if the group's context is cancelled. This requires that Group retain a reference to the context, which it does not currently do.