Merge pull request #770 from glowing-bear/connectionlock

Add a lock on the connection
This commit is contained in:
Tor Hveem 2016-04-13 17:33:26 +02:00
commit a08452548e

View file

@ -15,6 +15,9 @@ weechat.factory('connection',
var connectionData = []; var connectionData = [];
var reconnectTimer; var reconnectTimer;
// Global connection lock to prevent multiple connections from being opened
var locked = false;
// Takes care of the connection and websocket hooks // Takes care of the connection and websocket hooks
var connect = function (host, port, passwd, ssl, noCompression, successCallback, failCallback) { var connect = function (host, port, passwd, ssl, noCompression, successCallback, failCallback) {
$rootScope.passwordError = false; $rootScope.passwordError = false;
@ -120,6 +123,7 @@ weechat.factory('connection',
*/ */
$log.info("Disconnected from relay"); $log.info("Disconnected from relay");
$rootScope.$emit('relayDisconnect'); $rootScope.$emit('relayDisconnect');
locked = false;
if ($rootScope.userdisconnect || !$rootScope.waseverconnected) { if ($rootScope.userdisconnect || !$rootScope.waseverconnected) {
handleClose(evt); handleClose(evt);
$rootScope.userdisconnect = false; $rootScope.userdisconnect = false;
@ -154,6 +158,7 @@ weechat.factory('connection',
* the relay. * the relay.
*/ */
$log.error("Relay error", evt); $log.error("Relay error", evt);
locked = false; // release connection lock
$rootScope.lastError = Date.now(); $rootScope.lastError = Date.now();
if (evt.type === "error" && this.readyState !== 1) { if (evt.type === "error" && this.readyState !== 1) {
@ -162,6 +167,13 @@ weechat.factory('connection',
} }
}; };
if (locked) {
// We already have an open connection
$log.debug("Aborting connection (lock in use)");
}
// Kinda need a compare-and-swap here...
locked = true;
try { try {
ngWebsockets.connect(url, ngWebsockets.connect(url,
protocol, protocol,
@ -173,6 +185,7 @@ weechat.factory('connection',
'onerror': onerror 'onerror': onerror
}); });
} catch(e) { } catch(e) {
locked = false;
$log.debug("Websocket caught DOMException:", e); $log.debug("Websocket caught DOMException:", e);
$rootScope.lastError = Date.now(); $rootScope.lastError = Date.now();
$rootScope.errorMessage = true; $rootScope.errorMessage = true;
@ -250,6 +263,7 @@ weechat.factory('connection',
// The connection can time out on its own // The connection can time out on its own
ngWebsockets.failCallbacks('disconnection'); ngWebsockets.failCallbacks('disconnection');
$rootScope.connected = false; $rootScope.connected = false;
locked = false; // release the connection lock
$rootScope.$emit('relayDisconnect'); $rootScope.$emit('relayDisconnect');
$rootScope.$apply(); $rootScope.$apply();
}); });