This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
/
client.go
342 lines (248 loc) · 4.72 KB
/
client.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package nats
import (
"net"
"sync"
)
type Subscription struct {
sr *subscriptionRegistry
sid uint
frozen bool
maximum uint
received uint
subject string
queue string
Inbox chan *readMessage
}
func (s *Subscription) freeze() {
if s.frozen {
panic("subscription is frozen")
}
s.frozen = true
}
func (s *Subscription) SetSubject(v string) {
if s.frozen {
panic("subscription is frozen")
}
s.subject = v
}
func (s *Subscription) SetQueue(v string) {
if s.frozen {
panic("subscription is frozen")
}
s.queue = v
}
func (s *Subscription) SetMaximum(v uint) {
if s.frozen {
panic("subscription is frozen")
}
s.maximum = v
}
func (s *Subscription) writeSubscribe() writeObject {
var o = new(writeSubscribe)
o.Sid = s.sid
o.Subject = s.subject
o.Queue = s.queue
return o
}
func (s *Subscription) writeUnsubscribe(includeMaximum bool) writeObject {
var o = new(writeUnsubscribe)
o.Sid = s.sid
if includeMaximum {
o.Maximum = s.maximum
}
return o
}
func (s *Subscription) Subscribe() {
s.sr.Subscribe(s)
}
func (s *Subscription) Unsubscribe() {
s.sr.Unsubscribe(s)
}
func (s *Subscription) deliver(m *readMessage) {
s.received++
s.Inbox <- m
// Unsubscribe if the maximum number of messages has been received
if s.maximum > 0 && s.received >= s.maximum {
s.Unsubscribe()
}
}
type subscriptionRegistry struct {
sync.Mutex
*Client
sid uint
m map[uint]*Subscription
}
func (sr *subscriptionRegistry) emptyMap() {
sr.m = make(map[uint]*Subscription)
}
func (sr *subscriptionRegistry) setup(c *Client) {
sr.Client = c
sr.emptyMap()
}
func (sr *subscriptionRegistry) teardown() {
sr.Lock()
defer sr.Unlock()
for _, s := range sr.m {
close(s.Inbox)
}
sr.emptyMap()
}
func (sr *subscriptionRegistry) NewSubscription(sub string) *Subscription {
var s = new(Subscription)
sr.Lock()
s.sr = sr
sr.sid++
s.sid = sr.sid
sr.Unlock()
s.SetSubject(sub)
s.Inbox = make(chan *readMessage)
return s
}
func (sr *subscriptionRegistry) Subscribe(s *Subscription) {
sr.Lock()
sr.m[s.sid] = s
s.freeze()
sr.Unlock()
sr.Client.Write(s.writeSubscribe())
if s.maximum > 0 {
sr.Client.Write(s.writeUnsubscribe(true))
}
return
}
func (sr *subscriptionRegistry) Unsubscribe(s *Subscription) {
sr.Lock()
delete(sr.m, s.sid)
// Since this subscription is now removed from the registry, it will no
// longer receive messages and the inbox can be closed
close(s.Inbox)
sr.Unlock()
sr.Client.Write(s.writeUnsubscribe(false))
return
}
func (sr *subscriptionRegistry) deliver(m *readMessage) {
var s *Subscription
var ok bool
sr.Lock()
s, ok = sr.m[m.SubscriptionId]
sr.Unlock()
if ok {
s.deliver(m)
}
}
type Client struct {
subscriptionRegistry
Addr string
cc chan *Connection
// Notify running client to stop
sc chan bool
}
func NewClient(addr string) *Client {
var t = new(Client)
t.subscriptionRegistry.setup(t)
t.Addr = addr
t.cc = make(chan *Connection)
t.sc = make(chan bool)
return t
}
func (t *Client) Write(o writeObject) bool {
var c *Connection
var ok bool
c, ok = <-t.cc
if !ok {
return false
}
return c.Write(o)
}
func (t *Client) Ping() bool {
var c *Connection
var ok bool
c, ok = <-t.cc
if !ok {
return false
}
return c.Ping()
}
func (t *Client) publish(s string, m []byte, confirm bool) bool {
var o = new(writePublish)
var c *Connection
var ok bool
o.Subject = s
o.Message = m
c, ok = <-t.cc
if !ok {
return false
}
ok = c.Write(o)
if !ok {
return false
}
// Round trip to confirm the publish was received
if confirm {
return c.Ping()
}
return true
}
func (t *Client) Publish(s string, m []byte) bool {
return t.publish(s, m, false)
}
func (t *Client) PublishAndConfirm(s string, m []byte) bool {
return t.publish(s, m, true)
}
func (t *Client) Stop() {
t.sc <- true
}
func (t *Client) runConnection(n net.Conn) error {
var e error
var c *Connection
var dc chan bool
c = NewConnection(n)
dc = make(chan bool)
// Feed connection until stop
go func() {
for {
select {
case t.cc <- c:
case <-t.sc:
c.Stop()
// Wait for c.Run() to return and notify dc
<-dc
return
case <-dc:
return
}
}
}()
// Read messages until EOF
go func() {
var o readObject
for o = range c.oc {
switch oo := o.(type) {
case *readMessage:
t.deliver(oo)
}
}
}()
e = c.Run()
dc <- true
close(t.cc)
return e
}
func (t *Client) Run(d Dialer, h Handshaker) error {
var n net.Conn
var e error
n, e = d.Dial()
if e != nil {
return e
}
n, e = h.Handshake(n)
if e != nil {
return e
}
e = t.runConnection(n)
if e != nil {
return e
}
// No error means the client was stopped, and we should close all subscriptions
t.subscriptionRegistry.teardown()
return nil
}