Improve Rojo sync reliability (#739)

Uses non-recursive main loop and avoids hanging promises
This commit is contained in:
boatbomber
2023-07-16 14:59:30 -07:00
committed by GitHub
parent dd01a9bef3
commit 8662d2227c
2 changed files with 35 additions and 29 deletions

View File

@@ -11,13 +11,6 @@ local validateApiInfo = Types.ifEnabled(Types.ApiInfoResponse)
local validateApiRead = Types.ifEnabled(Types.ApiReadResponse) local validateApiRead = Types.ifEnabled(Types.ApiReadResponse)
local validateApiSubscribe = Types.ifEnabled(Types.ApiSubscribeResponse) local validateApiSubscribe = Types.ifEnabled(Types.ApiSubscribeResponse)
--[[
Returns a promise that will never resolve nor reject.
]]
local function hangingPromise()
return Promise.new(function() end)
end
local function rejectFailedRequests(response) local function rejectFailedRequests(response)
if response.code >= 400 then if response.code >= 400 then
local message = string.format("HTTP %s:\n%s", tostring(response.code), response.body) local message = string.format("HTTP %s:\n%s", tostring(response.code), response.body)
@@ -212,12 +205,8 @@ function ApiContext:retrieveMessages()
local function sendRequest() local function sendRequest()
local request = Http.get(url) local request = Http.get(url)
:catch(function(err) :catch(function(err)
if err.type == Http.Error.Kind.Timeout then if err.type == Http.Error.Kind.Timeout and self.__connected then
if self.__connected then return sendRequest()
return sendRequest()
else
return hangingPromise()
end
end end
return Promise.reject(err) return Promise.reject(err)

View File

@@ -299,27 +299,44 @@ function ServeSession:__initialSync(serverInfo)
end end
function ServeSession:__mainSyncLoop() function ServeSession:__mainSyncLoop()
return self.__apiContext:retrieveMessages() return Promise.new(function(resolve, reject)
:andThen(function(messages) while self.__status == Status.Connected do
Log.trace("Serve session {} retrieved {} messages", tostring(self), #messages) local success, result = self.__apiContext:retrieveMessages()
:andThen(function(messages)
if self.__status == Status.Disconnected then
-- In the time it took to retrieve messages, we disconnected
-- so we just resolve immediately without patching anything
return
end
for _, message in ipairs(messages) do Log.trace("Serve session {} retrieved {} messages", tostring(self), #messages)
local unappliedPatch = self.__reconciler:applyPatch(message)
if not PatchSet.isEmpty(unappliedPatch) then for _, message in messages do
Log.warn("Could not apply all changes requested by the Rojo server:\n{}", local unappliedPatch = self.__reconciler:applyPatch(message)
PatchSet.humanSummary(self.__instanceMap, unappliedPatch))
end
if self.__patchAppliedCallback then if not PatchSet.isEmpty(unappliedPatch) then
pcall(self.__patchAppliedCallback, message, unappliedPatch) Log.warn("Could not apply all changes requested by the Rojo server:\n{}",
end PatchSet.humanSummary(self.__instanceMap, unappliedPatch))
end
if self.__patchAppliedCallback then
pcall(self.__patchAppliedCallback, message, unappliedPatch)
end
end
end):await()
if self.__status == Status.Disconnected then
-- If we are no longer connected after applying, we stop silently
-- without checking for errors as they are no longer relevant
break
elseif success == false then
reject(result)
end end
end
if self.__status ~= Status.Disconnected then -- We are no longer connected, so we resolve the promise
return self:__mainSyncLoop() resolve()
end end)
end)
end end
function ServeSession:__stopInternal(err) function ServeSession:__stopInternal(err)