From 52754e038a3d025cd22d9745127c1234b1eb8790 Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 24 Oct 2016 16:36:40 +0100 Subject: [PATCH 1/2] Reconnect gracefully when asked to by real-time --- .../ide/connection/ConnectionManager.coffee | 29 +++++++++++++++++-- .../ide/references/ReferencesManager.coffee | 7 ++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/services/web/public/coffee/ide/connection/ConnectionManager.coffee b/services/web/public/coffee/ide/connection/ConnectionManager.coffee index f3d14a3e3d..97530b8e4e 100644 --- a/services/web/public/coffee/ide/connection/ConnectionManager.coffee +++ b/services/web/public/coffee/ide/connection/ConnectionManager.coffee @@ -27,6 +27,7 @@ define [], () -> @connected = false @userIsInactive = false + @gracefullyReconnecting = false @$scope.connection = reconnecting: false @@ -54,6 +55,7 @@ define [], () -> @ide.socket.on "connect", () => sl_console.log "[socket.io connect] Connected" @connected = true + @gracefullyReconnecting = false @ide.pushEvent("connected") @$scope.$apply () => @@ -81,7 +83,7 @@ define [], () -> @$scope.$apply () => @$scope.connection.reconnecting = false - if !$scope.connection.forced_disconnect and !@userIsInactive + if !$scope.connection.forced_disconnect and !@userIsInactive and !@gracefullyReconnecting @startAutoReconnectCountdown() @ide.socket.on 'forceDisconnect', (message) => @@ -97,7 +99,11 @@ define [], () -> setTimeout () -> location.reload() , 10 * 1000 - + + @ide.socket.on "reconnectGracefully", () => + sl_console.log "Reconnect gracefully" + @reconnectGracefully() + joinProject: () -> sl_console.log "[joinProject] joining..." @ide.socket.emit 'joinProject', { @@ -180,3 +186,22 @@ define [], () -> @$scope.$apply () => @$scope.connection.inactive_disconnect = true + RECONNECT_GRACEFULLY_RETRY_INTERVAL: 5000 # ms + MAX_RECONNECT_GRACEFULLY_INTERVAL: 60 * 5 * 1000 # 5 minutes + reconnectGracefully: () -> + @reconnectGracefullyStarted ?= new Date() + userIsInactive = (new Date() - @lastUserAction) > @RECONNECT_GRACEFULLY_RETRY_INTERVAL + maxIntervalReached = (new Date() - @reconnectGracefullyStarted) > @MAX_RECONNECT_GRACEFULLY_INTERVAL + if userIsInactive or maxIntervalReached + sl_console.log "[reconnectGracefully] User didn't do anything for last 5 seconds, reconnecting" + @_reconnectGracefullyNow() + else + sl_console.log "[reconnectGracefully] User is working, will try again in 5 seconds" + setTimeout () => + @reconnectGracefully() + , @RECONNECT_GRACEFULLY_RETRY_INTERVAL + + _reconnectGracefullyNow: () -> + @gracefullyReconnecting = true + @reconnectGracefullyStarted = null + @reconnectImmediately() \ No newline at end of file diff --git a/services/web/public/coffee/ide/references/ReferencesManager.coffee b/services/web/public/coffee/ide/references/ReferencesManager.coffee index 2f1e95c5b1..c5d7c2348b 100644 --- a/services/web/public/coffee/ide/references/ReferencesManager.coffee +++ b/services/web/public/coffee/ide/references/ReferencesManager.coffee @@ -17,8 +17,13 @@ define [ # When we join the project: # index all references files # and don't broadcast to all clients + @inited = false @$scope.$on 'project:joined', (e) => - @indexAllReferences(false) + # We only need to grab the references when the editor first loads, + # not on every reconnect + if !@inited + @inited = true + @indexAllReferences(false) setTimeout( (self) -> From 43aeb5a21df2c4f975f05ef84d7904bbadf40be9 Mon Sep 17 00:00:00 2001 From: James Allen Date: Tue, 25 Oct 2016 14:09:36 +0100 Subject: [PATCH 2/2] Clear SERVERID cookie when reconnecting gracefully --- .../web/public/coffee/ide/connection/ConnectionManager.coffee | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/web/public/coffee/ide/connection/ConnectionManager.coffee b/services/web/public/coffee/ide/connection/ConnectionManager.coffee index 97530b8e4e..811587e5ad 100644 --- a/services/web/public/coffee/ide/connection/ConnectionManager.coffee +++ b/services/web/public/coffee/ide/connection/ConnectionManager.coffee @@ -204,4 +204,6 @@ define [], () -> _reconnectGracefullyNow: () -> @gracefullyReconnecting = true @reconnectGracefullyStarted = null + # Clear cookie so we don't go to the same backend server + $.cookie("SERVERID", "", { expires: -1, path: "/" }) @reconnectImmediately() \ No newline at end of file