-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddleware_sink_retry.go
56 lines (50 loc) · 1.61 KB
/
middleware_sink_retry.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
package pipeline
import (
"context"
"time"
"github.com/arquivei/foundationkit/errors"
"github.com/arquivei/foundationkit/retrier"
"github.com/rs/zerolog/log"
)
// SinkWithRetry decorates a sink with a retrier.
// It uses an exponential backoff and tries for 5 times.
// Only runtime errors are retried.
func SinkWithRetry(next Sink) Sink {
r := retrier.NewRetrier(retrier.Settings{
RetryEvaluator: retrier.NewGenericRetryEvaluator(retrier.GenericRetryEvaluatorSettings{
ErrorsSeveritiesPolicy: retrier.EvaluationPolicyWhitelist,
ErrorsSeverities: []errors.Severity{errors.SeverityRuntime},
}),
BackoffCalculator: retrier.NewExponentialBackoffCalculator(retrier.ExponentialBackoffCalculatorSettings{
BaseBackoff: 2000 * time.Millisecond,
RandomExtraBackoff: 250 * time.Millisecond,
Multiplier: 2.0,
}),
ErrorWrapper: retrier.NewLastErrorWrapper(),
})
return &sinkRetrier{
next: next,
retrier: r,
}
}
type sinkRetrier struct {
next Sink
retrier *retrier.Retrier
}
func (s *sinkRetrier) Store(ctx context.Context, input ...SinkMessage) error {
return s.retrier.ExecuteOperation(func() error {
// context is canceled, just abort the operation
// Because this error is not a runtime error, it will not be retried
if err := ctx.Err(); err != nil {
return err
}
if err := s.next.Store(ctx, input...); err != nil {
// Logs only runtime errors so we can observe individual fails
if errors.GetSeverity(err) == errors.SeverityRuntime {
log.Ctx(ctx).Warn().Err(err).Msg("[goduck][pipeline] Failed to send message to sink.")
}
return err
}
return nil
})
}