Code coverage report for node/operations.js

Statements: 84.68% (105 / 124)      Branches: 47.62% (20 / 42)      Functions: 83.33% (15 / 18)      Lines: 84.68% (105 / 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                                            1   1   1   1 1118   1118 1118 1118 1118   1118 1118   1118       1118       1118     1 2576   2576 2576 2576 2576 2576     1 1337   1337 1337       1 25   25               25 24       1 1103   1103     1 8619   8619     1 3920   3920 3920 18   3902       1           1 2578   2578 2578 2578   2578   2578     1 2566   2566 2566 2566   2566   2566     1 3799   3799 3799     3799 1223     2576 2576 2576     2576 2576 2576 2576 2576 2576   2576   2576     1                                                         1 2604   2604 2604   38     2566 2566 2566     2566 2566   2566     1 1103   1103 1103     1 2197   2197     1 51   51 51     1 102   102 102 82 82 82         82                            
// 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;
 
    Eif (self.operations.requests.out[self.id] === self) {
        delete self.operations.requests.out[self.id];
    }
};
 
Operations.prototype.checkLastTimeoutTime = function checkLastTimeoutTime(now) {
    var self = this;
 
    Iif (self.lastTimeoutTime &&
        now > self.lastTimeoutTime + self.connectionStalePeriod) {
        var err = errors.ConnectionStaleTimeoutError({
            lastTimeoutTime: self.lastTimeoutTime
        });
        process.nextTick(function opCheckLastTimedout() {
            self.connection.timedOutEvent.emit(self.connection, err);
        });
    } 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++;
 
    req.timeHeapHandle = 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++;
 
    req.timeHeapHandle = self.connection.channel.timeHeap.update(req);
 
    return req;
};
 
Operations.prototype.popOutReq = function popOutReq(id, context) {
    var self = this;
 
    var req = self.requests.out[id];
    Iif (!req) {
        self.logMissingOutRequest(id, context);
        return null;
    } else if (req.isTombstone) {
        return null;
    }
 
    Eif (req.timeHeapHandle) {
        req.timeHeapHandle.cancel();
        req.timeHeapHandle = 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
    if (context && context.originalId) {
        context = {
            error: context,
            id: context.originalId,
            info: 'got error frame for unknown id'
        };
    } else if (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;
    }
 
    Eif (req.timeHeapHandle) {
        req.timeHeapHandle.cancel();
        req.timeHeapHandle = null;
    }
 
    delete self.requests.in[id];
    self.pending.in--;
 
    return req;
};
 
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);
            }
        }
    }
};