-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
128 lines (104 loc) · 2.59 KB
/
errors.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2022 Charalampos Mitsakis (go.mitsakis.org/workerpool)
// Licensed under the Apache License, Version 2.0
package workerpool
import (
"errors"
"time"
)
type behavior interface {
Retryable() bool
Unaccounted() bool
}
// errorIsRetryable returns the retryability of an error.
func errorIsRetryable(err error) bool {
var errBehavior behavior
if errors.As(err, &errBehavior) {
return errBehavior.Retryable()
}
return false
}
type retryable struct {
Err error
}
func (err retryable) Error() string {
return err.Err.Error()
}
func (err retryable) Unwrap() error {
return err.Err
}
func (err retryable) Retryable() bool {
return true
}
func (err retryable) Unaccounted() bool {
return false
}
// ErrorWrapRetryable marks an error as retryable.
func ErrorWrapRetryable(err error) error {
if err == nil {
return nil
}
return &retryable{Err: err}
}
func errorIsUnaccounted(err error) bool {
var errBehavior behavior
if errors.As(err, &errBehavior) {
return errBehavior.Unaccounted()
}
return false
}
type retryableUnaccounted struct {
Err error
}
func (err retryableUnaccounted) Error() string {
return err.Err.Error()
}
func (err retryableUnaccounted) Unwrap() error {
return err.Err
}
func (err retryableUnaccounted) Retryable() bool {
return true
}
func (err retryableUnaccounted) Unaccounted() bool {
return true
}
// ErrorWrapRetryableUnaccounted marks an error as retryable and unaccounted.
// This means the pool will keep retrying the job indefinitely,
// without incrementing the attempt counter of the job.
func ErrorWrapRetryableUnaccounted(err error) error {
if err == nil {
return nil
}
return &retryableUnaccounted{Err: err}
}
type errorWorkerBehavior interface {
PauseWorker() time.Duration
}
func errorPausesWorker(err error) time.Duration {
var errWorkerBehavior errorWorkerBehavior
if errors.As(err, &errWorkerBehavior) {
return errWorkerBehavior.PauseWorker()
}
return 0
}
type errorTypePauseWorker struct {
Err error
duration time.Duration
}
func (err errorTypePauseWorker) Error() string {
return err.Err.Error()
}
func (err errorTypePauseWorker) Unwrap() error {
return err.Err
}
func (err errorTypePauseWorker) PauseWorker() time.Duration {
return err.duration
}
// ErrorWrapPauseWorker stops the worker for at least the given duration of time,
// and starts another worker so concurrency will not decrease.
// After the pause duration has passed, the worker can start again if it is needed.
func ErrorWrapPauseWorker(dur time.Duration, err error) error {
if err == nil {
return nil
}
return &errorTypePauseWorker{Err: err, duration: dur}
}