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.
This commit is contained in:
parent
e044aae1df
commit
3ccc8d67f4
|
@ -47,14 +47,12 @@ func getRetryAfter(resp *http.Response) (time.Duration, bool) {
|
||||||
return time.Duration(seconds) * time.Second, true
|
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)
|
timer := time.NewTimer(duration)
|
||||||
defer timer.Stop()
|
defer timer.Stop()
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return false
|
|
||||||
case <-timer.C:
|
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 {
|
func (c *LogClient) doAndParse(ctx context.Context, method string, uri string, reqBody interface{}, respBody interface{}) error {
|
||||||
numRetries := 0
|
numRetries := 0
|
||||||
retry:
|
retry:
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
req, err := c.makeRequest(ctx, method, uri, reqBody)
|
req, err := c.makeRequest(ctx, method, uri, reqBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%s %s: error creating request: %w", method, uri, err)
|
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 {
|
func (c *LogClient) shouldRetry(ctx context.Context, numRetries int, resp *http.Response) bool {
|
||||||
if ctx.Err() != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if numRetries == maxRetries {
|
if numRetries == maxRetries {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -247,7 +244,8 @@ func (c *LogClient) shouldRetry(ctx context.Context, numRetries int, resp *http.
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return sleep(ctx, delay)
|
sleep(ctx, delay)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSTH retrieves the current STH from the log.
|
// GetSTH retrieves the current STH from the log.
|
||||||
|
|
Loading…
Reference in New Issue