-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.js
697 lines (576 loc) · 20.4 KB
/
connection.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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var assert = require('assert');
var bufrw = require('bufrw');
var extend = require('xtend');
var ReadMachine = require('bufrw/stream/read_machine');
var inherits = require('util').inherits;
var v2 = require('./v2');
var errors = require('./errors');
var States = require('./reqres_states');
var TChannelConnectionBase = require('./connection_base');
function TChannelConnection(channel, socket, direction, socketRemoteAddr) {
assert(socketRemoteAddr !== channel.hostPort,
'refusing to create self connection'
);
var self = this;
TChannelConnectionBase.call(self, channel, direction, socketRemoteAddr);
self.identifiedEvent = self.defineEvent('identified');
if (direction === 'out') {
if (self.channel.emitConnectionMetrics) {
self.channel.connectionsInitiatedStat.increment(1, {
'host-port': self.channel.hostPort || '0.0.0.0:0',
'peer-host-port': socketRemoteAddr
});
}
} else {
if (self.channel.emitConnectionMetrics) {
self.channel.connectionsAcceptedStat.increment(1, {
'host-port': self.channel.hostPort,
'peer-host-port': socketRemoteAddr
});
}
}
self.socket = socket;
self.ephemeral = false;
var opts = {
logger: self.channel.logger,
random: self.channel.random,
timers: self.channel.timers,
hostPort: self.channel.hostPort,
requireAs: self.channel.requireAs,
requireCn: self.channel.requireCn,
tracer: self.tracer,
processName: self.options.processName,
connection: self,
handleCallLazily: handleCallLazily
};
// jshint forin:true
self.handler = new v2.Handler(opts);
self.mach = ReadMachine(bufrw.UInt16BE, v2.Frame.RW);
self.setupSocket();
self.setupHandler();
self.start();
function handleCallLazily(frame) {
return self.handleCallLazily(frame);
}
}
inherits(TChannelConnection, TChannelConnectionBase);
TChannelConnection.prototype.setLazyHandling = function setLazyHandling(enabled) {
var self = this;
// TODO: push down read machine concern into handler entirely;
// boundary should just be self.handler.handleChunk in
// onSocketChunk under setupSocket; then the switching logic
// moves wholly into a `self.handler.setLazyHandling(bool)`
if (enabled && self.mach.chunkRW !== v2.LazyFrame.RW) {
self.mach.chunkRW = v2.LazyFrame.RW;
} else if (!enabled && self.mach.chunkRW !== v2.Frame.RW) {
self.mach.chunkRW = v2.Frame.RW;
}
self.handler.useLazyFrames(enabled);
};
TChannelConnection.prototype.setupSocket = function setupSocket() {
var self = this;
self.socket.setNoDelay(true);
// TODO: stream the data with backpressure
// when you add data event listener you go into
// a deoptimized mode and you have lost all
// backpressure on the stream
self.socket.on('data', onSocketChunk);
self.socket.on('close', onSocketClose);
self.socket.on('error', onSocketError);
// TODO: move to method for function optimization
function onSocketChunk(chunk) {
var err = self.mach.handleChunk(chunk);
if (err) {
self.sendProtocolError('read', err);
}
}
// TODO: move to method for function optimization
function onSocketClose() {
self.resetAll(errors.SocketClosedError({
reason: 'remote closed',
socketRemoteAddr: self.socketRemoteAddr,
direction: self.direction,
remoteName: self.remoteName
}));
if (self.ephemeral) {
var peer = self.channel.peers.get(self.socketRemoteAddr);
if (peer) {
peer.close(noop);
}
self.channel.peers.delete(self.socketRemoteAddr);
}
}
function onSocketError(err) {
self.onSocketError(err);
}
};
function noop() {}
TChannelConnection.prototype.setupHandler = function setupHandler() {
var self = this;
self.setLazyHandling(self.channel.options.useLazyHandling);
self.handler.write = function write(buf, done) {
self.socket.write(buf, null, done);
};
self.mach.emit = handleReadFrame;
self.handler.writeErrorEvent.on(onWriteError);
self.handler.errorEvent.on(onHandlerError);
self.handler.errorFrameEvent.on(onErrorFrame);
self.handler.callIncomingRequestEvent.on(onCallRequest);
self.handler.callIncomingResponseEvent.on(onCallResponse);
self.handler.pingIncomingResponseEvent.on(onPingResponse);
self.handler.callIncomingErrorFrameEvent.on(onCallErrorFrame);
// TODO: restore dumping from old:
// var stream = self.socket;
// if (dumpEnabled) {
// stream = stream.pipe(Spy(process.stdout, {
// prefix: '>>> ' + self.remoteAddr + ' '
// }));
// }
// stream = stream
// .pipe(self.reader)
// .pipe(self.handler)
// ;
// if (dumpEnabled) {
// stream = stream.pipe(Spy(process.stdout, {
// prefix: '<<< ' + self.remoteAddr + ' '
// }));
// }
// stream = stream
// .pipe(self.socket)
// ;
function onWriteError(err) {
self.onWriteError(err);
}
function onHandlerError(err) {
self.onHandlerError(err);
}
function onErrorFrame(errFrame) {
self.onErrorFrame(errFrame);
}
function handleReadFrame(frame) {
self.handleReadFrame(frame);
}
function onCallRequest(req) {
self.handleCallRequest(req);
}
function onCallResponse(res) {
self.onCallResponse(res);
}
function onPingResponse(res) {
self.handlePingResponse(res);
}
function onCallErrorFrame(errFrame) {
self.onCallErrorFrame(errFrame);
}
};
TChannelConnection.prototype.sendProtocolError =
function sendProtocolError(type, err) {
var self = this;
assert(type === 'write' || type === 'read',
'Got invalid type: ' + type);
var protocolError;
if (type === 'read') {
protocolError = errors.TChannelReadProtocolError(err, {
remoteName: self.remoteName,
localName: self.channel.hostPort,
frameId: err.frameId
});
self.channel.inboundProtocolErrorsStat.increment(1, {
'host-port': self.channel.hostPort || '0.0.0.0:0',
'peer-host-port': self.socketRemoteAddr
});
self.handler.sendErrorFrame(
protocolError.frameId || v2.Frame.NullId, null,
'ProtocolError', protocolError.message);
self.resetAll(protocolError);
} else if (type === 'write') {
protocolError = errors.TChannelWriteProtocolError(err, {
remoteName: self.remoteName,
localName: self.channel.hostPort,
frameId: err.frameId
});
// TODO: what if you have a write error in a call req cont frame
self.resetAll(protocolError);
}
};
TChannelConnection.prototype.onWriteError = function onWriteError(err) {
var self = this;
self.sendProtocolError('write', err);
};
TChannelConnection.prototype.onErrorFrame = function onErrorFrame(errFrame) {
var self = this;
// TODO: too coupled to v2
switch (errFrame.body.code) {
case v2.ErrorResponse.Codes.ProtocolError:
var codeErrorType = v2.ErrorResponse.CodeErrors[errFrame.body.code];
self.resetAll(codeErrorType({
originalId: errFrame.id,
message: String(errFrame.body.message)
}));
return;
case v2.ErrorResponse.Codes.Declined:
var match = /^draining:\s*(.+)$/.exec(errFrame.body.message);
if (match) {
self.draining = true;
self.drainReason = 'remote draining: ' + match[1];
// TODO:
// - info log?
// - invaliadet peer score?
return;
}
logUnhandled(v2.ErrorResponse.CodeNames[errFrame.body.code]);
break;
case v2.ErrorResponse.Codes.BadRequest:
case v2.ErrorResponse.Codes.Busy:
case v2.ErrorResponse.Codes.Cancelled:
case v2.ErrorResponse.Codes.NetworkError:
case v2.ErrorResponse.Codes.Timeout:
case v2.ErrorResponse.Codes.UnexpectedError:
case v2.ErrorResponse.Codes.Unhealthy:
logUnhandled(v2.ErrorResponse.CodeNames[errFrame.body.code]);
return;
default:
logUnhandled('unknown');
}
function logUnhandled(codeName) {
self.logger.warn('unhandled error frame', self.extendLogInfo({
id: errFrame.id,
errorCode: errFrame.body.code,
errorCodeName: codeName,
errorTracing: errFrame.body.tracing,
errorMessage: errFrame.body.message
}));
}
};
TChannelConnection.prototype.onHandlerError = function onHandlerError(err) {
var self = this;
self.resetAll(err);
};
TChannelConnection.prototype.handlePingResponse = function handlePingResponse(resFrame) {
var self = this;
// TODO: explicit type
self.pingResponseEvent.emit(self, {id: resFrame.id});
};
TChannelConnection.prototype.handleReadFrame = function handleReadFrame(frame) {
var self = this;
if (!self.closing) {
self.ops.lastTimeoutTime = 0;
}
self.handler.handleFrame(frame);
};
TChannelConnection.prototype.onCallResponse = function onCallResponse(res) {
var self = this;
var req = self.ops.getOutReq(res.id);
if (res.state === States.Done || res.state === States.Error) {
self.ops.popOutReq(res.id, res);
} else {
self._deferPopOutReq(res);
}
if (!req) {
return;
}
if (self.tracer && !req.forwardTrace) {
// TODO: better annotations
req.span.annotate('cr');
self.tracer.report(req.span);
res.span = req.span;
}
req.emitResponse(res);
};
TChannelConnection.prototype._deferPopOutReq = function _deferPopOutReq(res) {
var self = this;
var called = false;
res.errorEvent.on(popOutReq);
res.finishEvent.on(popOutReq);
// TODO: move to method
function popOutReq() {
if (called) {
return;
}
called = true;
self.ops.popOutReq(res.id, res);
}
};
TChannelConnection.prototype.ping = function ping() {
var self = this;
return self.handler.sendPingRequest();
};
TChannelConnection.prototype.onCallErrorFrame =
function onCallErrorFrame(errFrame) {
var self = this;
var id = errFrame.id;
var req = self.ops.getOutReq(id);
var codeErrorType = v2.ErrorResponse.CodeErrors[errFrame.body.code];
var err = codeErrorType({
originalId: id,
message: String(errFrame.body.message)
});
if (req) {
if (req.res) {
req.res.errorEvent.emit(req.res, err);
} else {
// Only popOutReq if there is no call response object yet
req = self.ops.popOutReq(id, err);
req.emitError(err);
}
}
};
TChannelConnection.prototype.start = function start() {
var self = this;
if (self.direction === 'out') {
self.handler.sendInitRequest();
self.handler.initResponseEvent.on(onOutIdentified);
} else {
self.handler.initRequestEvent.on(onInIdentified);
}
var now = self.timers.now();
var initOp = new InitOperation(self, now, self.channel.initTimeout);
var initTo = self.channel.timeHeap.update(initOp, now);
function onOutIdentified(init) {
initTo.cancel();
self.onOutIdentified(init);
}
function onInIdentified(init) {
initTo.cancel();
self.onInIdentified(init);
}
};
TChannelConnection.prototype.onOutIdentified = function onOutIdentified(init) {
var self = this;
if (init.hostPort === '0.0.0.0:0') {
return self.emit('error', errors.EphemeralInitResponse({
hostPort: init.hostPort,
socketRemoteAddr: self.socketRemoteAddr,
processName: init.processName
}));
}
self.remoteName = init.hostPort;
self.identifiedEvent.emit(self, {
hostPort: init.hostPort,
processName: init.processName
});
};
TChannelConnection.prototype.onInIdentified = function onInIdentified(init) {
var self = this;
if (init.hostPort === '0.0.0.0:0') {
self.ephemeral = true;
self.remoteName = '' + self.socket.remoteAddress + ':' + self.socket.remotePort;
assert(self.remoteName !== self.channel.hostPort,
'should not be able to receive ephemeral connection from self');
} else {
self.remoteName = init.hostPort;
}
self.channel.peers.add(self.remoteName).addConnection(self);
self.identifiedEvent.emit(self, {
hostPort: self.remoteName,
processName: init.processName
});
};
TChannelConnection.prototype.close = function close(callback) {
var self = this;
if (self.socket.destroyed) {
callback();
} else {
self.socket.once('close', callback);
self.resetAll(errors.LocalSocketCloseError());
}
};
TChannelConnection.prototype.onSocketError = function onSocketError(err) {
var self = this;
if (!self.closing) {
self.resetAll(errors.SocketError(err, {
hostPort: self.channel.hostPort,
direction: self.direction,
socketRemoteAddr: self.socketRemoteAddr
}));
}
};
TChannelConnection.prototype.nextFrameId = function nextFrameId() {
var self = this;
return self.handler.nextFrameId();
};
TChannelConnection.prototype.buildOutRequest = function buildOutRequest(options) {
var self = this;
var req = self.handler.buildOutRequest(options);
req.errorEvent.on(onReqError);
return req;
function onReqError(err) {
self.ops.popOutReq(req.id, err);
}
};
TChannelConnection.prototype.buildOutResponse = function buildOutResponse(req, options) {
var self = this;
options = options || {};
options.inreq = req;
options.channel = self.channel;
options.logger = self.logger;
options.random = self.random;
options.timers = self.timers;
options.tracing = req.tracing;
options.span = req.span;
options.checksumType = req.checksum && req.checksum.type;
// TODO: take over popInReq on req/res error?
return self.handler.buildOutResponse(req, options);
};
// this connection is completely broken, and is going away
// In addition to erroring out all of the pending work, we reset the state
// in case anybody stumbles across this object in a core dump.
TChannelConnection.prototype.resetAll = function resetAll(err) {
var self = this;
self.ops.destroy();
err = err || errors.TChannelConnectionCloseError();
if (self.closing) {
return;
}
self.closing = true;
self.closeError = err;
self.socket.destroy();
var requests = self.ops.getRequests();
var pending = self.ops.getPending();
var inOpKeys = Object.keys(requests.in);
var outOpKeys = Object.keys(requests.out);
if (!err) {
err = errors.UnknownConnectionReset();
}
if (!self.remoteName && self.channel.emitConnectionMetrics) {
if (self.direction === 'out') {
self.channel.connectionsConnectErrorsStat.increment(1, {
'host-port': self.channel.hostPort || '0.0.0.0:0',
'peer-host-port': self.socketRemoteAddr
});
} else {
self.channel.connectionsAcceptedErrorsStat.increment(1, {
'host-port': self.channel.hostPort,
'peer-host-port': self.socketRemoteAddr
});
}
} else if (self.channel.emitConnectionMetrics) {
if (err.type !== 'tchannel.socket-local-closed') {
self.channel.connectionsErrorsStat.increment(1, {
'host-port': self.channel.hostPort || '0.0.0.0:0',
'peer-host-port': self.remoteName,
'type': err.type // TODO unified error type
});
}
self.channel.connectionsClosedStat.increment(1, {
'host-port': self.channel.hostPort || '0.0.0.0:0',
'peer-host-port': self.remoteName,
'reason': err.type // TODO unified reason type
});
}
var logInfo = self.extendLogInfo({
error: err,
numInOps: inOpKeys.length,
numOutOps: outOpKeys.length,
inPending: pending.in,
outPending: pending.out
});
// requests that we've received we can delete, but these reqs may have started their
// own outgoing work, which is hard to cancel. By setting this.closing, we make sure
// that once they do finish that their callback will swallow the response.
inOpKeys.forEach(function eachInOp(id) {
self.ops.popInReq(id);
// TODO: support canceling pending handlers
// TODO report or handle or log errors or something
});
// for all outgoing requests, forward the triggering error to the user callback
outOpKeys.forEach(function eachOutOp(id) {
var req = self.ops.popOutReq(id);
if (!req) {
return;
}
req.emitError(makeReqError(req));
});
function makeReqError(req) {
var reqErr = err;
if (reqErr.type === 'tchannel.socket-local-closed') {
reqErr = errors.TChannelLocalResetError(reqErr);
} else {
reqErr = errors.TChannelConnectionResetError(reqErr);
}
return req.extendLogInfo(self.extendLogInfo(reqErr));
}
self.ops.clear();
var errorCodeName = errors.classify(err);
if (errorCodeName !== 'NetworkError' &&
errorCodeName !== 'ProtocolError'
) {
self.logger.warn('resetting connection', logInfo);
self.errorEvent.emit(self, err);
} else if (
err.type !== 'tchannel.socket-local-closed'
) {
logInfo.error = extend(err);
logInfo.error.message = err.message;
self.logger.info('resetting connection', logInfo);
}
self.closeEvent.emit(self, err);
};
function InitOperation(connection, time, timeout) {
var self = this;
self.connection = connection;
self.time = time;
self.timeout = timeout;
}
InitOperation.prototype.onTimeout = function onTimeout(now) {
var self = this;
// noop if identify succeeded
if (self.connection.remoteName || self.connection.closing) {
return;
}
var elapsed = now - self.time;
var err = errors.ConnectionTimeoutError({
start: self.time,
elapsed: elapsed,
timeout: self.timeout
});
self.connection.logger.warn('destroying due to init timeout', self.connection.extendLogInfo({
error: err
}));
self.connection.resetAll(err);
};
TChannelConnection.prototype.sendLazyErrorFrame =
function sendLazyErrorFrame(reqFrame, codeString, message) {
var self = this;
var res = reqFrame.bodyRW.lazy.readService(reqFrame);
self.handler.sendErrorFrame(
reqFrame.id, res.err ? null : res.value,
codeString, message);
};
TChannelConnection.prototype._drain =
function _drain(reason, exempt) {
var self = this;
TChannelConnectionBase.prototype._drain.call(self, reason, exempt);
if (self.remoteName) {
sendDrainingFrame();
} else {
self.identifiedEvent.on(sendDrainingFrame);
}
function sendDrainingFrame() {
self.handler.sendErrorFrame(
v2.Frame.NullId, null,
'Declined',
'draining: ' + self.drainReason);
}
};
module.exports = TChannelConnection;