From 1044741269e04c353e9653532f2a0346e8c044ff Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Thu, 6 Apr 2023 11:25:16 +1000 Subject: [PATCH] Initial commit --- LICENSE | 21 +++++++++++++++++++++ README.md | 3 +++ group.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 group.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bd28b97 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0d5ff72 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Group + +A group provides a way manage the lifetime of a group of related goroutines. \ No newline at end of file diff --git a/group.go b/group.go new file mode 100644 index 0000000..4d243d1 --- /dev/null +++ b/group.go @@ -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 +}