-
Notifications
You must be signed in to change notification settings - Fork 69
/
option.go
66 lines (56 loc) · 1.6 KB
/
option.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
package async
import (
"time"
"github.com/mantzas/patron/errors"
"github.com/mantzas/patron/log"
)
// FailStrategy type definition.
type FailStrategy int
const (
// NackExitStrategy does not acknowledge the message and exits the application on error.
NackExitStrategy FailStrategy = 0
// NackStrategy does not acknowledge the message, leaving it for reprocessing, and continues.
NackStrategy FailStrategy = 1
// AckStrategy acknowledges message and continues.
AckStrategy FailStrategy = 2
)
func (fs FailStrategy) String() string {
switch fs {
case NackExitStrategy:
return "NackExitStrategy"
case NackStrategy:
return "NackStrategy"
case AckStrategy:
return "AckStrategy"
default:
return "N/A"
}
}
// OptionFunc definition for configuring the component in a functional way.
type OptionFunc func(*Component) error
// FailureStrategy option for setting the strategy of handling failures in the async component.
func FailureStrategy(fs FailStrategy) OptionFunc {
return func(c *Component) error {
if fs > AckStrategy || fs < NackExitStrategy {
return errors.New("invalid strategy provided")
}
c.failStrategy = fs
log.Info("failure strategy set")
return nil
}
}
// ConsumerRetry set's the parameters for the retry policy of the consumer.
func ConsumerRetry(retries int, retryWait time.Duration) OptionFunc {
return func(c *Component) error {
if retries < 0 {
return errors.New("invalid retries provided")
}
if retryWait < 0 {
return errors.New("invalid retry wait provided")
}
c.retries = retries
c.retryWait = retryWait
log.Info("consumer retry set")
return nil
}
}