Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davecheney committed Apr 6, 2023
0 parents commit 1044741
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
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.
3 changes: 3 additions & 0 deletions README.md
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.
52 changes: 52 additions & 0 deletions group.go
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
}

0 comments on commit 1044741

Please sign in to comment.