2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
package routine
|
2019-10-04 17:08:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2020-01-28 23:50:41 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-10-04 17:08:45 +00:00
|
|
|
)
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
type Routine func(ctx context.Context) error
|
2019-10-04 17:08:45 +00:00
|
|
|
|
2022-04-05 18:17:53 +00:00
|
|
|
// cancelCh is the ctx.Done()
|
|
|
|
// When cancel() is called, if the routine is running a blocking call (e.g. some ACL replication RPCs),
|
2022-10-21 19:58:06 +00:00
|
|
|
// stoppedCh won't be closed till the blocking call returns, while cancelCh will be closed immediately.
|
2022-04-05 18:17:53 +00:00
|
|
|
// cancelCh is used to properly detect routine running status between cancel() and close(stoppedCh)
|
2021-05-20 14:07:23 +00:00
|
|
|
type routineTracker struct {
|
2020-11-13 19:56:41 +00:00
|
|
|
cancel context.CancelFunc
|
2022-04-05 18:17:53 +00:00
|
|
|
cancelCh <-chan struct{} // closed when ctx is done
|
|
|
|
stoppedCh chan struct{} // closed when no longer running
|
2020-11-13 19:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (r *routineTracker) running() bool {
|
2020-11-13 19:56:41 +00:00
|
|
|
select {
|
|
|
|
case <-r.stoppedCh:
|
|
|
|
return false
|
2022-04-05 18:17:53 +00:00
|
|
|
case <-r.cancelCh:
|
|
|
|
return false
|
2020-11-13 19:56:41 +00:00
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 22:59:31 +00:00
|
|
|
func (r *routineTracker) wait() {
|
|
|
|
<-r.stoppedCh
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
type Manager struct {
|
2019-10-04 17:08:45 +00:00
|
|
|
lock sync.RWMutex
|
2020-01-28 23:50:41 +00:00
|
|
|
logger hclog.Logger
|
2019-10-04 17:08:45 +00:00
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
routines map[string]*routineTracker
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func NewManager(logger hclog.Logger) *Manager {
|
2019-10-04 17:08:45 +00:00
|
|
|
if logger == nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
logger = hclog.New(&hclog.LoggerOptions{
|
|
|
|
Output: os.Stderr,
|
|
|
|
})
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
return &Manager{
|
|
|
|
logger: logger,
|
|
|
|
routines: make(map[string]*routineTracker),
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (m *Manager) IsRunning(name string) bool {
|
2019-10-04 17:08:45 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
if routine, ok := m.routines[name]; ok {
|
2020-11-13 19:56:41 +00:00
|
|
|
return routine.running()
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (m *Manager) Start(ctx context.Context, name string, routine Routine) error {
|
2019-10-04 17:08:45 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
2020-11-13 19:56:41 +00:00
|
|
|
if instance, ok := m.routines[name]; ok && instance.running() {
|
2019-10-04 17:08:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
rtCtx, cancel := context.WithCancel(ctx)
|
|
|
|
instance := &routineTracker{
|
2020-11-13 19:56:41 +00:00
|
|
|
cancel: cancel,
|
2022-04-05 18:17:53 +00:00
|
|
|
cancelCh: ctx.Done(),
|
2020-11-13 19:56:41 +00:00
|
|
|
stoppedCh: make(chan struct{}),
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
go m.execute(rtCtx, name, routine, instance.stoppedCh)
|
2019-10-04 17:08:45 +00:00
|
|
|
|
|
|
|
m.routines[name] = instance
|
2020-01-28 23:50:41 +00:00
|
|
|
m.logger.Info("started routine", "routine", name)
|
2019-10-04 17:08:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
// execute will run the given routine in the foreground and close the given channel when its done executing
|
|
|
|
func (m *Manager) execute(ctx context.Context, name string, routine Routine, done chan struct{}) {
|
|
|
|
defer func() {
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := routine(ctx)
|
|
|
|
if err != nil && err != context.DeadlineExceeded && err != context.Canceled {
|
|
|
|
m.logger.Error("routine exited with error",
|
|
|
|
"routine", name,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
} else {
|
2022-04-05 18:17:53 +00:00
|
|
|
m.logger.Info("stopped routine", "routine", name)
|
2021-05-20 14:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 18:17:53 +00:00
|
|
|
// Caveat: The returned stoppedCh indicates that the routine is completed
|
2022-10-21 19:58:06 +00:00
|
|
|
// It's possible that ctx is canceled, but stoppedCh not yet closed
|
|
|
|
// Use mgr.IsRunning(name) than this stoppedCh to tell whether the
|
|
|
|
// instance is still running (not cancelled or completed).
|
2021-05-20 14:07:23 +00:00
|
|
|
func (m *Manager) Stop(name string) <-chan struct{} {
|
2020-11-13 19:56:41 +00:00
|
|
|
instance := m.stopInstance(name)
|
|
|
|
if instance == nil {
|
|
|
|
// Fabricate a closed channel so it won't block forever.
|
|
|
|
ch := make(chan struct{})
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance.stoppedCh
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (m *Manager) stopInstance(name string) *routineTracker {
|
2019-10-04 17:08:45 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
instance, ok := m.routines[name]
|
|
|
|
if !ok {
|
|
|
|
// no running instance
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:56:41 +00:00
|
|
|
if !instance.running() {
|
|
|
|
return instance
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 18:17:53 +00:00
|
|
|
m.logger.Info("stopping routine", "routine", name)
|
2019-10-04 17:08:45 +00:00
|
|
|
instance.cancel()
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2019-10-04 17:08:45 +00:00
|
|
|
delete(m.routines, name)
|
2020-11-13 19:56:41 +00:00
|
|
|
|
|
|
|
return instance
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 22:59:31 +00:00
|
|
|
// StopAll goroutines. Once StopAll is called, it is no longer safe to add no
|
|
|
|
// goroutines to the Manager.
|
2021-05-20 14:07:23 +00:00
|
|
|
func (m *Manager) StopAll() {
|
2019-10-04 17:08:45 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
for name, routine := range m.routines {
|
2020-11-13 19:56:41 +00:00
|
|
|
if !routine.running() {
|
2019-10-04 17:08:45 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-04-05 18:17:53 +00:00
|
|
|
m.logger.Info("stopping routine", "routine", name)
|
2019-10-04 17:08:45 +00:00
|
|
|
routine.cancel()
|
|
|
|
}
|
2021-06-02 22:59:31 +00:00
|
|
|
}
|
2019-10-04 17:08:45 +00:00
|
|
|
|
2021-06-02 22:59:31 +00:00
|
|
|
// Wait for all goroutines to stop after StopAll is called.
|
|
|
|
func (m *Manager) Wait() {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
for _, routine := range m.routines {
|
|
|
|
routine.wait()
|
|
|
|
}
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|