forked from moscajs/aedes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aedes.js
305 lines (250 loc) · 7.08 KB
/
aedes.js
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
'use strict'
var mqemitter = require('mqemitter')
var EE = require('events').EventEmitter
var util = require('util')
var memory = require('aedes-persistence')
var parallel = require('fastparallel')
var series = require('fastseries')
var shortid = require('shortid')
var Packet = require('aedes-packet')
var bulk = require('bulk-write-stream')
var reusify = require('reusify')
var Client = require('./lib/client')
var xtend = require('xtend')
module.exports = Aedes
var defaultOptions = {
concurrency: 100,
heartbeatInterval: 60000, // 1 minute
connectTimeout: 30000, // 30 secs
authenticate: defaultAuthenticate,
authorizePublish: defaultAuthorizePublish,
authorizeSubscribe: defaultAuthorizeSubscribe,
published: defaultPublished
}
function Aedes (opts) {
var that = this
if (!(this instanceof Aedes)) {
return new Aedes(opts)
}
opts = xtend(defaultOptions, opts)
this.id = shortid()
this.counter = 0
this.connectTimeout = opts.connectTimeout
this.mq = opts.mq || mqemitter(opts)
this.handle = function handle (conn) {
conn.setMaxListeners(opts.concurrency * 2)
// return, just to please standard
return new Client(that, conn)
}
this.persistence = opts.persistence || memory()
this.persistence.broker = this
this._parallel = parallel()
this._series = series()
this._enqueuers = reusify(DoEnqueues)
this.authenticate = opts.authenticate
this.authorizePublish = opts.authorizePublish
this.authorizeSubscribe = opts.authorizeSubscribe
this.published = opts.published
this.clients = {}
this.brokers = {}
var heartbeatTopic = '$SYS/' + that.id + '/heartbeat'
this._heartbeatInterval = setInterval(heartbeat, opts.heartbeatInterval)
var bufId = new Buffer(that.id, 'utf8')
function heartbeat () {
that.publish({
topic: heartbeatTopic,
payload: bufId
}, noop)
}
function deleteOldBrokers (broker) {
if (that.brokers[broker] + 3 * opts.heartbeatInterval < Date.now()) {
delete that.brokers[broker]
}
}
this._clearWillInterval = setInterval(function () {
Object.keys(that.brokers).forEach(deleteOldBrokers)
that.persistence
.streamWill(that.brokers)
.pipe(bulk.obj(receiveWills))
}, opts.heartbeatInterval * 4)
function receiveWills (chunks, done) {
that._parallel(that, checkAndPublish, chunks, done)
}
function checkAndPublish (will, done) {
var needsPublishing =
!that.brokers[will.brokerId] ||
that.brokers[will.brokerId] + 3 * opts.heartbeatInterval <
Date.now()
if (needsPublishing) {
// randomize this, so that multiple brokers
// do not publish the same wills at the same time
that.publish(will, function publishWill (err) {
if (err) {
return done(err)
}
that.persistence.delWill({
id: will.clientId
}, done)
})
} else {
done()
}
}
this.mq.on('$SYS/+/heartbeat', function storeBroker (packet, done) {
that.brokers[packet.payload.toString()] = Date.now()
done()
})
this.mq.on('$SYS/+/new/clients', function closeSameClients (packet, done) {
var serverId = packet.topic.split('/')[1]
var clientId = packet.payload.toString()
if (that.clients[clientId] && serverId !== that.id) {
that.clients[clientId].close(done)
} else {
done()
}
})
// metadata
this.connectedClients = 0
}
util.inherits(Aedes, EE)
function storeRetained (_, done) {
var packet = this.packet
if (packet.retain) {
this.broker.persistence.storeRetained(packet, done)
} else {
done()
}
}
function emitPacket (_, done) {
this.broker.mq.emit(this.packet, done)
}
function enqueueOffline (_, done) {
var packet = this.packet
var enqueuer = this.broker._enqueuers.get()
enqueuer.complete = done
enqueuer.status = this
enqueuer.topic = packet.topic
this.broker.persistence.subscriptionsByTopic(
packet.topic,
enqueuer.done
)
}
function DoEnqueues () {
this.next = null
this.status = null
this.complete = null
this.topic = null
var that = this
this.done = function doneEnqueue (err, subs) {
var status = that.status
var broker = status.broker
if (err) {
// is this really recoverable?
// let's just error the whole aedes
broker.emit('error', err)
} else {
var complete = that.complete
if (that.topic.indexOf('$SYS') === 0) {
subs = subs.filter(removeSharp)
}
that.status = null
that.complete = null
that.topic = null
broker._parallel(
status,
doEnqueue, subs, complete)
broker._enqueuers.release(that)
}
}
}
function removeSharp (sub) {
return sub.topic !== '#'
}
function doEnqueue (sub, done) {
this.broker.persistence.outgoingEnqueue(sub, this.packet, done)
}
function callPublished (_, done) {
this.broker.published(this.packet, this.client, done)
this.broker.emit('publish', this.packet, this.client)
}
var publishFuncsSimple = [
storeRetained,
emitPacket,
callPublished
]
var publishFuncsQoS = [
storeRetained,
enqueueOffline,
emitPacket,
callPublished
]
Aedes.prototype.publish = function (packet, client, done) {
if (typeof client === 'function') {
done = client
client = null
}
var p = new Packet(packet, this)
var publishFuncs = publishFuncsSimple
if (p.qos > 0) {
publishFuncs = publishFuncsQoS
}
this._series(new PublishState(this, client, p), publishFuncs, null, done)
}
Aedes.prototype.subscribe = function (topic, func, done) {
this.mq.on(topic, func, done)
}
Aedes.prototype.unsubscribe = function (topic, func, done) {
this.mq.removeListener(topic, func, done)
}
Aedes.prototype.registerClient = function (client) {
var that = this
if (this.clients[client.id]) {
// moving out so we wait for this, so we don't
// unregister a good client
this.clients[client.id].close(function closeClient () {
that._finishRegisterClient(client)
})
} else {
this._finishRegisterClient(client)
}
}
Aedes.prototype._finishRegisterClient = function (client) {
this.connectedClients++
this.clients[client.id] = client
this.emit('client', client)
this.publish({
topic: '$SYS/' + this.id + '/new/clients',
payload: new Buffer(client.id, 'utf8')
}, noop)
}
Aedes.prototype.unregisterClient = function (client) {
this.connectedClients--
delete this.clients[client.id]
this.emit('clientDisconnect', client)
}
function closeClient (client, cb) {
this.clients[client].close(cb)
}
Aedes.prototype.close = function (cb) {
clearInterval(this._heartbeatInterval)
clearInterval(this._clearWillInterval)
this._parallel(this, closeClient, Object.keys(this.clients), cb || noop)
}
function defaultAuthenticate (client, username, password, callback) {
callback(null, true)
}
function defaultAuthorizePublish (client, packet, callback) {
callback(null)
}
function defaultAuthorizeSubscribe (client, sub, callback) {
callback(null, sub)
}
function defaultPublished (packet, client, callback) {
callback(null)
}
function PublishState (broker, client, packet) {
this.broker = broker
this.client = client
this.packet = packet
}
function noop () {}