-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandler_opts.go
116 lines (90 loc) · 2.24 KB
/
handler_opts.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
package gilmour
import "sync"
const TIMEOUT = 600
/*
Handler Options to be passed alongside each Handler at time of topic
subscription. The struct allows chaining and can conveniently be used like
x := NewHandlerOpts().SetTimeout(500).SetGroup("hello-world")
*/
type HandlerOpts struct {
group string
timeout int
oneShot bool
_noResponse bool
_isSlot bool
sync.RWMutex
}
// Get the execution timeout for the associated handler. If one was not
// explicitly set Gilmour assumes and also sets a default of 600.
func (h *HandlerOpts) GetTimeout() int {
//Parallel Goroutines will othrwise run into race condition.
h.Lock()
defer h.Unlock()
if h.timeout == 0 {
h.timeout = TIMEOUT
}
return h.timeout
}
// Set the execution timeout for this Handler, otherwise a default of 600
// seconds is assumed.
func (h *HandlerOpts) SetTimeout(t int) *HandlerOpts {
h.Lock()
defer h.Unlock()
h.timeout = t
return h
}
// If the Handler is to be executed as a part of an exclusive Group.
func (h *HandlerOpts) GetGroup() string {
h.RLock()
defer h.RUnlock()
return h.group
}
// Set this handler to be a part of an exclusive Group. At the most one
// handler is executed per exclusive group.
func (h *HandlerOpts) SetGroup(group string) *HandlerOpts {
h.Lock()
defer h.Unlock()
h.group = group
return h
}
// Is this a one shot handler? Read Setter for details.
func (h *HandlerOpts) isOneShot() bool {
h.RLock()
defer h.RUnlock()
return h.oneShot
}
// Set this handler to be executed only once. The subscription would be
// unsubscribed on the very first message delivered to the handler,
// either successfully or unsuccessfully.
func (h *HandlerOpts) setOneShot() *HandlerOpts {
h.Lock()
defer h.Unlock()
h.oneShot = true
return h
}
func (h *HandlerOpts) isSlot() bool {
h.RLock()
defer h.RUnlock()
return h._isSlot
}
func (h *HandlerOpts) sendResponse(ok bool) *HandlerOpts {
h.Lock()
defer h.Unlock()
h._noResponse = !ok
return h
}
func (h *HandlerOpts) shouldSendResponse() bool {
h.RLock()
defer h.RUnlock()
return !h._noResponse
}
func (h *HandlerOpts) setSlot() *HandlerOpts {
h.Lock()
defer h.Unlock()
h._noResponse = true
h._isSlot = true
return h
}
func NewHandlerOpts() *HandlerOpts {
return &HandlerOpts{}
}