From 0253ab78b454d90fe47e76d6c8ea652ec33e9cf8 Mon Sep 17 00:00:00 2001 From: Marc Cirauqui Date: Thu, 30 May 2024 11:52:07 +0200 Subject: [PATCH] use channel --- pkg/helpers/retry.go | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/pkg/helpers/retry.go b/pkg/helpers/retry.go index 375eb4d..0f12347 100644 --- a/pkg/helpers/retry.go +++ b/pkg/helpers/retry.go @@ -42,27 +42,24 @@ func (r *RetryWindow) Do(action func(attempt int, successes int) bool) RetryResu successCount := 0 // run a while true loop, exiting when the timeout expires for { - attempt++ - if action(attempt, successCount) { - successCount++ - if successCount >= r.ConsecutiveSuccesses { - success <- struct{}{} - return - } - } else { - successCount = 0 - } - if r.Context != nil { - if err := r.Context.Err(); err != nil { - if err == context.Canceled || err == context.DeadlineExceeded { - failure <- struct{}{} + select { + case <-r.Context.Done(): + failure <- struct{}{} + return + default: + attempt++ + if action(attempt, successCount) { + successCount++ + if successCount >= r.ConsecutiveSuccesses { + success <- struct{}{} return } + } else { + successCount = 0 } + time.Sleep(r.Interval) } - time.Sleep(r.Interval) } - }() select {