|
| 1 | +package gracegroup |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "golang.org/x/sync/errgroup" |
| 9 | +) |
| 10 | + |
| 11 | +type ( |
| 12 | + // StartFn does not accept context as argument because most of start functions do not need it |
| 13 | + // because then context canceled shutdown functions should be called. |
| 14 | + // If start fn returns error then group will be shutdown, otherwise it won't. |
| 15 | + StartFn func() error |
| 16 | + // ShutdownFn accepts context as argument to handle shutdown timeout and must |
| 17 | + // support handling timed out context. |
| 18 | + // If function returns error it wont affect to other shutdown functions. |
| 19 | + ShutdownFn func(ctx context.Context) error |
| 20 | +) |
| 21 | + |
| 22 | +// Gracegroup is a managare to execute processes and functions to shutdown processes. |
| 23 | +type Group struct { |
| 24 | + mu sync.Mutex |
| 25 | + |
| 26 | + cfg Config |
| 27 | + startFns []StartFn |
| 28 | + shutdownFns []ShutdownFn |
| 29 | +} |
| 30 | + |
| 31 | +func New(cfg Config) *Group { |
| 32 | + return &Group{ |
| 33 | + cfg: cfg, |
| 34 | + startFns: make([]StartFn, 0), |
| 35 | + shutdownFns: make([]ShutdownFn, 0), |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Add adds a start function and a shutdown function to the group. |
| 40 | +// Func does not invoke start func immediately, it will wait for Wait method. |
| 41 | +func (r *Group) Add(start StartFn, shutdown ShutdownFn) { |
| 42 | + r.mu.Lock() |
| 43 | + defer r.mu.Unlock() |
| 44 | + |
| 45 | + r.startFns = append(r.startFns, start) |
| 46 | + r.shutdownFns = append(r.shutdownFns, shutdown) |
| 47 | +} |
| 48 | + |
| 49 | +// Wait does next things: |
| 50 | +// 1. invokes all start functions concurrently, |
| 51 | +// 2. waits one of next condition: |
| 52 | +// a. passed to Wait context is canceled, |
| 53 | +// b. one of start functions returns error, |
| 54 | +// 3. invokes all shutdown functions concurrently and waiting while all of them will be finished. |
| 55 | +// |
| 56 | +// If any of start functions return nil then group won't initiate shutdown. |
| 57 | +// If any of shutdown functions returns error it wont stop shutdown process. Shutdown function |
| 58 | +// must support context DeadlineExceeded error and exit on it. Group does not forcelly stop shutdown |
| 59 | +// then context deadline exceeded. |
| 60 | +// |
| 61 | +// Wait could return error from: |
| 62 | +// 1. one of start functions, |
| 63 | +// 2. one of shutdown functions, |
| 64 | +// 3. error from Wait context if it is not context.Calceled error, |
| 65 | +// 4. context.DeadlineExceeded if cfg.ShutdownTimeout is exceeded on shutdown. |
| 66 | +func (r *Group) Wait(ctx context.Context) error { |
| 67 | + g, ctx := errgroup.WithContext(ctx) |
| 68 | + |
| 69 | + for _, start := range r.startFns { |
| 70 | + g.Go(start) |
| 71 | + } |
| 72 | + |
| 73 | + err := r.wait(ctx, g) |
| 74 | + |
| 75 | + shutdownError := r.shutdown() |
| 76 | + |
| 77 | + return errors.Join(shutdownError, err) |
| 78 | +} |
| 79 | + |
| 80 | +func (r *Group) wait(ctx context.Context, g *errgroup.Group) error { |
| 81 | + done := make(chan struct{}) |
| 82 | + |
| 83 | + go func() { |
| 84 | + //nolint:errcheck,gosec // err will be set on errgroup context cause |
| 85 | + g.Wait() |
| 86 | + |
| 87 | + close(done) |
| 88 | + }() |
| 89 | + |
| 90 | + // no needs set error from ctx or errgroup |
| 91 | + // because errgroup set error cause to context on wait method |
| 92 | + // or argument context has error cause |
| 93 | + select { |
| 94 | + case <-done: |
| 95 | + case <-ctx.Done(): |
| 96 | + } |
| 97 | + |
| 98 | + if err := context.Cause(ctx); !errors.Is(err, context.Canceled) { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func (r *Group) shutdown() error { |
| 106 | + ctx, cancel := context.WithCancel(context.Background()) |
| 107 | + |
| 108 | + if r.cfg.ShutdownTimeout > 0 { |
| 109 | + ctx, cancel = context.WithTimeout(ctx, r.cfg.ShutdownTimeout) |
| 110 | + } |
| 111 | + defer cancel() |
| 112 | + |
| 113 | + g := &errgroup.Group{} |
| 114 | + |
| 115 | + for _, shutdownFn := range r.shutdownFns { |
| 116 | + g.Go(func() error { |
| 117 | + return shutdownFn(ctx) |
| 118 | + }) |
| 119 | + } |
| 120 | + |
| 121 | + return g.Wait() |
| 122 | +} |
0 commit comments