-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrier.go
72 lines (63 loc) · 2.04 KB
/
retrier.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
package redmine
import (
"fmt"
"net/http"
"time"
redmine "github.com/nixys/nxs-go-redmine/v5"
"github.com/rs/zerolog"
)
const (
// MaxRetries is the maximum number of retries
MaxRetries = 5
// RetryDelay is the delay step between retries
RetryDelay = 5 * time.Second
)
// Retry retries the given function until it succeeds or the maximum number of retries is reached
// it should be used everywhere you call the underlying API object (inside the library: r.cfg.api, outside: r.GetAPI())
func Retry(log *zerolog.Logger, fn func() (redmine.StatusCode, error)) error {
for i := 1; i <= MaxRetries; i++ {
statusCode, err := fn()
if statusCode == http.StatusNotFound {
log.Warn().Msg("issue not found")
return nil
}
if statusCode > 499 {
if i < MaxRetries {
log.Warn().Int("retries", i).Int("status_code", int(statusCode)).Err(err).Msg("retrying")
time.Sleep(RetryDelay * time.Duration(i))
continue
}
log.Warn().Int("retries", i).Int("status_code", int(statusCode)).Err(err).Msg("giving up")
}
if err != nil {
return err
}
return nil
}
return fmt.Errorf("failed after %d retries", MaxRetries)
}
// RetryResult retries the given function until it succeeds or the maximum number of retries is reached
// it should be used everywhere you call the underlying API object (inside the library: r.cfg.api, outside: r.GetAPI())
func RetryResult[V any](log *zerolog.Logger, fn func() (V, redmine.StatusCode, error)) (V, error) {
var zero V
for i := 1; i <= MaxRetries; i++ {
v, statusCode, err := fn()
if statusCode == http.StatusNotFound {
log.Warn().Msg("issue not found")
return zero, nil
}
if statusCode > 499 {
if i < MaxRetries {
log.Warn().Int("retries", i).Int("status_code", int(statusCode)).Err(err).Msg("retrying")
time.Sleep(RetryDelay * time.Duration(i))
continue
}
log.Warn().Int("retries", i).Int("status_code", int(statusCode)).Err(err).Msg("giving up")
}
if err != nil {
return v, err
}
return v, nil
}
return zero, fmt.Errorf("failed after %d retries", MaxRetries)
}