mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
* Move group into a separate helper module for reuse * Add shutdownCh to worker The shutdown channel is used to signal that worker has stopped. * Make server shutdown block on workers' shutdownCh * Fix waiting for eval broker state change blocking indefinitely There was a race condition in the GenericNotifier between the Run and WaitForChange functions, where WaitForChange blocks trying to write to a full unsubscribeCh, but the Run function never reads from the unsubscribeCh as it has already stopped. This commit fixes it by unblocking if the notifier has been stopped. * Bound the amount of time server shutdown waits on worker completion * Fix lostcancel linter error * Fix worker test using unexpected worker constructor * Add changelog --------- Co-authored-by: Marvin Chin <marvinchin@users.noreply.github.com>
51 lines
958 B
Go
51 lines
958 B
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package group
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
)
|
|
|
|
// group wraps a func() in a goroutine and provides a way to block until it
|
|
// exits. Inspired by https://godoc.org/golang.org/x/sync/errgroup
|
|
type Group struct {
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
// Go starts f in a goroutine and must be called before Wait.
|
|
func (g *Group) Go(f func()) {
|
|
g.wg.Add(1)
|
|
go func() {
|
|
defer g.wg.Done()
|
|
f()
|
|
}()
|
|
}
|
|
|
|
func (g *Group) AddCh(ch <-chan struct{}) {
|
|
g.Go(func() {
|
|
<-ch
|
|
})
|
|
}
|
|
|
|
// Wait for all goroutines to exit. Must be called after all calls to Go
|
|
// complete.
|
|
func (g *Group) Wait() {
|
|
g.wg.Wait()
|
|
}
|
|
|
|
// Wait for all goroutines to exit, or for the context to finish.
|
|
// Must be called after all calls to Go complete.
|
|
func (g *Group) WaitWithContext(ctx context.Context) {
|
|
doneCh := make(chan struct{})
|
|
go func() {
|
|
defer close(doneCh)
|
|
g.Wait()
|
|
}()
|
|
select {
|
|
case <-doneCh:
|
|
case <-ctx.Done():
|
|
}
|
|
}
|