-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsubscriber_options.go
107 lines (94 loc) · 3.07 KB
/
subscriber_options.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
package amqp
import (
"time"
"github.com/devimteam/amqp/logger"
)
type subscriberOptions struct {
channelBuffer int
wait struct {
flag bool
timeout time.Duration
}
workers int
processAll bool
log logger.Logger
msgOpts subMessageOptions
errorBefore []ErrorBefore
observerOpts []ObserverOption
}
func defaultSubOptions() subscriberOptions {
opts := subscriberOptions{}
opts.processAll = false
opts.wait.flag = true
opts.wait.timeout = defaultWaitDeadline
opts.channelBuffer = defaultEventBuffer
opts.msgOpts.minPriority = MinMessagePriority
opts.msgOpts.maxPriority = MaxMessagePriority
opts.msgOpts.defaultContentType = defaultContentType
opts.workers = defaultWorkers
opts.log = logger.NoopLogger
return opts
}
// WaitConnection tells client to wait connection before Subscription or Pub executing.
func SubscriberWaitConnection(should bool, timeout time.Duration) SubscriberOption {
return func(subscriber *Subscriber) {
subscriber.opts.wait.flag = should
if timeout != 0 {
subscriber.opts.wait.timeout = timeout
}
}
}
// EventChanBuffer sets the buffer of event channel for Subscription method.
func SubscriberBufferSize(a int) SubscriberOption {
return func(subscriber *Subscriber) {
subscriber.opts.channelBuffer = a
}
}
// AllowedPriority rejects messages, which not in range.
func SubscriberAllowedPriority(from, to uint8) SubscriberOption {
return func(subscriber *Subscriber) {
subscriber.opts.msgOpts.minPriority = from
subscriber.opts.msgOpts.maxPriority = to
}
}
// SubscriberLogger option sets logger, which logs error messages.
func SubscriberLogger(lg logger.Logger) SubscriberOption {
return func(subscriber *Subscriber) {
subscriber.opts.log = lg
}
}
// DeliverBefore adds functions, that should be called before sending Event to channel.
func SubscriberDeliverBefore(before ...DeliveryBefore) SubscriberOption {
return func(subscriber *Subscriber) {
for i := range before {
subscriber.opts.msgOpts.deliveryBefore = append(subscriber.opts.msgOpts.deliveryBefore, before[i])
}
}
}
// Add this option with true value that allows you to handle all deliveries from current channel, even if the Done was sent.
func SubProcessAllDeliveries(v bool) SubscriberOption {
return func(subscriber *Subscriber) {
subscriber.opts.processAll = v
}
}
// HandlersAmount sets the amount of handle processes, which receive deliveries from one channel.
// For n > 1 client does not guarantee the order of events.
func SubHandlersAmount(n int) SubscriberOption {
return func(subscriber *Subscriber) {
if n > 0 {
subscriber.opts.workers = n
}
}
}
// SetDefaultContentType sets content type which codec should be used if ContentType field of message is empty.
// JSON is used by default.
func SubSetDefaultContentType(t string) SubscriberOption {
return func(subscriber *Subscriber) {
subscriber.opts.msgOpts.defaultContentType = t
}
}
func SubWithObserverOptions(opts ...ObserverOption) SubscriberOption {
return func(subscriber *Subscriber) {
subscriber.opts.observerOpts = append(subscriber.opts.observerOpts, opts...)
}
}