<commit_before>'use strict';

var Promise    = require('bluebird');
var Pool       = require('./pool');
var _          = require('lodash');
var Connection = require('./connection');
var WRRPool    = require('wrr-pool');
var errors     = require('./errors');
var events     = require('events');
var util       = require('util');

var riakQuorumValues = {
    4294967294 : 'one',
    one        : 4294967294,
    4294967293 : 'quorum',
    quorum     : 4294967293,
    4294967292 : 'all',
    all        : 4294967292,
    4294967291 : 'default',
    default    : 4294967291
};

function Client(options) {
    var self = this;

    self.options = _.defaultsDeep(options || {}, {
        clientId: 'no-riak-client',
        connectionString: '127.0.0.1:8087', // 'host:port:weight,host:port:weight,..', e.g.: '10.0.1.1:8087:10,10.0.1.2:8087:5,10.0.1.3:8087:2'
        failoverConnectionString: '',
        autoJSON: true,
        connectionTimeout: 3000,
        connectionBufSize: 256 * 1024,
        maxConnectionErrors: 3,
        maxConnectionErrorsPeriod: 60 * 1000,
        maxConnectionLifetime: 15 * 60 * 1000,
        retries: 3,
        pool: {},
        auth: false,
        tls: {
            // secureProtocol: 'SSLv23_method',
            rejectUnauthorized: false,
            // `riak-admin security ciphers` shows a different list, but these are known to work:
            ciphers: 'DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:AES128-SHA256:AES128-SHA:AES256-SHA256:AES256-SHA:RC4-SHA'
        }
    });

    self.servers = new WRRPool(); // weighted round robin pool of servers
    self.failoverServers = new WRRPool();

    self.options.connectionString.trim().split(',').forEach(function (hostStr) {
        var h = hostStr.trim().split(':');

        if (h.length < 2) { return null; }

        self.servers.add({
            host: h[0],
            port: parseInt(h[1]),
            origWeight: parseInt(h[2] || 10),
            errors: []
        }, parseInt(h[2] || 10));

        return null;
    });

    self.options.failoverConnectionString.trim().split(',').forEach(function (hostStr) {
        var h = hostStr.trim().split(':');

        if (h.length < 2) { return null; }

        self.failoverServers.add({
            host: h[0],
            port: parseInt(h[1]),
            origWeight: parseInt(h[2] || 10),
            errors: []
        }, parseInt(h[2] || 10));

        return null;
    });

    if (self.servers.size() === 0) {
        throw new Error('No Riak servers were defined');
    }<commit_msg>Adjust number of retries to allow client to start with failover<commit_after>'use strict';

var Promise    = require('bluebird');
var Pool       = require('./pool');
var _          = require('lodash');
var Connection = require('./connection');
var WRRPool    = require('wrr-pool');
var errors     = require('./errors');
var events     = require('events');
var util       = require('util');

var riakQuorumValues = {
    4294967294 : 'one',
    one        : 4294967294,
    4294967293 : 'quorum',
    quorum     : 4294967293,
    4294967292 : 'all',
    all        : 4294967292,
    4294967291 : 'default',
    default    : 4294967291
};

function Client(options) {
    var self = this;

    self.options = _.defaultsDeep(options || {}, {
        clientId: 'no-riak-client',
        connectionString: '127.0.0.1:8087', // 'host:port:weight,host:port:weight,..', e.g.: '10.0.1.1:8087:10,10.0.1.2:8087:5,10.0.1.3:8087:2'
        failoverConnectionString: '',
        autoJSON: true,
        connectionTimeout: 3000,
        connectionBufSize: 256 * 1024,
        maxConnectionErrors: 3,
        maxConnectionErrorsPeriod: 60 * 1000,
        maxConnectionLifetime: 15 * 60 * 1000,
        // retries: 3,
        pool: {},
        auth: false,
        tls: {
            // secureProtocol: 'SSLv23_method',
            rejectUnauthorized: false,
            // `riak-admin security ciphers` shows a different list, but these are known to work:
            ciphers: 'DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:AES128-SHA256:AES128-SHA:AES256-SHA256:AES256-SHA:RC4-SHA'
        }
    });

    self.servers = new WRRPool(); // weighted round robin pool of servers
    self.failoverServers = new WRRPool();

    self.options.connectionString.trim().split(',').forEach(function (hostStr) {
        var h = hostStr.trim().split(':');

        if (h.length < 2) { return null; }

        self.servers.add({
            host: h[0],
            port: parseInt(h[1]),
            origWeight: parseInt(h[2] || 10),
            errors: []
        }, parseInt(h[2] || 10));

        return null;
    });

    self.options.retries = self.options.retries || (self.servers.size() * self.options.maxConnectionErrors);

    self.options.failoverConnectionString.trim().split(',').forEach(function (hostStr) {
        var h = hostStr.trim().split(':');

        if (h.length < 2) { return null; }

        self.failoverServers.add({
            host: h[0],
            port: parseInt(h[1]),
            origWeight: parseInt(h[2] || 10),
            errors: []
        }, parseInt(h[2] || 10));

        return null;
    });

    if (self.servers.size() === 0) {
        throw new Error('No Riak servers were defined');
    }
{'fraction_non_alphanumeric': 0.15522107243650046, 'fraction_numerical': 0.07751646284101599, 'mean_word_length': 2.2653562653562656, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4460075', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1699, 'n_words': 348}