-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathretryexecutor.go
109 lines (90 loc) · 2.76 KB
/
retryexecutor.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package utils
import (
"context"
"errors"
"fmt"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"time"
"github.com/jfrog/jfrog-client-go/utils/log"
)
type ExecutionHandlerFunc func() (shouldRetry bool, err error)
type RetryExecutor struct {
// The context
Context context.Context
// The amount of retries to perform.
MaxRetries int
// Number of milliseconds to sleep between retries.
RetriesIntervalMilliSecs int
// Message to display when retrying.
ErrorMessage string
// Prefix to print at the beginning of each log.
LogMsgPrefix string
// ExecutionHandler is the operation to run with retries.
ExecutionHandler ExecutionHandlerFunc
}
func (runner *RetryExecutor) Execute() error {
var err error
var shouldRetry bool
for i := 0; i <= runner.MaxRetries; i++ {
// Run ExecutionHandler
shouldRetry, err = runner.ExecutionHandler()
// If we should not retry, return.
if !shouldRetry {
return err
}
if cancelledErr := runner.checkCancelled(); cancelledErr != nil {
return cancelledErr
}
// Print retry log message
runner.LogRetry(i, err)
// Going to sleep for RetryInterval milliseconds
if runner.RetriesIntervalMilliSecs > 0 && i < runner.MaxRetries {
time.Sleep(time.Millisecond * time.Duration(runner.RetriesIntervalMilliSecs))
}
}
// If the error is not nil, return it and log the timeout message. Otherwise, generate new error.
if err != nil {
log.Info(runner.getTimeoutErrorMsg())
return err
}
return errorutils.CheckError(RetryExecutorTimeoutError{runner.getTimeoutErrorMsg()})
}
// Error of this type will be returned if the executor reaches timeout and no other error is returned by the execution handler.
type RetryExecutorTimeoutError struct {
errMsg string
}
func (retryErr RetryExecutorTimeoutError) Error() string {
return retryErr.errMsg
}
func (runner *RetryExecutor) getTimeoutErrorMsg() string {
prefix := ""
if runner.LogMsgPrefix != "" {
prefix = runner.LogMsgPrefix + " "
}
return fmt.Sprintf("%sexecutor timeout after %v attempts with %v milliseconds wait intervals", prefix, runner.MaxRetries, runner.RetriesIntervalMilliSecs)
}
func (runner *RetryExecutor) LogRetry(attemptNumber int, err error) {
message := fmt.Sprintf("%s(Attempt %v)", runner.LogMsgPrefix, attemptNumber+1)
if runner.ErrorMessage != "" {
message = fmt.Sprintf("%s - %s", message, runner.ErrorMessage)
}
if err != nil {
message = fmt.Sprintf("%s: %s", message, err.Error())
}
if err != nil || runner.ErrorMessage != "" {
log.Warn(message)
} else {
log.Debug(message)
}
}
func (runner *RetryExecutor) checkCancelled() error {
if runner.Context == nil {
return nil
}
contextErr := runner.Context.Err()
if errors.Is(contextErr, context.Canceled) {
log.Info("Retry executor was cancelled")
return contextErr
}
return nil
}