-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathregistry.go
187 lines (145 loc) · 3.22 KB
/
registry.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package vega
import (
"sync"
"time"
"github.com/vektra/errors"
)
type Registry struct {
sync.Mutex
mailboxes map[string]Mailbox
creator func(string) Mailbox
}
func NewRegistry(create func(string) Mailbox) *Registry {
return &Registry{
mailboxes: make(map[string]Mailbox),
creator: create,
}
}
func NewMemRegistry() *Registry {
return NewRegistry(NewMemMailbox)
}
func (r *Registry) Poll(name string) (*Delivery, error) {
r.Lock()
defer r.Unlock()
if mailbox, ok := r.mailboxes[name]; ok {
msg, err := mailbox.Poll()
if err != nil {
return nil, err
}
if msg == nil {
return nil, nil
}
return NewDelivery(mailbox, msg), nil
}
return nil, nil
}
func (r *Registry) LongPoll(name string, til time.Duration) (*Delivery, error) {
r.Lock()
mailbox, ok := r.mailboxes[name]
if !ok {
debugf("missing mailbox %s\n", name)
r.Unlock()
return nil, errors.Subject(ENoMailbox, name)
}
debugf("long polling %s: %#v\n", name, mailbox)
val, err := mailbox.Poll()
if err != nil {
r.Unlock()
return nil, err
}
if val != nil {
r.Unlock()
return NewDelivery(mailbox, val), nil
}
indicator := mailbox.AddWatcher()
r.Unlock()
select {
case val := <-indicator:
if val == nil {
return nil, nil
}
return NewDelivery(mailbox, val), nil
case <-time.Tick(til):
return nil, nil
}
}
func (r *Registry) LongPollCancelable(name string, til time.Duration, done chan struct{}) (*Delivery, error) {
r.Lock()
mailbox, ok := r.mailboxes[name]
if !ok {
debugf("missing mailbox %s\n", name)
r.Unlock()
return nil, errors.Subject(ENoMailbox, name)
}
debugf("long polling %s: %#v\n", name, mailbox)
val, err := mailbox.Poll()
if err != nil {
r.Unlock()
return nil, err
}
if val != nil {
r.Unlock()
return NewDelivery(mailbox, val), nil
}
indicator := mailbox.AddWatcherCancelable(done)
r.Unlock()
select {
case <-done:
// It's possible for both done and indicator to have values.
// So we need to also check if there a value in indicator and
// if so, pull it out and nack it.
select {
case val := <-indicator:
if val != nil {
mailbox.Nack(val.MessageId)
}
default:
}
return nil, nil
case val := <-indicator:
// It's possible for both done and indicator to have values.
// So we need to also check if there a value in indicator and
// if so, pull it out and nack it.
select {
case <-done:
if val != nil {
mailbox.Nack(val.MessageId)
return nil, nil
}
default:
}
if val == nil {
return nil, nil
}
return NewDelivery(mailbox, val), nil
case <-time.Tick(til):
return nil, nil
}
}
var ENoMailbox = errors.New("No such mailbox available")
func (r *Registry) Push(name string, value *Message) error {
r.Lock()
defer r.Unlock()
if mailbox, ok := r.mailboxes[name]; ok {
return mailbox.Push(value)
}
return errors.Subject(ENoMailbox, name)
}
func (r *Registry) Declare(name string) error {
r.Lock()
defer r.Unlock()
debugf("declaring mailbox: '%s'\n", name)
if _, ok := r.mailboxes[name]; !ok {
r.mailboxes[name] = r.creator(name)
}
return nil
}
func (r *Registry) Abandon(name string) error {
r.Lock()
defer r.Unlock()
if m, ok := r.mailboxes[name]; ok {
m.Abandon()
delete(r.mailboxes, name)
}
return nil
}