Code coverage report for node/peers.js

Statements: 89.76% (114 / 127)      Branches: 84.44% (38 / 45)      Functions: 86.67% (13 / 15)      Lines: 90.98% (111 / 122)      Ignored: none     

All files » node/ » peers.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                                            1 1 1   1 1 1   1 580 290   290 290 290   290 290 290 290 290     1   1 277 277 277 277 475   277   1 475 277         277         1 14 14     1 1046 1046 1046 191 3   188 22   166 166   188   1043     1 54 54 54 54 54       1 284 284     1 1630 1630 1630 1630 1551   1630     1                   1 279 279 279 279 151 151 125 125     279 279     1 3 3 3 2 2 2 2     3 3     1                   1 666 666 666 666     1 812   812 812 812 12   800   800   793 793 793   793 793 849 849 849 817 817   817 783 783       793     1  
// 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 inherits = require('util').inherits;
var EventEmitter = require('./lib/event_emitter');
 
var errors = require('./errors');
var TChannelPeer = require('./peer');
var TChannelSelfPeer = require('./self_peer');
 
function TChannelPeers(channel, options) {
    if (!(this instanceof TChannelPeers)) {
        return new TChannelPeers(channel, options);
    }
    var self = this;
    EventEmitter.call(self);
    self.allocPeerEvent = self.defineEvent('allocPeer');
 
    self.channel = channel;
    self.logger = self.channel.logger;
    self.options = options || {};
    self._map = Object.create(null);
    self.selfPeer = TChannelSelfPeer(self.channel);
}
 
inherits(TChannelPeers, EventEmitter);
 
TChannelPeers.prototype.close = function close(callback) {
    var self = this;
    var peers = [self.selfPeer].concat(self.values());
    var counter = peers.length;
    peers.forEach(function eachPeer(peer) {
        peer.close(onClose);
    });
    self.clear();
 
    function onClose() {
        if (--counter <= 0) {
            Iif (counter < 0) {
                self.logger.error('closed more peers than expected', {
                    counter: counter
                });
            }
            callback();
        }
    }
};
 
TChannelPeers.prototype.get = function get(hostPort) {
    var self = this;
    return self._map[hostPort] || null;
};
 
TChannelPeers.prototype.add = function add(hostPort, options) {
    var self = this;
    var peer = self._map[hostPort];
    if (!peer) {
        if (hostPort === self.channel.hostPort) {
            return self.selfPeer;
        }
        if (self.channel.topChannel) {
            peer = self.channel.topChannel.peers.add(hostPort);
        } else {
            peer = TChannelPeer(self.channel, hostPort, options);
            self.allocPeerEvent.emit(self, peer);
        }
        self._map[hostPort] = peer;
    }
    return peer;
};
 
TChannelPeers.prototype.addPeer = function addPeer(peer) {
    var self = this;
    assert(peer instanceof TChannelPeer, 'invalid peer');
    assert(!self._map[peer.hostPort], 'peer already defined');
    Eif (peer.hostPort !== self.channel.hostPort) {
        self._map[peer.hostPort] = peer;
    }
};
 
TChannelPeers.prototype.keys = function keys() {
    var self = this;
    return Object.keys(self._map);
};
 
TChannelPeers.prototype.values = function values() {
    var self = this;
    var keys = Object.keys(self._map);
    var ret = new Array(keys.length);
    for (var i = 0; i < keys.length; i++) {
        ret[i] = self._map[keys[i]];
    }
    return ret;
};
 
TChannelPeers.prototype.entries = function entries() {
    var self = this;
    var keys = Object.keys(self._map);
    var ret = new Array(keys.length);
    for (var i = 0; i < keys.length; i++) {
        ret[i] = [keys[i], self._map[keys[i]]];
    }
    return ret;
};
 
TChannelPeers.prototype.clear = function clear() {
    var self = this;
    var keys = self.keys();
    var vals = new Array(keys.length);
    if (self.channel.subChannels) {
        var names = Object.keys(self.channel.subChannels);
        for (var i = 0; i < names.length; i++) {
            var subChannel = self.channel.subChannels[names[i]];
            subChannel.peers._map = Object.create(null);
        }
    }
    self._map = Object.create(null);
    return vals;
};
 
TChannelPeers.prototype.delete = function del(hostPort) {
    var self = this;
    var peer = self._map[hostPort];
    if (self.channel.subChannels) {
        var names = Object.keys(self.channel.subChannels);
        for (var i = 0; i < names.length; i++) {
            var subChannel = self.channel.subChannels[names[i]];
            delete subChannel.peers._map[hostPort];
        }
    }
    delete self._map[hostPort];
    return peer;
};
 
TChannelPeers.prototype.waitForIdentified =
function waitForIdentified(options, callback) {
    var self = this;
 
    assert(typeof options.host === 'string', 'options.host is required');
 
    var peer = self.add(options.host);
    peer.waitForIdentified(callback);
};
 
TChannelPeers.prototype.request = function peersRequest(req, options) {
    var self = this;
    var peer = self.choosePeer(req, options);
    Iif (!peer) throw errors.NoPeerAvailable(); // TODO: operational error?
    return peer.request(options);
};
 
TChannelPeers.prototype.choosePeer = function choosePeer(req, options) {
    var self = this;
 
    Iif (!options) options = {};
    var hosts = null;
    if (options.host) {
        return self.add(options.host);
    } else {
        hosts = Object.keys(self._map);
    }
    if (!hosts || !hosts.length) return null;
 
    var threshold = options.peerScoreThreshold;
    Eif (threshold === undefined) threshold = self.options.peerScoreThreshold;
    Eif (threshold === undefined) threshold = 0;
 
    var selectedPeer = null, selectedScore = 0;
    for (var i = 0; i < hosts.length; i++) {
        var hostPort = hosts[i];
        var peer = self.add(hostPort);
        if (!req || !req.triedRemoteAddrs[hostPort]) {
            var score = peer.state.shouldRequest(req, options);
            var want = score > threshold &&
                       (selectedPeer === null || score > selectedScore);
            if (want) {
                selectedPeer = peer;
                selectedScore = score;
            }
        }
    }
    return selectedPeer;
};
 
module.exports = TChannelPeers;