From 19b67d67950453ea5a4f0a45d09709e993b134ec Mon Sep 17 00:00:00 2001 From: Dmytro Nozdrin Date: Tue, 14 Mar 2023 12:51:31 +0200 Subject: [PATCH] Improved memory consumption on timeout checks (#90) Thank you for the contribution! :green_heart: --- health.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/health.go b/health.go index 791dc67..6354578 100644 --- a/health.go +++ b/health.go @@ -200,8 +200,10 @@ func (h *Health) Measure(ctx context.Context) Check { defer close(resCh) }() + timeout := time.NewTimer(c.Timeout) + select { - case <-time.After(c.Timeout): + case <-timeout.C: mu.Lock() defer mu.Unlock() @@ -210,6 +212,10 @@ func (h *Health) Measure(ctx context.Context) Check { failures[c.Name] = string(StatusTimeout) status = getAvailability(status, c.SkipOnErr) case res := <-resCh: + if !timeout.Stop() { + <-timeout.C + } + mu.Lock() defer mu.Unlock()