| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | // Copyright 2016 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Package errgroup provides synchronization, error propagation, and Context | 
					
						
							|  |  |  | // cancelation for groups of goroutines working on subtasks of a common task. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // [errgroup.Group] is related to [sync.WaitGroup] but adds handling of tasks | 
					
						
							|  |  |  | // returning errors. | 
					
						
							|  |  |  | package errgroup | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | 	"runtime" | 
					
						
							|  |  |  | 	"runtime/debug" | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | 	"sync" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type token struct{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // A Group is a collection of goroutines working on subtasks that are part of | 
					
						
							| 
									
										
										
										
											2025-04-07 11:04:56 +01:00
										 |  |  | // the same overall task. A Group should not be reused for different tasks. | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | // | 
					
						
							|  |  |  | // A zero Group is valid, has no limit on the number of active goroutines, | 
					
						
							|  |  |  | // and does not cancel on error. | 
					
						
							|  |  |  | type Group struct { | 
					
						
							|  |  |  | 	cancel func(error) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	wg sync.WaitGroup | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sem chan token | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	errOnce sync.Once | 
					
						
							|  |  |  | 	err     error | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mu         sync.Mutex | 
					
						
							|  |  |  | 	panicValue any  // = PanicError | PanicValue; non-nil if some Group.Go goroutine panicked. | 
					
						
							|  |  |  | 	abnormal   bool // some Group.Go goroutine terminated abnormally (panic or goexit). | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (g *Group) done() { | 
					
						
							|  |  |  | 	if g.sem != nil { | 
					
						
							|  |  |  | 		<-g.sem | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	g.wg.Done() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WithContext returns a new Group and an associated Context derived from ctx. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The derived Context is canceled the first time a function passed to Go | 
					
						
							|  |  |  | // returns a non-nil error or the first time Wait returns, whichever occurs | 
					
						
							|  |  |  | // first. | 
					
						
							|  |  |  | func WithContext(ctx context.Context) (*Group, context.Context) { | 
					
						
							| 
									
										
										
										
											2025-03-10 10:22:28 +01:00
										 |  |  | 	ctx, cancel := context.WithCancelCause(ctx) | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | 	return &Group{cancel: cancel}, ctx | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | // Wait blocks until all function calls from the Go method have returned | 
					
						
							|  |  |  | // normally, then returns the first non-nil error (if any) from them. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // If any of the calls panics, Wait panics with a [PanicValue]; | 
					
						
							|  |  |  | // and if any of them calls [runtime.Goexit], Wait calls runtime.Goexit. | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | func (g *Group) Wait() error { | 
					
						
							|  |  |  | 	g.wg.Wait() | 
					
						
							|  |  |  | 	if g.cancel != nil { | 
					
						
							|  |  |  | 		g.cancel(g.err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | 	if g.panicValue != nil { | 
					
						
							|  |  |  | 		panic(g.panicValue) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if g.abnormal { | 
					
						
							|  |  |  | 		runtime.Goexit() | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | 	return g.err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Go calls the given function in a new goroutine. | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2025-06-10 01:08:57 +02:00
										 |  |  | // The first call to Go must happen before a Wait. | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | // It blocks until the new goroutine can be added without the number of | 
					
						
							|  |  |  | // goroutines in the group exceeding the configured limit. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The first goroutine in the group that returns a non-nil error, panics, or | 
					
						
							|  |  |  | // invokes [runtime.Goexit] will cancel the associated Context, if any. | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | func (g *Group) Go(f func() error) { | 
					
						
							|  |  |  | 	if g.sem != nil { | 
					
						
							|  |  |  | 		g.sem <- token{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | 	g.add(f) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (g *Group) add(f func() error) { | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | 	g.wg.Add(1) | 
					
						
							|  |  |  | 	go func() { | 
					
						
							|  |  |  | 		defer g.done() | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | 		normalReturn := false | 
					
						
							|  |  |  | 		defer func() { | 
					
						
							|  |  |  | 			if normalReturn { | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			v := recover() | 
					
						
							|  |  |  | 			g.mu.Lock() | 
					
						
							|  |  |  | 			defer g.mu.Unlock() | 
					
						
							|  |  |  | 			if !g.abnormal { | 
					
						
							|  |  |  | 				if g.cancel != nil { | 
					
						
							|  |  |  | 					g.cancel(g.err) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				g.abnormal = true | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if v != nil && g.panicValue == nil { | 
					
						
							|  |  |  | 				switch v := v.(type) { | 
					
						
							|  |  |  | 				case error: | 
					
						
							|  |  |  | 					g.panicValue = PanicError{ | 
					
						
							|  |  |  | 						Recovered: v, | 
					
						
							|  |  |  | 						Stack:     debug.Stack(), | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				default: | 
					
						
							|  |  |  | 					g.panicValue = PanicValue{ | 
					
						
							|  |  |  | 						Recovered: v, | 
					
						
							|  |  |  | 						Stack:     debug.Stack(), | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}() | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | 		err := f() | 
					
						
							|  |  |  | 		normalReturn = true | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | 			g.errOnce.Do(func() { | 
					
						
							|  |  |  | 				g.err = err | 
					
						
							|  |  |  | 				if g.cancel != nil { | 
					
						
							|  |  |  | 					g.cancel(g.err) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TryGo calls the given function in a new goroutine only if the number of | 
					
						
							|  |  |  | // active goroutines in the group is currently below the configured limit. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The return value reports whether the goroutine was started. | 
					
						
							|  |  |  | func (g *Group) TryGo(f func() error) bool { | 
					
						
							|  |  |  | 	if g.sem != nil { | 
					
						
							|  |  |  | 		select { | 
					
						
							|  |  |  | 		case g.sem <- token{}: | 
					
						
							|  |  |  | 			// Note: this allows barging iff channels in general allow barging. | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | 	g.add(f) | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetLimit limits the number of active goroutines in this group to at most n. | 
					
						
							|  |  |  | // A negative value indicates no limit. | 
					
						
							| 
									
										
										
										
											2025-02-10 15:10:30 +00:00
										 |  |  | // A limit of zero will prevent any new goroutines from being added. | 
					
						
							| 
									
										
										
										
											2024-04-26 11:31:10 +02:00
										 |  |  | // | 
					
						
							|  |  |  | // Any subsequent call to the Go method will block until it can add an active | 
					
						
							|  |  |  | // goroutine without exceeding the configured limit. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The limit must not be modified while any goroutines in the group are active. | 
					
						
							|  |  |  | func (g *Group) SetLimit(n int) { | 
					
						
							|  |  |  | 	if n < 0 { | 
					
						
							|  |  |  | 		g.sem = nil | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(g.sem) != 0 { | 
					
						
							|  |  |  | 		panic(fmt.Errorf("errgroup: modify limit while %v goroutines in the group are still active", len(g.sem))) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	g.sem = make(chan token, n) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // PanicError wraps an error recovered from an unhandled panic | 
					
						
							|  |  |  | // when calling a function passed to Go or TryGo. | 
					
						
							|  |  |  | type PanicError struct { | 
					
						
							|  |  |  | 	Recovered error | 
					
						
							|  |  |  | 	Stack     []byte // result of call to [debug.Stack] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p PanicError) Error() string { | 
					
						
							| 
									
										
										
										
											2025-06-10 01:08:57 +02:00
										 |  |  | 	if len(p.Stack) > 0 { | 
					
						
							|  |  |  | 		return fmt.Sprintf("recovered from errgroup.Group: %v\n%s", p.Recovered, p.Stack) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-05-10 14:27:25 +00:00
										 |  |  | 	return fmt.Sprintf("recovered from errgroup.Group: %v", p.Recovered) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p PanicError) Unwrap() error { return p.Recovered } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // PanicValue wraps a value that does not implement the error interface, | 
					
						
							|  |  |  | // recovered from an unhandled panic when calling a function passed to Go or | 
					
						
							|  |  |  | // TryGo. | 
					
						
							|  |  |  | type PanicValue struct { | 
					
						
							|  |  |  | 	Recovered any | 
					
						
							|  |  |  | 	Stack     []byte // result of call to [debug.Stack] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p PanicValue) String() string { | 
					
						
							|  |  |  | 	if len(p.Stack) > 0 { | 
					
						
							|  |  |  | 		return fmt.Sprintf("recovered from errgroup.Group: %v\n%s", p.Recovered, p.Stack) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return fmt.Sprintf("recovered from errgroup.Group: %v", p.Recovered) | 
					
						
							|  |  |  | } |