lib/rpc-client/router.js
function wrrRoute(client, serverType, msg, cb) {
    const servers = client._station.serversMap[serverType];
    if (!servers || !servers.length) {
        utils.invokeCallback(cb, new Error(`rpc servers not exist with serverType: ${serverType}`));
        return;
    }
    let index;
    let weight;
    if (!client.wrrParam) {
        client.wrrParam = {};
    }
    if (client.wrrParam[serverType]) {
        index = client.wrrParam[serverType].index;
        weight = client.wrrParam[serverType].weight;
    } else {
        index = -1;
        weight = 0;
    }
    const getMaxWeight = () => {
        let maxWeight = -1;
        for (let i = 0; i < servers.length; i++) {
            const server = client._station.servers[servers[i]];
            if (!!server.weight && server.weight > maxWeight) {
                maxWeight = server.weight;
            }
        }
        return maxWeight;
    };
    while (true) {      // eslint-disable-line
        index = (index + 1) % servers.length;
        if (index === 0) {
            weight -= 1;
            if (weight <= 0) {
                weight = getMaxWeight();
                if (weight <= 0) {
                    utils.invokeCallback(cb, new Error('rpc wrr route get invalid weight.'));
                    return;
                }
            }
        }
        const server = client._station.servers[servers[index]];
        if (server.weight >= weight) {
            client.wrrParam[serverType] = { index: index, weight: weight };
            utils.invokeCallback(cb, null, server.id);
            return;
        }
    }
}
function wrrRoute(client, serverType, msg, cb) {
    const servers = client._station.serversMap[serverType];
    if (!servers || !servers.length) {
        utils.invokeCallback(cb, new Error(`rpc servers not exist with serverType: ${serverType}`));
        return;
    }
    let index;
    let weight;
    if (!client.wrrParam) {
        client.wrrParam = {};
    }
    if (client.wrrParam[serverType]) {
        index = client.wrrParam[serverType].index;
        weight = client.wrrParam[serverType].weight;
    } else {
        index = -1;
        weight = 0;
    }
    const getMaxWeight = () => {
        let maxWeight = -1;
        for (let i = 0; i < servers.length; i++) {
            const server = client._station.servers[servers[i]];
            if (!!server.weight && server.weight > maxWeight) {
                maxWeight = server.weight;
            }
        }
        return maxWeight;
    };
    while (true) {      // eslint-disable-line
        index = (index + 1) % servers.length;
        if (index === 0) {
            weight -= 1;
            if (weight <= 0) {
                weight = getMaxWeight();
                if (weight <= 0) {
                    utils.invokeCallback(cb, new Error('rpc wrr route get invalid weight.'));
                    return;
                }
            }
        }
        const server = client._station.servers[servers[index]];
        if (server.weight >= weight) {
            client.wrrParam[serverType] = { index: index, weight: weight };
            utils.invokeCallback(cb, null, server.id);
            return;
        }
    }
}
function wrrRoute ( client , serverType , msg , cb ) { const servers = client . _station . serversMap [ serverType ] ; if ( ! servers || ! servers . length ) { utils . invokeCallback ( cb , new Error ( ` ${ serverType } ` ) ) ; return ; } let index ; let weight ; if ( ! client . wrrParam ) { client . wrrParam = { } ; } if ( client . wrrParam [ serverType ] ) { index = client . wrrParam [ serverType ] . index ; weight = client . wrrParam [ serverType ] . weight ; } else { index = - 1 ; weight = 0 ; } const getMaxWeight = ( ) => { let maxWeight = - 1 ; for ( let i = 0 ; i < servers . length ; i ++ ) { const server = client . _station . servers [ servers [ i ] ] ; if ( ! ! server . weight && server . weight > maxWeight ) { maxWeight = server . weight ; } } return maxWeight ; } ; while ( true ) { // eslint-disable-line index = ( index + 1 ) % servers . length ; if ( index === 0 ) { weight -= 1 ; if ( weight <= 0 ) { weight = getMaxWeight ( ) ; if ( weight <= 0 ) { utils . invokeCallback ( cb , new Error ( 'rpc wrr route get invalid weight.' ) ) ; return ; } } } const server = client . _station . servers [ servers [ index ] ] ; if ( server . weight >= weight ) { client . wrrParam [ serverType ] = { index : index , weight : weight } ; utils . invokeCallback ( cb , null , server . id ) ; return ; } } }
Weight-Round-Robin algorithm for calculating server id.

@param client {Object} rpc client.
@param serverType {String} rpc target serverType.
@param msg {Object} rpc message.
@param cb {Function} cb(err, serverId).
Weight - Round - Robin algorithm for calculating server id .