Code coverage report for node/operations.js

Statements: 90.32% (112 / 124)      Branches: 64.29% (27 / 42)      Functions: 94.74% (18 / 19)      Lines: 90.32% (112 / 124)      Ignored: none     

All files » node/ » operations.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                                            1   1   1   1 1109   1109 1109 1109 1109   1109 1109   1109       1109       1109     1 2561   2561 2561 2561 2561 2561     1 2492   2492 1375       1         1 55   55               55 51       1 1092   1092     1 7677   7677     1 3908   3908 3908 24   3884       1           1 2584   2584 2584 2584   2584   2584     1 2566   2566 2566 2566   2566   2566     1 2607   2607 2607 23 23 2584 23     2561 2561 2561 2561 2561 2561   2561   2561     1   23     23           23                     23             1 2608   2608 2608   47     2561 2561   2561     1 1214   1214 5 1209 1209       1 1092   1092 1092     1 2204   2204     1 80   80 80     1 160   160 160 126 126 126         126                            
// 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 errors = require('./errors.js');
 
var TOMBSTONE_TTL_OFFSET = 500;
 
module.exports = Operations;
 
function Operations(opts) {
    var self = this;
 
    self.timers = opts.timers;
    self.logger = opts.logger;
    self.random = opts.random;
    self.connectionStalePeriod = opts.connectionStalePeriod;
 
    self.connection = opts.connection;
    self.destroyed = false; // TODO need this?
 
    self.requests = {
        in: Object.create(null),
        out: Object.create(null)
    };
    self.pending = {
        in: 0,
        out: 0
    };
    self.lastTimeoutTime = 0;
}
 
function OperationTombstone(operations, id, time, timeout) {
    var self = this;
 
    self.isTombstone = true;
    self.operations = operations;
    self.id = id;
    self.time = time;
    self.timeout = timeout;
}
 
OperationTombstone.prototype.onTimeout = function onTimeout(now) {
    var self = this;
 
    if (self.operations.requests.out[self.id] === self) {
        delete self.operations.requests.out[self.id];
    }
};
 
OperationTombstone.prototype.emitError = function emitError() {
    // noop function since tombstones go along side normal requests as called
    // by TChannelConnection#resetAll
};
 
Operations.prototype.checkLastTimeoutTime = function checkLastTimeoutTime(now) {
    var self = this;
 
    Iif (self.lastTimeoutTime &&
        now > self.lastTimeoutTime + self.connectionStalePeriod) {
        var err = errors.ConnectionStaleTimeoutError({
            lastTimeoutTime: self.lastTimeoutTime
        });
        self.connection.timedOutEvent.emit(self.connection, err);
        // TODO: y u no:
        // self.lastTimeoutTime = now;
    } else if (!self.lastTimeoutTime) {
        self.lastTimeoutTime = now;
    }
};
 
Operations.prototype.getRequests = function getRequests() {
    var self = this;
 
    return self.requests;
};
 
Operations.prototype.getPending = function getPending() {
    var self = this;
 
    return self.pending;
};
 
Operations.prototype.getOutReq = function getOutReq(id) {
    var self = this;
 
    var req = self.requests.out[id] || null;
    if (req && req.isTombstone) {
        return null;
    } else {
        return req;
    }
};
 
Operations.prototype.getInReq = function getInReq(id) {
    var self = this;
 
    return self.requests.in[id];
};
 
Operations.prototype.addOutReq = function addOutReq(req) {
    var self = this;
 
    req.operations = self;
    self.requests.out[req.id] = req;
    self.pending.out++;
 
    self.connection.channel.timeHeap.update(req);
 
    return req;
};
 
Operations.prototype.addInReq = function addInReq(req) {
    var self = this;
 
    req.operations = self;
    self.requests.in[req.id] = req;
    self.pending.in++;
 
    self.connection.channel.timeHeap.update(req);
 
    return req;
};
 
Operations.prototype.popOutReq = function popOutReq(id, context) {
    var self = this;
 
    var req = self.requests.out[id];
    if (!req) {
        self.logMissingOutRequest(id, context);
        return null;
    } else if (req.isTombstone) {
        return null;
    }
 
    var now = self.timers.now();
    var timeout = TOMBSTONE_TTL_OFFSET + req.timeout;
    var tombstone = new OperationTombstone(self, req.id, now, timeout);
    req.operations = null;
    self.requests.out[id] = tombstone;
    self.pending.out--;
 
    self.connection.channel.timeHeap.update(tombstone, now);
 
    return req;
};
 
Operations.prototype.logMissingOutRequest =
function logMissingOutRequest(id, context) {
    var self = this;
 
    // context is err or res
    Iif (context && context.originalId) {
        context = {
            error: context,
            id: context.originalId,
            info: 'got error frame for unknown id'
        };
    } else Iif (context && context.id) {
        context = {
            responseId: context.id,
            code: context.code,
            arg1: Buffer.isBuffer(context.arg1) ?
                String(context.arg1) : 'streamed-arg1',
            info: 'got call response for unknown id'
        };
    }
 
    // This could be because of a confused / corrupted server.
    self.logger.info('popOutReq received for unknown or lost id', {
        context: context,
        socketRemoteAddr: self.connection.socketRemoteAddr,
        direction: self.connection.direction
    });
};
 
Operations.prototype.popInReq = function popInReq(id) {
    var self = this;
 
    var req = self.requests.in[id];
    if (!req) {
        // TODO warn ?
        return null;
    }
 
    delete self.requests.in[id];
    self.pending.in--;
 
    return req;
};
 
Operations.prototype.removeReq = function removeReq(id) {
    var self = this;
 
    if (id in self.requests.in) {
        delete self.requests.in[id];
    } else Eif (id in self.requests.out) {
        delete self.requests.out[id];
    }
};
 
Operations.prototype.clear = function clear() {
    var self = this;
 
    self.pending.in = 0;
    self.pending.out = 0;
};
 
Operations.prototype.destroy = function destroy() {
    var self = this;
 
    self.destroyed = true;
};
 
Operations.prototype.sanitySweep = function sanitySweep() {
    var self = this;
 
    self._sweepOps(self.requests.in, 'in');
    self._sweepOps(self.requests.out, 'out');
};
 
Operations.prototype._sweepOps = function _sweepOps(ops, direction) {
    var self = this;
 
    var opKeys = Object.keys(ops);
    for (var i = 0; i < opKeys.length; i++) {
        var id = opKeys[i];
        var op = ops[id];
        Iif (op === undefined) {
            self.logger.warn('unexpected undefined operation', {
                direction: direction,
                id: id
            });
        } else Iif (op.timedOut) {
            self.logger.warn('lingering timed-out operation', {
                direction: direction,
                id: id
            });
 
            if (direction === 'in') {
                self.popInReq(id);
            } else if (direction === 'out') {
                self.popOutReq(id);
            }
        }
    }
};