/
retry_policy.go
63 lines (54 loc) · 1.95 KB
/
retry_policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2024 H0llyW00dzZ
//
// License: MIT License
package terminal
import (
"fmt"
"math"
"strings"
"time"
)
// retryWithExponentialBackoff attempts to execute the RetryableFunc with a retry policy.
// It applies exponential backoff between retries and logs an error if the maximum number of retries is reached.
//
// Note: this a powerful retry policy, unlike that shitty complex go codes
func (op *RetryableOperation) retryWithExponentialBackoff(handleError ErrorHandlerFunc) (bool, error) {
const maxRetries = 3
baseDelay := time.Second
var lastErr error // Variable to store the last error encountered
for attempt := 0; attempt < maxRetries; attempt++ {
success, err := op.retryFunc()
if err == nil {
return success, nil
}
lastErr = err // Store the last error encountered
// Log debug information
logger.Debug(DEBUGRETRYPOLICY, attempt+1, err)
// Use the provided error handler to check if we should retry.
if handleError(err) {
delay := baseDelay * time.Duration(math.Pow(2, float64(attempt)))
time.Sleep(delay)
// Log the retry attempt number and the last error message
logger.Any(RetryingStupid500Error, lastErr, attempt+1)
continue // Retry the request
} else {
// Non-retryable error or max retries exceeded
logger.Error(ErrorNonretryableerror, err)
return false, err
}
}
// If this point is reached, retries have been exhausted without success.
// Use the last error encountered in the final error message.
err := fmt.Errorf(ErrorLowLevelMaximumRetries, lastErr)
return false, err
}
// standardAPIErrorHandler is the standard error handling strategy for API errors.
func standardAPIErrorHandler(err error) bool {
// Error 500 Google Api
return strings.Contains(err.Error(), Error500GoogleAPI)
}
// standardOtherAPIErrorHandler is the standard error handling strategy for API errors.
func standardOtherAPIErrorHandler(err error) bool {
// Error 500 Other Api
return strings.Contains(err.Error(), Code500)
}