-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1044741
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 pkg | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Group | ||
|
||
A group provides a way manage the lifetime of a group of related goroutines. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// package group provides a way to manage the lifecycle of a group of goroutines. | ||
package group | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// A G manages the lifetime of a set of goroutines from a common context. | ||
// The first goroutine in the group to return will cause the context to be canceled, | ||
// terminating the remaining goroutines. | ||
type G struct { | ||
// ctx is the context passed to all goroutines in the group. | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
done sync.WaitGroup | ||
|
||
errOnce sync.Once | ||
err error | ||
} | ||
|
||
// New returns a new group using the given context. | ||
func New(ctx context.Context) *G { | ||
ctx, cancel := context.WithCancel(ctx) | ||
return &G{ | ||
ctx: ctx, | ||
cancel: cancel, | ||
} | ||
} | ||
|
||
// Add runs fn in a new goroutine and adds it to the group. | ||
// The goroutine should exit when the context passed to it is canceled. | ||
func (g *G) Add(fn func(context.Context) error) { | ||
g.done.Add(1) | ||
go func() { | ||
defer g.done.Done() | ||
defer g.cancel() | ||
if err := fn(g.ctx); err != nil { | ||
g.errOnce.Do(func() { g.err = err }) | ||
} | ||
}() | ||
} | ||
|
||
// Wait waits for all goroutines in the group to exit. | ||
// If any of the goroutines fail with an error, Wait will return the first error. | ||
func (g *G) Wait() error { | ||
g.done.Wait() | ||
g.errOnce.Do(func() { | ||
// noop, required to synchronise on the errOnce mutex. | ||
}) | ||
return g.err | ||
} |