Do a reconnect on network problems. After 10s it gives up

This commit is contained in:
Peter 'Pita' Martischka 2011-11-25 15:24:10 -08:00
parent 53bf7dc19d
commit fa098d73cd
5 changed files with 80 additions and 203 deletions

View file

@ -77,10 +77,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
{
if (socket)
{
/*socket.onclosed = function() {};
socket.onhiccup = function() {};
socket.disconnect(true);*/
socket.disconnect();
setChannelState("DISCONNECTED", "unload");
}
});
if ($.browser.mozilla)
@ -100,18 +97,6 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
editor.setBaseAttributedText(serverVars.initialAttributedText, serverVars.apool);
editor.setUserChangeNotificationCallback(wrapRecordingErrors("handleUserChanges", handleUserChanges));
function abandonConnection(reason)
{
if (socket)
{
/*socket.onclosed = function() {};
socket.onhiccup = function() {};*/
socket.disconnect();
}
socket = null;
setChannelState("DISCONNECTED", reason);
}
function dmesg(str)
{
if (typeof window.ajlog == "string") window.ajlog += str + '\n';
@ -124,7 +109,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
{
if (channelState == "CONNECTING" && (((+new Date()) - initialStartConnectTime) > 20000))
{
abandonConnection("initsocketfail"); // give up
setChannelState("DISCONNECTED", "initsocketfail");
}
else
{
@ -141,8 +126,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
if (state == "COMMITTING" && (t - lastCommitTime) > 20000)
{
// a commit is taking too long
appLevelDisconnectReason = "slowcommit";
socket.disconnect();
setChannelState("DISCONNECTED", "slowcommit");
}
else if (state == "COMMITTING" && (t - lastCommitTime) > 5000)
{
@ -230,11 +214,6 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
handleMessageFromServer(obj);
});*/
socket.on('disconnect', function(obj)
{
handleSocketClosed(true);
});
/*var success = false;
callCatchingErrors("setUpSocket", function() {
appLevelDisconnectReason = null;
@ -366,7 +345,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
if (newRev != (rev + 1))
{
dmesg("bad message revision on NEW_CHANGES: " + newRev + " not " + (rev + 1));
socket.disconnect();
setChannelState("DISCONNECTED", "badmessage_newchanges");
return;
}
rev = newRev;
@ -378,7 +357,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
if (newRev != (rev + 1))
{
dmesg("bad message revision on ACCEPT_COMMIT: " + newRev + " not " + (rev + 1));
socket.disconnect();
setChannelState("DISCONNECTED", "badmessage_acceptcommit");
return;
}
rev = newRev;
@ -520,48 +499,6 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
//pad.dmesg($.map(getConnectedUsers(), function(u) { return u.userId.slice(-2); }).join(','));
}
function handleSocketClosed(params)
{
socket = null;
$.each(keys(userSet), function()
{
var uid = String(this);
if (uid != userId)
{
var userInfo = userSet[uid];
delete userSet[uid];
callbacks.onUserLeave(userInfo);
dmesgUsers();
}
});
var reason = appLevelDisconnectReason || params.reason;
var shouldReconnect = params.reconnect;
if (shouldReconnect)
{
// determine if this is a tight reconnect loop due to weird connectivity problems
reconnectTimes.push(+new Date());
var TOO_MANY_RECONNECTS = 8;
var TOO_SHORT_A_TIME_MS = 10000;
if (reconnectTimes.length >= TOO_MANY_RECONNECTS && ((+new Date()) - reconnectTimes[reconnectTimes.length - TOO_MANY_RECONNECTS]) < TOO_SHORT_A_TIME_MS)
{
setChannelState("DISCONNECTED", "looping");
}
else
{
setChannelState("RECONNECTING", reason);
setUpSocket();
}
}
else
{
setChannelState("DISCONNECTED", reason);
}
}
function setChannelState(newChannelState, moreInfo)
{
if (newChannelState != channelState)
@ -650,128 +587,6 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
return rev;
}
function getDiagnosticInfo()
{
var maxCaughtErrors = 3;
var maxAceErrors = 3;
var maxDebugMessages = 50;
var longStringCutoff = 500;
function trunc(str)
{
return String(str).substring(0, longStringCutoff);
}
var info = {
errors: {
length: 0
}
};
function addError(e, catcher, time)
{
var error = {
catcher: catcher
};
if (time) error.time = time;
// a little over-cautious?
try
{
if (e.description) error.description = e.description;
}
catch (x)
{}
try
{
if (e.fileName) error.fileName = e.fileName;
}
catch (x)
{}
try
{
if (e.lineNumber) error.lineNumber = e.lineNumber;
}
catch (x)
{}
try
{
if (e.message) error.message = e.message;
}
catch (x)
{}
try
{
if (e.name) error.name = e.name;
}
catch (x)
{}
try
{
if (e.number) error.number = e.number;
}
catch (x)
{}
try
{
if (e.stack) error.stack = trunc(e.stack);
}
catch (x)
{}
info.errors[info.errors.length] = error;
info.errors.length++;
}
for (var i = 0;
((i < caughtErrors.length) && (i < maxCaughtErrors)); i++)
{
addError(caughtErrors[i], caughtErrorCatchers[i], caughtErrorTimes[i]);
}
if (editor)
{
var aceErrors = editor.getUnhandledErrors();
for (var i = 0;
((i < aceErrors.length) && (i < maxAceErrors)); i++)
{
var errorRecord = aceErrors[i];
addError(errorRecord.error, "ACE", errorRecord.time);
}
}
info.time = +new Date();
info.collabState = state;
info.channelState = channelState;
info.lastCommitTime = lastCommitTime;
info.numSocketReconnects = reconnectTimes.length;
info.userId = userId;
info.currentRev = rev;
info.participants = (function()
{
var pp = [];
for (var u in userSet)
{
pp.push(u);
}
return pp.join(',');
})();
if (debugMessages.length > maxDebugMessages)
{
debugMessages = debugMessages.slice(debugMessages.length - maxDebugMessages, debugMessages.length);
}
info.debugMessages = {
length: 0
};
for (var i = 0; i < debugMessages.length; i++)
{
info.debugMessages[i] = trunc(debugMessages[i]);
info.debugMessages.length++;
}
return info;
}
function getMissedChanges()
{
var obj = {};
@ -863,10 +678,10 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
sendClientMessage: sendClientMessage,
sendMessage: sendMessage,
getCurrentRevisionNumber: getCurrentRevisionNumber,
getDiagnosticInfo: getDiagnosticInfo,
getMissedChanges: getMissedChanges,
callWhenNotCommitting: callWhenNotCommitting,
addHistoricalAuthors: tellAceAboutHistoricalAuthors
addHistoricalAuthors: tellAceAboutHistoricalAuthors,
setChannelState: setChannelState
});
}