From 3ccc8d67f4dca686eacf4567572924964b40e619 Mon Sep 17 00:00:00 2001 From: Andrew Ayer Date: Fri, 3 Feb 2023 17:25:12 -0500 Subject: [PATCH] Improve handling of contexts when retrying requests Previously, if the context was canceled while sleeping, we'd return the last HTTP error. Now, we return the context error instead. --- ct/client/logclient.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ct/client/logclient.go b/ct/client/logclient.go index b4009f6..9e6c86f 100644 --- a/ct/client/logclient.go +++ b/ct/client/logclient.go @@ -47,14 +47,12 @@ func getRetryAfter(resp *http.Response) (time.Duration, bool) { return time.Duration(seconds) * time.Second, true } -func sleep(ctx context.Context, duration time.Duration) bool { +func sleep(ctx context.Context, duration time.Duration) { timer := time.NewTimer(duration) defer timer.Stop() select { case <-ctx.Done(): - return false case <-timer.C: - return true } } @@ -184,6 +182,9 @@ func (c *LogClient) makeRequest(ctx context.Context, method string, uri string, func (c *LogClient) doAndParse(ctx context.Context, method string, uri string, reqBody interface{}, respBody interface{}) error { numRetries := 0 retry: + if ctx.Err() != nil { + return ctx.Err() + } req, err := c.makeRequest(ctx, method, uri, reqBody) if err != nil { return fmt.Errorf("%s %s: error creating request: %w", method, uri, err) @@ -220,10 +221,6 @@ retry: } func (c *LogClient) shouldRetry(ctx context.Context, numRetries int, resp *http.Response) bool { - if ctx.Err() != nil { - return false - } - if numRetries == maxRetries { return false } @@ -247,7 +244,8 @@ func (c *LogClient) shouldRetry(ctx context.Context, numRetries int, resp *http. return false } - return sleep(ctx, delay) + sleep(ctx, delay) + return true } // GetSTH retrieves the current STH from the log.