This repository was archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanger.js
230 lines (194 loc) · 5.68 KB
/
anger.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
'use strict'
const EE = require('events').EventEmitter
const nes = require('nes')
const Histogram = require('native-hdr-histogram')
const tryAgain = require('try-again')
const histUtil = require('hdr-histogram-percentiles-obj')
const steed = require('steed')
const get = require('lodash.get')
const xtend = require('xtend')
const Client = nes.Client
const defaultAgainOpts = {
retries: 8,
max: 10000,
jitter: 0.2,
factor: 2,
min: 100
}
function anger (opts) {
if (opts.senders > opts.connections) {
throw Error('Senders *are* connections, must be <= connections')
}
const tracker = new EE()
const clients = new Array(opts.connections)
const retries = new Array(opts.connections)
const senders = new Array(opts.senders)
const latencies = new Histogram(1, 10000, 5)
const connectLatencies = new Histogram(1, 10000, 5)
const identifier = opts.identifier || 'id'
const tail = opts.tail || false
const timeout = opts.timeout || false
const expectedResponses = typeof opts.responses === 'number'
? opts.responses
: opts.connections * opts.requests
const map = new Map()
const uidOf = typeof identifier === 'function'
? identifier
: (payload) => get(payload, identifier)
const auth = getAuth(opts.auth)
const again = tryAgain(xtend(defaultAgainOpts, opts.retryOpts))
let timedOutResponses = 0
let disconnects = 0
let reconnects = -clients.length // will be 0 after connecting each client
for (let i = 0; i < clients.length; i++) {
clients[i] = new Client(opts.url)
clients[i].anger = {
id: i,
sender: false
}
retries[i] = 0
clients[i].onDisconnect = () => { disconnects++ }
clients[i].onConnect = () => { reconnects++ }
if (i < opts.senders) {
senders[i] = clients[i]
clients[i].anger.sender = true
}
}
// map because of errors
steed.map(clients, (client, done) => {
const startTime = process.hrtime()
let numRetries = 0
again((success, failure, fatal) => {
client.connect({ auth: auth(client, client.anger.id) }, (err) => {
if (err) failure(err)
else success()
})
}, (err) => {
if (err) {
numRetries++
} else {
const end = process.hrtime(startTime)
const responseTime = end[0] * 1e3 + end[1] / 1e6
connectLatencies.record(responseTime)
retries[client.anger.id] = numRetries
done()
}
}, done)
}, (err) => {
if (err) {
return onError(err)
}
tracker.emit('connect')
// map because of errors
steed.map(clients, (client, done) => {
client.subscribe(opts.subscription, handler, done)
}, (err) => {
if (err) {
return onError(err)
}
tracker.emit('subscribe')
})
})
tracker.on('subscribe', next)
let totalResponses = 0
let totalRequests = 0
function handler (payload) {
const uid = uidOf(payload)
const mapObj = map.get(uid)
if (mapObj.finished) return
totalResponses++
recordResponseTime(mapObj.start)
if (!--mapObj.expectedResponses) {
mapObj.finished = true
if (timeout) clearTimeout(mapObj.timeout)
tracker.emit('publish-events-received', uid)
if (!tail && totalResponses + timedOutResponses >= expectedResponses) {
complete()
}
return next(mapObj.sender)
}
}
function handleTimeout (uid) {
return function () {
const mapObj = map.get(uid)
mapObj.finished = true
recordResponseTime(mapObj.start)
timedOutResponses += mapObj.expectedResponses
if (!tail && totalResponses + timedOutResponses === expectedResponses) {
complete()
}
return next(mapObj.sender)
}
}
function next (sender) {
if (totalRequests === opts.requests) return
// begin emitting the requests if none have been emitted
if (!totalRequests) {
for (let i = 0; i < senders.length; i++) {
triggerSender(senders[i])
}
}
// if sender is passed in, trigger it
if (sender) triggerSender(sender)
}
function triggerSender (sender) {
const uid = opts.trigger(sender)
if (++totalRequests === opts.requests && tail) setTimeout(complete, tail)
let replyTimeout
if (timeout) replyTimeout = setTimeout(handleTimeout(uid), timeout)
map.set(uid, {
start: process.hrtime(),
sender: sender,
expectedResponses: clients.length,
timeout: replyTimeout,
finished: false,
id: uid
})
// emit trigger every time a client sends a message
tracker.emit('trigger', uid)
}
function complete () {
clients.forEach(disconnect)
tracker.emit('end', {
latency: histUtil.addPercentiles(latencies, histUtil.histAsObj(latencies)),
requests: totalRequests,
responses: totalResponses,
timedOutResponses: timedOutResponses,
connectLatencies: histUtil.addPercentiles(connectLatencies, histUtil.histAsObj(connectLatencies)),
retriesAvg: mean(retries),
connections: clients.length,
senders: opts.senders,
disconnects: disconnects,
reconnects: reconnects
})
}
function disconnect (client) {
client.disconnect()
}
function onError (err) {
clients.forEach(disconnect)
tracker.emit('error', err)
}
function recordResponseTime (startTime) {
const end = process.hrtime(startTime)
const responseTime = end[0] * 1e3 + end[1] / 1e6
latencies.record(responseTime)
}
return tracker
}
function _noop () {}
function getAuth (auth) {
let ret = _noop
if (auth) {
if (typeof auth === 'function') {
ret = auth
} else {
ret = function () { return auth }
}
}
return ret
}
function mean (vals) {
return vals.reduce((a, b) => a + b) / vals.length
}
module.exports = anger