Add Object.freeze on private enums
Improve code readability
This commit is contained in:
@ -34,18 +34,19 @@ ClusterDnsEndPointDiscoverer.prototype.discover = function(failedTcpEndPoint) {
|
||||
function discover(resolve, reject) {
|
||||
self._discoverEndPoint(failedTcpEndPoint)
|
||||
.then(function (endPoints) {
|
||||
if (!endPoints)
|
||||
if (!endPoints) {
|
||||
self._log.info(util.format("Discovering attempt %d/%d failed: no candidate found.", attempt, self._maxDiscoverAttempts));
|
||||
}
|
||||
return endPoints;
|
||||
})
|
||||
.catch(function (exc) {
|
||||
self._log.info(util.format("Discovering attempt %d/%d failed with error: %s.\n%s", attempt, self._maxDiscoverAttempts, exc, exc.stack));
|
||||
})
|
||||
.then(function (endPoints) {
|
||||
if (endPoints)
|
||||
return resolve(endPoints);
|
||||
if (attempt++ === self._maxDiscoverAttempts)
|
||||
if (endPoints) return resolve(endPoints);
|
||||
if (attempt++ === self._maxDiscoverAttempts) {
|
||||
return reject(new Error('Failed to discover candidate in ' + self._maxDiscoverAttempts + ' attempts.'));
|
||||
}
|
||||
setTimeout(discover, 500, resolve, reject);
|
||||
});
|
||||
}
|
||||
@ -74,8 +75,7 @@ ClusterDnsEndPointDiscoverer.prototype._discoverEndPoint = function (failedTcpEn
|
||||
if (endPoints) return endPoints;
|
||||
return self._tryGetGossipFrom(gossipCandidates[j++])
|
||||
.then(function (gossip) {
|
||||
if (!gossip || !gossip.members || gossip.members.length === 0)
|
||||
return;
|
||||
if (!gossip || !gossip.members || gossip.members.length === 0) return;
|
||||
var bestNode = self._tryDetermineBestNode(gossip.members);
|
||||
if (bestNode) {
|
||||
self._oldGossip = gossip.members;
|
||||
@ -105,10 +105,11 @@ ClusterDnsEndPointDiscoverer.prototype._arrangeGossipCandidates = function (memb
|
||||
var j = members.length;
|
||||
for (var k = 0; k < members.length; ++k)
|
||||
{
|
||||
if (members[k].state === 'Manager')
|
||||
if (members[k].state === 'Manager') {
|
||||
result[--j] = new GossipSeed({host: members[k].externalHttpIp, port: members[k].externalHttpPort});
|
||||
else
|
||||
} else {
|
||||
result[++i] = new GossipSeed({host: members[k].externalHttpIp, port: members[k].externalHttpPort});
|
||||
}
|
||||
}
|
||||
this._randomShuffle(result, 0, i); // shuffle nodes
|
||||
this._randomShuffle(result, j, members.length - 1); // shuffle managers
|
||||
@ -122,8 +123,7 @@ ClusterDnsEndPointDiscoverer.prototype._getGossipCandidatesFromDns = function ()
|
||||
var endpoints = self._gossipSeeds;
|
||||
self._randomShuffle(endpoints, 0, endpoints.length - 1);
|
||||
resolve(endpoints);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
const dnsOptions = {
|
||||
family: 4,
|
||||
hints: dns.ADDRCONFIG | dns.V4MAPPED,
|
||||
@ -192,7 +192,7 @@ ClusterDnsEndPointDiscoverer.prototype._tryGetGossipFrom = function (endPoint) {
|
||||
});
|
||||
};
|
||||
|
||||
const VNodeStates = {
|
||||
const VNodeStates = Object.freeze({
|
||||
'Initializing': 0,
|
||||
'Unknown': 1,
|
||||
'PreReplica': 2,
|
||||
@ -204,7 +204,7 @@ const VNodeStates = {
|
||||
'Manager': 8,
|
||||
'ShuttingDown': 9,
|
||||
'Shutdown': 10
|
||||
};
|
||||
});
|
||||
|
||||
ClusterDnsEndPointDiscoverer.prototype._tryDetermineBestNode = function (members) {
|
||||
var notAllowedStates = [
|
||||
@ -240,8 +240,7 @@ function rndNext(min, max) {
|
||||
}
|
||||
|
||||
ClusterDnsEndPointDiscoverer.prototype._randomShuffle = function (arr, i, j) {
|
||||
if (i >= j)
|
||||
return;
|
||||
if (i >= j) return;
|
||||
for (var k = i; k <= j; ++k)
|
||||
{
|
||||
var index = rndNext(k, j + 1);
|
||||
|
@ -17,14 +17,14 @@ var TcpCommand = require('../systemData/tcpCommand');
|
||||
var TcpFlags = require('../systemData/tcpFlags');
|
||||
var InspectionDecision = require('../systemData/inspectionDecision');
|
||||
|
||||
const ConnectionState = {
|
||||
const ConnectionState = Object.freeze({
|
||||
Init: 'init',
|
||||
Connecting: 'connecting',
|
||||
Connected: 'connected',
|
||||
Closed: 'closed'
|
||||
};
|
||||
});
|
||||
|
||||
const ConnectingPhase = {
|
||||
const ConnectingPhase = Object.freeze({
|
||||
Invalid: 'invalid',
|
||||
Reconnecting: 'reconnecting',
|
||||
EndPointDiscovery: 'endpointDiscovery',
|
||||
@ -32,7 +32,7 @@ const ConnectingPhase = {
|
||||
Authentication: 'authentication',
|
||||
Identification: 'identification',
|
||||
Connected: 'connected'
|
||||
};
|
||||
});
|
||||
|
||||
const TimerPeriod = 200;
|
||||
const TimerTickMessage = new messages.TimerTickMessage();
|
||||
@ -187,8 +187,7 @@ EventStoreConnectionLogicHandler.prototype._closeConnection = function(reason, e
|
||||
|
||||
this._logInfo("Closed. Reason: %s", reason);
|
||||
|
||||
if (error)
|
||||
this.emit('error', error);
|
||||
if (error) this.emit('error', error);
|
||||
|
||||
this.emit('closed', reason);
|
||||
};
|
||||
@ -205,7 +204,7 @@ EventStoreConnectionLogicHandler.prototype._closeTcpConnection = function(reason
|
||||
this._connection = null;
|
||||
};
|
||||
|
||||
var _nextSeqNo = -1;
|
||||
var _nextSeqNo = 0;
|
||||
function createOperationItem(operation, maxRetries, timeout) {
|
||||
var operationItem = {
|
||||
seqNo: _nextSeqNo++,
|
||||
@ -283,10 +282,11 @@ EventStoreConnectionLogicHandler.prototype._startSubscription = function(msg) {
|
||||
this._state === ConnectionState.Connected ? "fire" : "enqueue",
|
||||
operation.constructor.name, operation, msg.maxRetries, msg.timeout);
|
||||
var subscription = createSubscriptionItem(operation, msg.maxRetries, msg.timeout);
|
||||
if (this._state === ConnectionState.Connecting)
|
||||
if (this._state === ConnectionState.Connecting) {
|
||||
this._subscriptions.enqueueSubscription(subscription);
|
||||
else
|
||||
} else {
|
||||
this._subscriptions.startSubscription(subscription, this._connection);
|
||||
}
|
||||
break;
|
||||
case ConnectionState.Closed:
|
||||
msg.cb(new Error("Connection closed. Connection: " + this._esConnection.connectionName));
|
||||
@ -312,10 +312,11 @@ EventStoreConnectionLogicHandler.prototype._startPersistentSubscription = functi
|
||||
this._state === ConnectionState.Connected ? "fire" : "enqueue",
|
||||
operation.constructor.name, operation, msg.maxRetries, msg.timeout);
|
||||
var subscription = createSubscriptionItem(operation, msg.maxRetries, msg.timeout);
|
||||
if (this._state === ConnectionState.Connecting)
|
||||
if (this._state === ConnectionState.Connecting) {
|
||||
this._subscriptions.enqueueSubscription(subscription);
|
||||
else
|
||||
} else {
|
||||
this._subscriptions.startSubscription(subscription, this._connection);
|
||||
}
|
||||
break;
|
||||
case ConnectionState.Closed:
|
||||
msg.cb(new Error("Connection closed. " + this._esConnection.connectionName));
|
||||
@ -561,8 +562,9 @@ EventStoreConnectionLogicHandler.prototype._handleTcpPackage = function(connecti
|
||||
default:
|
||||
throw new Error("Unknown InspectionDecision: " + result.decision);
|
||||
}
|
||||
if (this._state === ConnectionState.Connected)
|
||||
if (this._state === ConnectionState.Connected) {
|
||||
this._operations.scheduleWaitingOperations(connection);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@ -609,8 +611,7 @@ EventStoreConnectionLogicHandler.prototype._reconnectTo = function(endPoints) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._state !== ConnectionState.Connected || this._connection.remoteEndPoint === endPoint)
|
||||
return;
|
||||
if (this._state !== ConnectionState.Connected || this._connection.remoteEndPoint === endPoint) return;
|
||||
|
||||
var msg = util.format("EventStoreConnection '%s': going to reconnect to [%j]. Current endpoint: [%j, L%j].",
|
||||
this._esConnection.connectionName, endPoint, this._connection.remoteEndPoint, this._connection.localEndPoint);
|
||||
@ -627,30 +628,26 @@ EventStoreConnectionLogicHandler.prototype._timerTick = function() {
|
||||
{
|
||||
case ConnectionState.Init: break;
|
||||
case ConnectionState.Connecting:
|
||||
if (this._connectingPhase === ConnectingPhase.Reconnecting && (Date.now() - this._reconnInfo.timeStamp) >= this._settings.reconnectionDelay)
|
||||
{
|
||||
if (this._connectingPhase === ConnectingPhase.Reconnecting && (Date.now() - this._reconnInfo.timeStamp) >= this._settings.reconnectionDelay) {
|
||||
this._logDebug("TimerTick checking reconnection...");
|
||||
|
||||
this._reconnInfo = {reconnectionAttempt: this._reconnInfo.reconnectionAttempt + 1, timeStamp: Date.now()};
|
||||
if (this._settings.maxReconnections >= 0 && this._reconnInfo.reconnectionAttempt > this._settings.maxReconnections)
|
||||
if (this._settings.maxReconnections >= 0 && this._reconnInfo.reconnectionAttempt > this._settings.maxReconnections) {
|
||||
this._closeConnection("Reconnection limit reached.");
|
||||
else
|
||||
{
|
||||
} else {
|
||||
this.emit('reconnecting', {});
|
||||
this._discoverEndpoint(null);
|
||||
}
|
||||
}
|
||||
else if (this._connectingPhase === ConnectingPhase.Authentication && (Date.now() - this._authInfo.timeStamp) >= this._settings.operationTimeout)
|
||||
{
|
||||
} else if (this._connectingPhase === ConnectingPhase.Authentication && (Date.now() - this._authInfo.timeStamp) >= this._settings.operationTimeout) {
|
||||
this.emit('authenticationFailed', "Authentication timed out.");
|
||||
if (this._clientVersion === 1) {
|
||||
this._goToIdentifiedState();
|
||||
} else {
|
||||
this._goToConnectedState();
|
||||
}
|
||||
}
|
||||
else if (this._connectingPhase === ConnectingPhase.Authentication || this._connectingPhase === ConnectingPhase.Connected)
|
||||
} else if (this._connectingPhase === ConnectingPhase.Authentication || this._connectingPhase === ConnectingPhase.Connected) {
|
||||
this._manageHeartbeats();
|
||||
}
|
||||
break;
|
||||
case ConnectionState.Connected:
|
||||
// operations timeouts are checked only if connection is established and check period time passed
|
||||
@ -677,8 +674,7 @@ EventStoreConnectionLogicHandler.prototype._manageHeartbeats = function() {
|
||||
if (this._connection === null) return;
|
||||
|
||||
var timeout = this._heartbeatInfo.isIntervalStage ? this._settings.heartbeatInterval : this._settings.heartbeatTimeout;
|
||||
if ((Date.now() - this._heartbeatInfo.timeStamp) < timeout)
|
||||
return;
|
||||
if ((Date.now() - this._heartbeatInfo.timeStamp) < timeout) return;
|
||||
|
||||
var packageNumber = this._packageNumber;
|
||||
if (this._heartbeatInfo.lastPackageNumber !== packageNumber)
|
||||
@ -711,15 +707,17 @@ EventStoreConnectionLogicHandler.prototype._manageHeartbeats = function() {
|
||||
EventStoreConnectionLogicHandler.prototype._logDebug = function(message) {
|
||||
if (!this._settings.verboseLogging) return;
|
||||
|
||||
if (arguments.length > 1)
|
||||
message = util.format.apply(util, Array.prototype.slice.call(arguments));
|
||||
if (arguments.length > 1) {
|
||||
message = util.format.apply(util, Array.prototype.slice.call(arguments));
|
||||
}
|
||||
|
||||
this._settings.log.debug("EventStoreConnection '%s': %s", this._esConnection.connectionName, message);
|
||||
};
|
||||
|
||||
EventStoreConnectionLogicHandler.prototype._logInfo = function(message){
|
||||
if (arguments.length > 1)
|
||||
if (arguments.length > 1) {
|
||||
message = util.format.apply(util, Array.prototype.slice.call(arguments));
|
||||
}
|
||||
|
||||
this._settings.log.info("EventStoreConnection '%s': %s", this._esConnection.connectionName, message);
|
||||
};
|
||||
|
@ -1,5 +1,4 @@
|
||||
var util = require('util');
|
||||
var ensure = require('../common/utils/ensure');
|
||||
|
||||
function Message() {
|
||||
}
|
||||
|
@ -110,8 +110,7 @@ OperationsManager.prototype.checkTimeoutsAndRetry = function(connection) {
|
||||
};
|
||||
|
||||
OperationsManager.prototype.scheduleOperationRetry = function(operation) {
|
||||
if (!this.removeOperation(operation))
|
||||
return;
|
||||
if (!this.removeOperation(operation)) return;
|
||||
|
||||
this._logDebug("ScheduleOperationRetry for %s.", operation);
|
||||
if (operation.maxRetries >= 0 && operation.retryCount >= operation.maxRetries)
|
||||
@ -166,10 +165,11 @@ OperationsManager.prototype.scheduleOperation = function(operation, connection)
|
||||
OperationsManager.prototype._logDebug = function(message) {
|
||||
if (!this._settings.verboseLogging) return;
|
||||
|
||||
if (arguments.length > 1)
|
||||
if (arguments.length > 1) {
|
||||
message = util.format.apply(util, Array.prototype.slice.call(arguments));
|
||||
}
|
||||
|
||||
this._settings.log.debug("EventStoreConnection '%s': %s.", this._connectionName, message);
|
||||
};
|
||||
|
||||
module.exports = OperationsManager;
|
||||
module.exports = OperationsManager;
|
@ -1,8 +1,6 @@
|
||||
function typeName(t) {
|
||||
if (typeof t === 'function')
|
||||
return t.name;
|
||||
if (typeof t === 'object')
|
||||
return t.constructor.name;
|
||||
if (typeof t === 'function') return t.name;
|
||||
if (typeof t === 'object') return t.constructor.name;
|
||||
throw new TypeError('type must be a function or object, not ' + typeof t);
|
||||
}
|
||||
|
||||
@ -36,8 +34,7 @@ SimpleQueuedHandler.prototype._processQueue = function() {
|
||||
while(message) {
|
||||
var typeId = typeName(message);
|
||||
var handler = this._handlers[typeId];
|
||||
if (!handler)
|
||||
throw new Error("No handler registered for message " + typeId);
|
||||
if (!handler) throw new Error("No handler registered for message " + typeId);
|
||||
setImmediate(handler, message);
|
||||
message = this._messages.shift();
|
||||
}
|
||||
|
Reference in New Issue
Block a user