-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsub_options.go
95 lines (76 loc) · 2.15 KB
/
sub_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
package psub
import (
"context"
"cloud.google.com/go/pubsub"
)
type SubscribeOption struct {
ACKErr *bool
RetrySubscribe *bool // default: true
IsLog *bool //default: false
DeduplicateFunc func(context.Context, *pubsub.Message) (bool, error) // return true if message duplicated
ACKHook func(*pubsub.Message)
NACKHook func(*pubsub.Message)
ReceiveSettings *pubsub.ReceiveSettings
}
func NewSubscribeOption() *SubscribeOption {
retry := true
return &SubscribeOption{
RetrySubscribe: &retry,
}
}
func (s *SubscribeOption) SetACKAll(ack bool) *SubscribeOption {
s.ACKErr = &ack
return s
}
func (s *SubscribeOption) SetRetry(retry bool) *SubscribeOption {
s.RetrySubscribe = &retry
return s
}
func (s *SubscribeOption) SetIsLog(isLog bool) *SubscribeOption {
s.IsLog = &isLog
return s
}
func (s *SubscribeOption) SetDeduplicate(isDuplicateFunc func(context.Context, *pubsub.Message) (bool, error)) *SubscribeOption {
s.DeduplicateFunc = isDuplicateFunc
return s
}
// SetACKHook not apply for deduplicate ack()
func (s *SubscribeOption) SetACKHook(f func(*pubsub.Message)) *SubscribeOption {
s.ACKHook = f
return s
}
func (s *SubscribeOption) SetNACKHook(f func(*pubsub.Message)) *SubscribeOption {
s.NACKHook = f
return s
}
func (s *SubscribeOption) SetReceiveSettings(settings pubsub.ReceiveSettings) *SubscribeOption {
s.ReceiveSettings = &settings
return s
}
func mergeSubscribeOption(opts ...*SubscribeOption) *SubscribeOption {
opt := NewSubscribeOption()
for i := range opts {
if opts[i].ACKErr != nil {
opt.ACKErr = opts[i].ACKErr
}
if opts[i].RetrySubscribe != nil {
opt.RetrySubscribe = opts[i].RetrySubscribe
}
if opts[i].IsLog != nil {
opt.IsLog = opts[i].IsLog
}
if opts[i].DeduplicateFunc != nil {
opt.DeduplicateFunc = opts[i].DeduplicateFunc
}
if opts[i].ACKHook != nil {
opt.ACKHook = opts[i].ACKHook
}
if opts[i].NACKHook != nil {
opt.NACKHook = opts[i].NACKHook
}
if opts[i].ReceiveSettings != nil {
opt.ReceiveSettings = opts[i].ReceiveSettings
}
}
return opt
}