From d57e191ad0be18acec3f8f9ac891c83234de2a53 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 31 Oct 2016 15:27:26 +0000 Subject: [PATCH 1/6] use socket.io connect method instead of reconnect the reconnect method tries 10 times by default, but we want to manage reconnections ourselves --- .../web/public/coffee/ide/connection/ConnectionManager.coffee | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/services/web/public/coffee/ide/connection/ConnectionManager.coffee b/services/web/public/coffee/ide/connection/ConnectionManager.coffee index 0454395d69..aa4eeb99d1 100644 --- a/services/web/public/coffee/ide/connection/ConnectionManager.coffee +++ b/services/web/public/coffee/ide/connection/ConnectionManager.coffee @@ -207,7 +207,9 @@ define [], () -> delete @$scope.connection.reconnection_countdown return if @connected @$scope.connection.reconnecting = true - @ide.socket.socket.reconnect() + # use socket.io connect() here to make a single attempt, the + # reconnect() method makes multiple attempts + @ide.socket.socket.connect() setTimeout (=> @startAutoReconnectCountdown() if !@connected), 2000 disconnectIfInactive: ()-> From f894048292826b2a121c91b8bd62ad11c05aaf1c Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 31 Oct 2016 16:19:07 +0000 Subject: [PATCH 2/6] fix lastUpdate in ConnectionManager --- .../web/public/coffee/ide/connection/ConnectionManager.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/web/public/coffee/ide/connection/ConnectionManager.coffee b/services/web/public/coffee/ide/connection/ConnectionManager.coffee index aa4eeb99d1..da7dbbc9b4 100644 --- a/services/web/public/coffee/ide/connection/ConnectionManager.coffee +++ b/services/web/public/coffee/ide/connection/ConnectionManager.coffee @@ -39,7 +39,7 @@ define [], () -> @tryReconnect() @$scope.$on 'cursor:editor:update', () => - @lastUserAction = new Date() + @lastUserAction = new Date() # time of last edit if !@connected @tryReconnect() @@ -170,7 +170,7 @@ define [], () -> startAutoReconnectCountdown: () -> twoMinutes = 2 * 60 * 1000 - if @lastUpdated? and new Date() - @lastUpdated > twoMinutes + if @lastUserAction? and new Date() - @lastUserAction > twoMinutes # between 1 minute and 3 minutes countdown = 60 + Math.floor(Math.random() * 120) else From 4da9008300fbd7e861dea4f9ab9bea61f2bbe8db Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 31 Oct 2016 15:32:01 +0000 Subject: [PATCH 3/6] rate limit on reconnection attempts (cursor/click) --- .../ide/connection/ConnectionManager.coffee | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/services/web/public/coffee/ide/connection/ConnectionManager.coffee b/services/web/public/coffee/ide/connection/ConnectionManager.coffee index da7dbbc9b4..987186fd19 100644 --- a/services/web/public/coffee/ide/connection/ConnectionManager.coffee +++ b/services/web/public/coffee/ide/connection/ConnectionManager.coffee @@ -36,16 +36,19 @@ define [], () -> inactive_disconnect: false @$scope.tryReconnectNow = () => - @tryReconnect() + # user manually requested reconnection via "Try now" button + @tryReconnectWithRateLimit({force:true}) @$scope.$on 'cursor:editor:update', () => @lastUserAction = new Date() # time of last edit if !@connected - @tryReconnect() + # user is editing, try to reconnect + @tryReconnectWithRateLimit() document.querySelector('body').addEventListener 'click', (e) => if !@connected and e.target.id != 'try-reconnect-now-button' - @tryReconnect() + # user is editing, try to reconnect + @tryReconnectWithRateLimit() @ide.socket = io.connect null, reconnect: false @@ -210,8 +213,25 @@ define [], () -> # use socket.io connect() here to make a single attempt, the # reconnect() method makes multiple attempts @ide.socket.socket.connect() + # record the time of the last attempt to connect + @lastConnectionAttempt = new Date() setTimeout (=> @startAutoReconnectCountdown() if !@connected), 2000 + MIN_RETRY_INTERVAL: 1000 # ms + BACKGROUND_RETRY_INTERVAL : 30 * 1000 # ms + + tryReconnectWithRateLimit: (options) -> + # bail out if the reconnect is already in progress + return if @$scope.connection?.reconnecting + # bail out if we are going to reconnect soon anyway + reconnectingSoon = @$scope.connection?.reconnection_countdown? and @$scope.connection.reconnection_countdown <= 5 + clickedTryNow = options?.force # user requested reconnection + return if reconnectingSoon and not clickedTryNow + # bail out if we tried reconnecting recently + allowedInterval = if clickedTryNow then @MIN_RETRY_INTERVAL else @BACKGROUND_RETRY_INTERVAL + return if @lastConnectionAttempt? and new Date() - @lastConnectionAttempt < allowedInterval + @tryReconnect() + disconnectIfInactive: ()-> @userIsInactive = (new Date() - @lastUserAction) > @disconnectAfterMs if @userIsInactive and @connected From e2c66e8d5687f08bf0e83df5caa49d77cb3424ef Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 31 Oct 2016 15:31:32 +0000 Subject: [PATCH 4/6] keep track of reconnect timer --- .../coffee/ide/connection/ConnectionManager.coffee | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/services/web/public/coffee/ide/connection/ConnectionManager.coffee b/services/web/public/coffee/ide/connection/ConnectionManager.coffee index 987186fd19..232b63e7cc 100644 --- a/services/web/public/coffee/ide/connection/ConnectionManager.coffee +++ b/services/web/public/coffee/ide/connection/ConnectionManager.coffee @@ -192,10 +192,16 @@ define [], () -> , 200) cancelReconnect: () -> - clearTimeout @timeoutId if @timeoutId? - + # clear timeout and set to null so we know there is no countdown running + if @timeoutId? + sl_console.log "[ConnectionManager] cancelling existing reconnect timer" + clearTimeout @timeoutId + @timeoutId = null + decreaseCountdown: () -> + @timeoutId = null return if !@$scope.connection.reconnection_countdown? + sl_console.log "[ConnectionManager] decreasing countdown", @$scope.connection.reconnection_countdown @$scope.$apply () => @$scope.connection.reconnection_countdown-- From 2461c1b7be4b4537a9de193f14f0894d801c20ae Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 31 Oct 2016 15:33:09 +0000 Subject: [PATCH 5/6] add more sl_console debugging to ConnectionManager --- .../web/public/coffee/ide/connection/ConnectionManager.coffee | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/web/public/coffee/ide/connection/ConnectionManager.coffee b/services/web/public/coffee/ide/connection/ConnectionManager.coffee index 232b63e7cc..083e0f061f 100644 --- a/services/web/public/coffee/ide/connection/ConnectionManager.coffee +++ b/services/web/public/coffee/ide/connection/ConnectionManager.coffee @@ -169,9 +169,11 @@ define [], () -> @tryReconnect() disconnect: () -> + sl_console.log "[socket.io] disconnecting client" @ide.socket.disconnect() startAutoReconnectCountdown: () -> + sl_console.log "[ConnectionManager] starting autoreconnect countdown" twoMinutes = 2 * 60 * 1000 if @lastUserAction? and new Date() - @lastUserAction > twoMinutes # between 1 minute and 3 minutes @@ -212,6 +214,7 @@ define [], () -> @timeoutId = setTimeout (=> @decreaseCountdown()), 1000 tryReconnect: () -> + sl_console.log "[ConnectionManager] tryReconnect" @cancelReconnect() delete @$scope.connection.reconnection_countdown return if @connected From b907620fc1bbb552d1a2d9bd443dc3bc2dd39571 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 31 Oct 2016 16:26:08 +0000 Subject: [PATCH 6/6] clean up whitespace --- .../public/coffee/ide/connection/ConnectionManager.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/web/public/coffee/ide/connection/ConnectionManager.coffee b/services/web/public/coffee/ide/connection/ConnectionManager.coffee index 083e0f061f..61d3d1c37d 100644 --- a/services/web/public/coffee/ide/connection/ConnectionManager.coffee +++ b/services/web/public/coffee/ide/connection/ConnectionManager.coffee @@ -28,8 +28,8 @@ define [], () -> @connected = false @userIsInactive = false @gracefullyReconnecting = false - - @$scope.connection = + + @$scope.connection = reconnecting: false # If we need to force everyone to reload the editor forced_disconnect: false @@ -262,7 +262,7 @@ define [], () -> setTimeout () => @reconnectGracefully() , @RECONNECT_GRACEFULLY_RETRY_INTERVAL - + _reconnectGracefullyNow: () -> @gracefullyReconnecting = true @reconnectGracefullyStarted = null