forked from zentures/surgemq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
449 lines (359 loc) · 11.4 KB
/
service.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright (c) 2014 The SurgeMQ Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"fmt"
"io"
"sync"
"sync/atomic"
"github.com/surge/glog"
"github.com/frickelblog/message"
"github.com/frickelblog/surgemq/sessions"
"github.com/frickelblog/surgemq/topics"
)
type (
OnCompleteFunc func(msg, ack message.Message, err error) error
OnPublishFunc func(msg *message.PublishMessage) error
)
type stat struct {
bytes int64
msgs int64
}
func (this *stat) increment(n int64) {
atomic.AddInt64(&this.bytes, n)
atomic.AddInt64(&this.msgs, 1)
}
var (
gsvcid uint64 = 0
)
type service struct {
// The ID of this service, it's not related to the Client ID, just a number that's
// incremented for every new service.
id uint64
// Is this a client or server. It's set by either Connect (client) or
// HandleConnection (server).
client bool
// The number of seconds to keep the connection live if there's no data.
// If not set then default to 5 mins.
keepAlive int
// The number of seconds to wait for the CONNACK message before disconnecting.
// If not set then default to 2 seconds.
connectTimeout int
// The number of seconds to wait for any ACK messages before failing.
// If not set then default to 20 seconds.
ackTimeout int
// The number of times to retry sending a packet if ACK is not received.
// If no set then default to 3 retries.
timeoutRetries int
// Network connection for this service
conn io.Closer
// Session manager for tracking all the clients
sessMgr *sessions.Manager
// Topics manager for all the client subscriptions
topicsMgr *topics.Manager
// sess is the session object for this MQTT session. It keeps track session variables
// such as ClientId, KeepAlive, Username, etc
sess *sessions.Session
// Wait for the various goroutines to finish starting and stopping
wgStarted sync.WaitGroup
wgStopped sync.WaitGroup
// writeMessage mutex - serializes writes to the outgoing buffer.
wmu sync.Mutex
// Whether this is service is closed or not.
closed int64
// Quit signal for determining when this service should end. If channel is closed,
// then exit.
done chan struct{}
// Incoming data buffer. Bytes are read from the connection and put in here.
in *buffer
// Outgoing data buffer. Bytes written here are in turn written out to the connection.
out *buffer
// onpub is the method that gets added to the topic subscribers list by the
// processSubscribe() method. When the server finishes the ack cycle for a
// PUBLISH message, it will call the subscriber, which is this method.
//
// For the server, when this method is called, it means there's a message that
// should be published to the client on the other end of this connection. So we
// will call publish() to send the message.
onpub OnPublishFunc
inStat stat
outStat stat
intmp []byte
outtmp []byte
subs []interface{}
qoss []byte
rmsgs []*message.PublishMessage
}
func (this *service) start() error {
var err error
// Create the incoming ring buffer
this.in, err = newBuffer(defaultBufferSize)
if err != nil {
return err
}
// Create the outgoing ring buffer
this.out, err = newBuffer(defaultBufferSize)
if err != nil {
return err
}
// If this is a server
if !this.client {
// Creat the onPublishFunc so it can be used for published messages
this.onpub = func(msg *message.PublishMessage) error {
if err := this.publish(msg, nil); err != nil {
glog.Errorf("service/onPublish: Error publishing message: %v", err)
return err
}
return nil
}
// If this is a recovered session, then add any topics it subscribed before
topics, qoss, err := this.sess.Topics()
if err != nil {
return err
} else {
for i, t := range topics {
this.topicsMgr.Subscribe([]byte(t), qoss[i], &this.onpub)
}
}
}
// Processor is responsible for reading messages out of the buffer and processing
// them accordingly.
this.wgStarted.Add(1)
this.wgStopped.Add(1)
go this.processor()
// Receiver is responsible for reading from the connection and putting data into
// a buffer.
this.wgStarted.Add(1)
this.wgStopped.Add(1)
go this.receiver()
// Sender is responsible for writing data in the buffer into the connection.
this.wgStarted.Add(1)
this.wgStopped.Add(1)
go this.sender()
// Wait for all the goroutines to start before returning
this.wgStarted.Wait()
return nil
}
// FIXME: The order of closing here causes panic sometimes. For example, if receiver
// calls this, and closes the buffers, somehow it causes buffer.go:476 to panid.
func (this *service) stop() {
defer func() {
// Let's recover from panic
if r := recover(); r != nil {
glog.Errorf("(%s) Recovering from panic: %v", this.cid(), r)
}
}()
doit := atomic.CompareAndSwapInt64(&this.closed, 0, 1)
if !doit {
return
}
// Close quit channel, effectively telling all the goroutines it's time to quit
if this.done != nil {
glog.Debugf("(%s) closing this.done", this.cid())
close(this.done)
}
// Close the network connection
if this.conn != nil {
glog.Debugf("(%s) closing this.conn", this.cid())
this.conn.Close()
}
this.in.Close()
this.out.Close()
// Wait for all the goroutines to stop.
this.wgStopped.Wait()
glog.Debugf("(%s) Received %d bytes in %d messages.", this.cid(), this.inStat.bytes, this.inStat.msgs)
glog.Debugf("(%s) Sent %d bytes in %d messages.", this.cid(), this.outStat.bytes, this.outStat.msgs)
// Unsubscribe from all the topics for this client, only for the server side though
if !this.client && this.sess != nil {
topics, _, err := this.sess.Topics()
if err != nil {
glog.Errorf("(%s/%d): %v", this.cid(), this.id, err)
} else {
for _, t := range topics {
if err := this.topicsMgr.Unsubscribe([]byte(t), &this.onpub); err != nil {
glog.Errorf("(%s): Error unsubscribing topic %q: %v", this.cid(), t, err)
}
}
}
}
// Publish will message if WillFlag is set. Server side only.
if !this.client && this.sess.Cmsg.WillFlag() {
glog.Infof("(%s) service/stop: connection unexpectedly closed. Sending Will.", this.cid())
this.onPublish(this.sess.Will)
}
// Remove the client topics manager
if this.client {
topics.Unregister(this.sess.ID())
}
// Remove the session from session store if it's suppose to be clean session
if this.sess.Cmsg.CleanSession() && this.sessMgr != nil {
this.sessMgr.Del(this.sess.ID())
}
this.conn = nil
this.in = nil
this.out = nil
}
func (this *service) publish(msg *message.PublishMessage, onComplete OnCompleteFunc) error {
//glog.Debugf("service/publish: Publishing %s", msg)
_, err := this.writeMessage(msg)
if err != nil {
return fmt.Errorf("(%s) Error sending %s message: %v", this.cid(), msg.Name(), err)
}
switch msg.QoS() {
case message.QosAtMostOnce:
if onComplete != nil {
return onComplete(msg, nil, nil)
}
return nil
case message.QosAtLeastOnce:
return this.sess.Pub1ack.Wait(msg, onComplete)
case message.QosExactlyOnce:
return this.sess.Pub2out.Wait(msg, onComplete)
}
return nil
}
func (this *service) subscribe(msg *message.SubscribeMessage, onComplete OnCompleteFunc, onPublish OnPublishFunc) error {
if onPublish == nil {
return fmt.Errorf("onPublish function is nil. No need to subscribe.")
}
_, err := this.writeMessage(msg)
if err != nil {
return fmt.Errorf("(%s) Error sending %s message: %v", this.cid(), msg.Name(), err)
}
var onc OnCompleteFunc = func(msg, ack message.Message, err error) error {
onComplete := onComplete
onPublish := onPublish
if err != nil {
if onComplete != nil {
return onComplete(msg, ack, err)
}
return err
}
sub, ok := msg.(*message.SubscribeMessage)
if !ok {
if onComplete != nil {
return onComplete(msg, ack, fmt.Errorf("Invalid SubscribeMessage received"))
}
return nil
}
suback, ok := ack.(*message.SubackMessage)
if !ok {
if onComplete != nil {
return onComplete(msg, ack, fmt.Errorf("Invalid SubackMessage received"))
}
return nil
}
if sub.PacketId() != suback.PacketId() {
if onComplete != nil {
return onComplete(msg, ack, fmt.Errorf("Sub and Suback packet ID not the same. %d != %d.", sub.PacketId(), suback.PacketId()))
}
return nil
}
retcodes := suback.ReturnCodes()
topics := sub.Topics()
if len(topics) != len(retcodes) {
if onComplete != nil {
return onComplete(msg, ack, fmt.Errorf("Incorrect number of return codes received. Expecting %d, got %d.", len(topics), len(retcodes)))
}
return nil
}
var err2 error = nil
for i, t := range topics {
c := retcodes[i]
if c == message.QosFailure {
err2 = fmt.Errorf("Failed to subscribe to '%s'\n%v", string(t), err2)
} else {
this.sess.AddTopic(string(t), c)
_, err := this.topicsMgr.Subscribe(t, c, &onPublish)
if err != nil {
err2 = fmt.Errorf("Failed to subscribe to '%s' (%v)\n%v", string(t), err, err2)
}
}
}
if onComplete != nil {
return onComplete(msg, ack, err2)
}
return err2
}
return this.sess.Suback.Wait(msg, onc)
}
func (this *service) unsubscribe(msg *message.UnsubscribeMessage, onComplete OnCompleteFunc) error {
_, err := this.writeMessage(msg)
if err != nil {
return fmt.Errorf("(%s) Error sending %s message: %v", this.cid(), msg.Name(), err)
}
var onc OnCompleteFunc = func(msg, ack message.Message, err error) error {
onComplete := onComplete
if err != nil {
if onComplete != nil {
return onComplete(msg, ack, err)
}
return err
}
unsub, ok := msg.(*message.UnsubscribeMessage)
if !ok {
if onComplete != nil {
return onComplete(msg, ack, fmt.Errorf("Invalid UnsubscribeMessage received"))
}
return nil
}
unsuback, ok := ack.(*message.UnsubackMessage)
if !ok {
if onComplete != nil {
return onComplete(msg, ack, fmt.Errorf("Invalid UnsubackMessage received"))
}
return nil
}
if unsub.PacketId() != unsuback.PacketId() {
if onComplete != nil {
return onComplete(msg, ack, fmt.Errorf("Unsub and Unsuback packet ID not the same. %d != %d.", unsub.PacketId(), unsuback.PacketId()))
}
return nil
}
var err2 error = nil
for _, tb := range unsub.Topics() {
// Remove all subscribers, which basically it's just this client, since
// each client has it's own topic tree.
err := this.topicsMgr.Unsubscribe(tb, nil)
if err != nil {
err2 = fmt.Errorf("%v\n%v", err2, err)
}
this.sess.RemoveTopic(string(tb))
}
if onComplete != nil {
return onComplete(msg, ack, err2)
}
return err2
}
return this.sess.Unsuback.Wait(msg, onc)
}
func (this *service) ping(onComplete OnCompleteFunc) error {
msg := message.NewPingreqMessage()
_, err := this.writeMessage(msg)
if err != nil {
return fmt.Errorf("(%s) Error sending %s message: %v", this.cid(), msg.Name(), err)
}
return this.sess.Pingack.Wait(msg, onComplete)
}
func (this *service) isDone() bool {
select {
case <-this.done:
return true
default:
}
return false
}
func (this *service) cid() string {
return fmt.Sprintf("%d/%s", this.id, this.sess.ID())
}