GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — tchannel-node/relay_handler.js at master · uber/tchannel-node
This repository was archived by the owner on May 3, 2022. It is now read-only.
Expand file tree/ Copy pathrelay_handler.jsMore file actions 314 lines (258 loc) · 9.07 KB / Copy pathrelay_handler.jsFile metadata and controls314 lines (258 loc) · 9.07 KB 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 // Copyright (c) 2020 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 errors = require('./errors'); var stat = require('./stat-tags.js'); var LazyRelayInReq = require('./lazy_relay.js').LazyRelayInReq; var logError = require('./lazy_relay.js').logError; RelayHandler.RelayRequest = RelayRequest; module.exports = RelayHandler; function RelayHandler(channel, circuits) { this.channel = channel; this.circuits = circuits || null; this.logger = this.channel.logger; this.lazyEnabled = this.channel.options.useLazyRelaying; } RelayHandler.prototype.type = 'tchannel.relay-handler'; RelayHandler.prototype.handleLazily = function handleLazily(conn, reqFrame) { var self = this; if (!self.lazyEnabled) { return false; } var rereq = LazyRelayInReq.alloc(); rereq.reset(conn, reqFrame); var err = rereq.initRead(); if (err) { rereq.onReadError(err); return true; } rereq.peer = self.channel.peers.choosePeer(null); if (!rereq.peer) { rereq.sendErrorFrame('Declined', 'no peer available for request'); self.logger.info('no relay peer available', rereq.extendLogInfo({})); rereq.free(); return true; } conn.ops.addInReq(rereq); rereq.createOutRequest(); return true; }; RelayHandler.prototype.handleRequest = function handleRequest(req, buildRes) { var self = this; req.forwardTrace = true; var peer = self.channel.peers.choosePeer(null); if (!peer) { // TODO: stat // TODO: allow for customization of this message so hyperbahn can // augment it with things like "at entry node", "at exit node", etc buildRes().sendError('Declined', 'no peer available for request'); self.logger.info('no relay peer available', req.extendLogInfo({})); return; } var rereq = new RelayRequest(self.channel, peer, req, buildRes); rereq.createOutRequest(); }; function RelayRequest(channel, peer, inreq, buildRes) { this.channel = channel; this.logger = this.channel.logger; this.inreq = inreq; this.inres = null; this.outres = null; this.outreq = null; this.buildRes = buildRes; this.peer = peer; this.error = null; this.boundOnError = onError; this.boundExtendLogInfo = extendLogInfo; this.boundOnIdentified = onIdentified; var self = this; inreq.timeoutEvent.on(onTimeout); function onTimeout(err) { self.onInRequestTimeout(err); } function onError(err) { self.onError(err); } function extendLogInfo(info) { return self.extendLogInfo(info); } function onIdentified(err) { if (err) { self.onError(err); } else { self.onIdentified(); } } } RelayRequest.prototype.createOutRequest = function createOutRequest() { var self = this; if (self.outreq) { self.logger.warn('relay request already started', self.extendLogInfo({})); return; } self.peer.waitForIdentified(self.boundOnIdentified); }; RelayRequest.prototype.onIdentified = function onIdentified() { var self = this; var conn = self.peer.getInConnection(true); if (!conn.remoteName) { // we get the problem self.logger.error('onIdentified called on no connection identified', { hostPort: self.peer.hostPort }); } if (conn.closing) { // most likely self.logger.error('onIdentified called on connection closing', { hostPort: self.peer.hostPort }); } var elapsed = self.channel.timers.now() - self.inreq.start; var timeout = Math.max(self.inreq.timeout - elapsed, 1); if (self.channel.maximumRelayTTL !== 0 && timeout > self.channel.maximumRelayTTL ) { self.logger.warn( 'Clamping timeout to maximum ttl allowed', self.extendLogInfo({ timeout: timeout, maximumTTL: self.channel.maximumRelayTTL }) ); timeout = self.channel.maximumRelayTTL; } // TODO use a type for this literal self.outreq = self.channel.request({ peer: self.peer, streamed: self.inreq.streamed, timeout: timeout, parent: self.inreq, tracing: self.inreq.tracing, checksum: self.inreq.checksum, forwardTrace: true, serviceName: self.inreq.serviceName, headers: self.inreq.headers, retryFlags: self.inreq.retryFlags }); self.outreq.responseEvent.on(onResponse); self.outreq.errorEvent.on(self.boundOnError); if (self.outreq.streamed) { self.outreq.sendStreams(self.inreq.arg1, self.inreq.arg2, self.inreq.arg3); } else { self.outreq.send(self.inreq.arg1, self.inreq.arg2, self.inreq.arg3); } self.channel.emitFastStat( 'tchannel.relay.latency', 'timing', elapsed, new stat.RelayLatencyTags() ); function onResponse(res) { self.onResponse(res); } }; RelayRequest.prototype.createOutResponse = function createOutResponse(options) { var self = this; if (self.outres) { self.logger.warn('relay request already responded', self.extendLogInfo({ error: self.error, options: options // TODO: seems like a Bad Idea ™ })); return null; } // It is possible that the inreq gets reaped with a timeout // It is also possible that the out request gets repead with a timeout // Both the in & out req try to create an outgoing response if (self.inreq.res && self.inreq.res.codeString === 'Timeout') { self.logger.debug('relay request already timed out', { codeString: self.inreq.res.codeString, responseMessage: self.inreq.res.message, serviceName: self.outreq && self.outreq.serviceName, arg1: self.outreq && String(self.outreq.arg1), outRemoteAddr: self.outreq && self.outreq.remoteAddr, inRemoteAddr: self.inreq.remoteAddr, inSocketRemoteAddr: self.inreq.connection.socketRemoteAddr, error: self.error }); return null; } self.outres = self.buildRes(options); return self.outres; }; RelayRequest.prototype.onResponse = function onResponse(res) { var self = this; if (self.inres) { self.logger.warn('relay request got more than one response callback', { // TODO: better context remoteAddr: res.remoteAddr, id: res.id }); return; } self.inres = res; if (!self.createOutResponse({ streamed: self.inres.streamed, headers: self.inres.headers, code: self.inres.code })) { return; } if (self.outres.streamed) { self.inres.arg2.pipe(self.outres.arg2); self.inres.arg3.pipe(self.outres.arg3); } else { self.outres.send(self.inres.arg2, self.inres.arg3); } }; RelayRequest.prototype.onError = function onError(err) { var self = this; if (self.error) { self.logger.warn('Unexpected double onError', self.inreq.extendLogInfo({ error: err, oldError: self.error })); } self.error = err; if (!self.createOutResponse()) { return; } var codeName = errors.classify(err) || 'UnexpectedError'; self.outres.sendError(codeName, err.message); self.logError(err, codeName); }; RelayRequest.prototype.onInRequestTimeout = function onInRequestTimeout(err) { var self = this; var codeName = errors.classify(err) || 'UnexpectedError'; self.logError(err, codeName); }; RelayRequest.prototype.extendLogInfo = function extendLogInfo(info) { var self = this; // XXX does inreq give: // info.remoteAddr = self.inreq.remoteAddr; // info.id = self.inreq.id; info.outRemoteAddr = self.outreq && self.outreq.remoteAddr; info = self.inreq.extendLogInfo(info); return info; }; RelayRequest.prototype.logError = function relayRequestLogError(err, codeName) { var self = this; logError(self.logger, err, codeName, self.boundExtendLogInfo); }; |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |