Skip to content

Commit 2bad49b

Browse files
authored
Merge pull request #8 from runreveal/simpler-timeouts
simple timeouts
2 parents 5886615 + 9cb5281 commit 2bad49b

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

await/await.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (r *runner) Run(ctx context.Context) error {
139139

140140
cancel(fmt.Errorf("await: %w", err))
141141

142-
waitOrTimeout(r.stopTimeout, &waitCount)
142+
err = waitOrTimeout(r.stopTimeout, &waitCount, err)
143143

144144
if errors.Is(err, context.Canceled) {
145145
slog.Info("await: stopped on context canceled")
@@ -151,18 +151,18 @@ func (r *runner) Run(ctx context.Context) error {
151151

152152
// waitTimeout will return either when the context is canceled or when the
153153
// counter reaches 0. It will check the counter every 10ms.
154-
func waitOrTimeout(timeout time.Duration, counter *int32) {
155-
ticker := time.NewTicker(10 * time.Millisecond)
156-
for {
157-
select {
158-
case <-time.After(timeout):
159-
slog.Error("await: timed out waiting for subroutines to finish")
160-
case <-ticker.C:
161-
if atomic.LoadInt32(counter) == 0 {
162-
return
163-
}
154+
func waitOrTimeout(timeout time.Duration, counter *int32, err error) error {
155+
slog.Info(fmt.Sprintf("await: waiting %s for subroutines to finish", timeout))
156+
for i := 0 * time.Second; i < timeout; i += 100 * time.Millisecond {
157+
if atomic.LoadInt32(counter) == 0 {
158+
return err
164159
}
160+
time.Sleep(100 * time.Millisecond)
161+
}
162+
if err != nil {
163+
return fmt.Errorf("await: shutdown timeout: %w", err)
165164
}
165+
return nil
166166
}
167167

168168
// ListenAndServe provides a graceful shutdown for an http.Server.

0 commit comments

Comments
 (0)