2016-03-09 20:46:15 +00:00
|
|
|
var EventStoreNodeConnection = require('./eventStoreNodeConnection');
|
|
|
|
var StaticEndpointDiscoverer = require('./core/staticEndpointDiscoverer');
|
2016-10-15 22:41:25 +00:00
|
|
|
var ClusterDnsEndPointDiscoverer = require('./core/clusterDnsEndPointDiscoverer');
|
2016-03-09 20:46:15 +00:00
|
|
|
var NoopLogger = require('./common/log/noopLogger');
|
2016-10-15 22:41:25 +00:00
|
|
|
var ensure = require('./common/utils/ensure');
|
2016-03-09 20:46:15 +00:00
|
|
|
|
2018-07-09 17:27:12 +00:00
|
|
|
var defaultConnectionSettings = Object.freeze({
|
2016-03-09 20:46:15 +00:00
|
|
|
log: new NoopLogger(),
|
|
|
|
verboseLogging: false,
|
|
|
|
|
|
|
|
maxQueueSize: 5000,
|
|
|
|
maxConcurrentItems: 5000,
|
|
|
|
maxRetries: 10,
|
|
|
|
maxReconnections: 10,
|
|
|
|
|
|
|
|
requireMaster: true,
|
|
|
|
|
|
|
|
reconnectionDelay: 100,
|
|
|
|
operationTimeout: 7*1000,
|
|
|
|
operationTimeoutCheckPeriod: 1000,
|
|
|
|
|
|
|
|
defaultUserCredentials: null,
|
|
|
|
useSslConnection: false,
|
|
|
|
targetHost: null,
|
|
|
|
validateServer: false,
|
|
|
|
|
|
|
|
failOnNoServerResponse: false,
|
|
|
|
heartbeatInterval: 750,
|
|
|
|
heartbeatTimeout: 1500,
|
2016-10-15 22:41:25 +00:00
|
|
|
clientConnectionTimeout: 1000,
|
|
|
|
|
|
|
|
// Cluster Settings
|
|
|
|
clusterDns: '',
|
2016-10-18 04:58:28 +00:00
|
|
|
maxDiscoverAttempts: 10,
|
2016-10-15 22:41:25 +00:00
|
|
|
externalGossipPort: 0,
|
|
|
|
gossipTimeout: 1000
|
2018-07-09 17:27:12 +00:00
|
|
|
});
|
2016-03-09 20:46:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
function merge(a,b) {
|
|
|
|
var c = {};
|
|
|
|
Object.getOwnPropertyNames(a).forEach(function(k) {
|
|
|
|
c[k] = a[k];
|
|
|
|
});
|
|
|
|
Object.getOwnPropertyNames(b).forEach(function(k) {
|
|
|
|
c[k] = b[k];
|
|
|
|
});
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2016-10-15 22:41:25 +00:00
|
|
|
function createFromTcpEndpoint(settings, tcpEndpoint, connectionName) {
|
2017-01-29 02:04:58 +00:00
|
|
|
if (!tcpEndpoint.port || !tcpEndpoint.host) throw new TypeError('endPoint object must have host and port properties.');
|
2016-10-15 22:41:25 +00:00
|
|
|
var mergedSettings = merge(defaultConnectionSettings, settings || {});
|
|
|
|
var endpointDiscoverer = new StaticEndpointDiscoverer(tcpEndpoint, settings.useSslConnection);
|
|
|
|
return new EventStoreNodeConnection(mergedSettings, null, endpointDiscoverer, connectionName || null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createFromStringEndpoint(settings, endPoint, connectionName) {
|
2016-12-07 02:54:04 +00:00
|
|
|
var m = endPoint.match(/^(tcp|discover):\/\/([^:]+):?(\d+)?$/);
|
2016-10-15 22:41:25 +00:00
|
|
|
if (!m) throw new Error('endPoint string must be tcp://hostname[:port] or discover://dns[:port]');
|
|
|
|
var scheme = m[1];
|
2017-01-29 18:59:55 +00:00
|
|
|
var host = m[2] || null;
|
|
|
|
var port = m[3] ? parseInt(m[3]) : null;
|
2016-10-15 22:41:25 +00:00
|
|
|
if (scheme === 'tcp') {
|
|
|
|
var tcpEndpoint = {
|
2017-01-29 02:04:58 +00:00
|
|
|
host: host,
|
2017-01-29 18:59:55 +00:00
|
|
|
port: port || 1113
|
2016-10-15 22:41:25 +00:00
|
|
|
};
|
|
|
|
return createFromTcpEndpoint(settings, tcpEndpoint, connectionName);
|
|
|
|
}
|
|
|
|
if (scheme === 'discover') {
|
2017-01-29 18:59:55 +00:00
|
|
|
return createFromClusterDns(settings, host, port || 2113, connectionName);
|
2016-10-15 22:41:25 +00:00
|
|
|
}
|
|
|
|
throw new Error('Invalid scheme for endPoint: ' + scheme);
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:59:55 +00:00
|
|
|
function createFromClusterDns(connectionSettings, clusterDns, externalGossipPort, connectionName) {
|
|
|
|
ensure.notNull(connectionSettings, "connectionSettings");
|
|
|
|
ensure.notNull(clusterDns, "clusterDns");
|
|
|
|
var mergedSettings = merge(defaultConnectionSettings, connectionSettings || {});
|
|
|
|
var clusterSettings = {
|
|
|
|
clusterDns: clusterDns,
|
|
|
|
gossipSeeds: null,
|
|
|
|
externalGossipPort: externalGossipPort,
|
|
|
|
maxDiscoverAttempts: mergedSettings.maxDiscoverAttempts,
|
|
|
|
gossipTimeout: mergedSettings.gossipTimeout
|
|
|
|
};
|
|
|
|
var endPointDiscoverer = new ClusterDnsEndPointDiscoverer(mergedSettings.log,
|
|
|
|
clusterSettings.clusterDns,
|
|
|
|
clusterSettings.maxDiscoverAttempts,
|
|
|
|
clusterSettings.externalGossipPort,
|
|
|
|
clusterSettings.gossipSeeds,
|
|
|
|
clusterSettings.gossipTimeout
|
|
|
|
);
|
|
|
|
return new EventStoreNodeConnection(mergedSettings, clusterSettings, endPointDiscoverer, connectionName);
|
|
|
|
}
|
|
|
|
|
2016-10-15 22:41:25 +00:00
|
|
|
function createFromGossipSeeds(connectionSettings, gossipSeeds, connectionName) {
|
|
|
|
ensure.notNull(connectionSettings, "connectionSettings");
|
|
|
|
ensure.notNull(gossipSeeds, "gossipSeeds");
|
|
|
|
var mergedSettings = merge(defaultConnectionSettings, connectionSettings || {});
|
|
|
|
var clusterSettings = {
|
|
|
|
clusterDns: '',
|
|
|
|
gossipSeeds: gossipSeeds,
|
|
|
|
externalGossipPort: 0,
|
|
|
|
maxDiscoverAttempts: mergedSettings.maxDiscoverAttempts,
|
|
|
|
gossipTimeout: mergedSettings.gossipTimeout
|
|
|
|
};
|
2016-10-18 04:58:28 +00:00
|
|
|
var endPointDiscoverer = new ClusterDnsEndPointDiscoverer(mergedSettings.log,
|
2016-10-15 22:41:25 +00:00
|
|
|
clusterSettings.clusterDns,
|
|
|
|
clusterSettings.maxDiscoverAttempts,
|
|
|
|
clusterSettings.externalGossipPort,
|
|
|
|
clusterSettings.gossipSeeds,
|
|
|
|
clusterSettings.gossipTimeout
|
|
|
|
);
|
|
|
|
return new EventStoreNodeConnection(mergedSettings, clusterSettings, endPointDiscoverer, connectionName);
|
|
|
|
}
|
|
|
|
|
2016-03-09 20:46:15 +00:00
|
|
|
/**
|
|
|
|
* Create an EventStore connection
|
2016-11-07 06:58:11 +00:00
|
|
|
* @public
|
|
|
|
* @alias createConnection
|
2016-03-09 20:46:15 +00:00
|
|
|
* @param {object} settings
|
2018-07-09 10:58:39 +00:00
|
|
|
* @param {boolean} [settings.verboseLogging=false]
|
|
|
|
* @param {number} [settings.maxQueueSize=5000]
|
|
|
|
* @param {number} [settings.maxConcurrentItems=5000]
|
|
|
|
* @param {number} [settings.maxRetries=10]
|
|
|
|
* @param {number} [settings.maxReconnections=10]
|
|
|
|
* @param {boolean} [settings.requireMaster=true]
|
|
|
|
* @param {number} [settings.reconnectionDelay=100]
|
|
|
|
* @param {number} [settings.operationTimeout=7000]
|
|
|
|
* @param {number} [settings.operationTimeoutCheckPeriod=1000]
|
|
|
|
* @param {object} [settings.defaultUserCredentials=null] - The default user credentials to use for requests
|
|
|
|
* @param {boolean} [settings.useSslConnection=false] - Whether to use SSL or not
|
|
|
|
* @param {object} [settings.targetHost=null]
|
|
|
|
* @param {boolean} [settings.validateServer=false]
|
|
|
|
* @param {boolean} [settings.failOnNoServerResponse=false]
|
|
|
|
* @param {number} [settings.heartbeatInterval=750]
|
|
|
|
* @param {number} [settings.heartbeatTimeout=1500]
|
|
|
|
* @param {number} [settings.clientConnectionTimeout=1000]
|
|
|
|
* @param {string} [settings.clusterDns='']
|
|
|
|
* @param {number} [settings.maxDiscoverAttempts=10]
|
|
|
|
* @param {number} [settings.externalGossipPort=0]
|
|
|
|
* @param {number} [settings.gossipTimeout=1000]
|
2016-10-15 22:41:25 +00:00
|
|
|
* @param {string|object|array} endPointOrGossipSeeds
|
2016-03-09 23:37:54 +00:00
|
|
|
* @param {string} [connectionName]
|
2016-03-09 20:46:15 +00:00
|
|
|
* @returns {EventStoreNodeConnection}
|
|
|
|
*/
|
2016-10-15 22:41:25 +00:00
|
|
|
module.exports.create = function(settings, endPointOrGossipSeeds, connectionName) {
|
|
|
|
if (Array.isArray(endPointOrGossipSeeds)) return createFromGossipSeeds(settings, endPointOrGossipSeeds, connectionName);
|
|
|
|
if (typeof endPointOrGossipSeeds === 'object') return createFromTcpEndpoint(settings, endPointOrGossipSeeds, connectionName);
|
|
|
|
if (typeof endPointOrGossipSeeds === 'string') return createFromStringEndpoint(settings, endPointOrGossipSeeds, connectionName);
|
|
|
|
throw new TypeError('endPointOrGossipSeeds must be an object, a string or an array.');
|
2018-07-09 17:27:12 +00:00
|
|
|
};
|