Skip to content

Commit

Permalink
Add syncback rate limits, ignore CurrentCamera, fix service writing
Browse files Browse the repository at this point in the history
  • Loading branch information
DervexDev committed May 5, 2024
1 parent 2b4b198 commit 9a8a940
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 15 deletions.
17 changes: 14 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased]

### Added

- "Creating" services is now possible via hydration

### Fixed

- `workspace.Camera` is now ignored by syncback so HTTP request limit no longer gets exceeded
- Removing services no longer causes Argon to crash, instead the user receives a warning

### Changed

- Two-way sync is now rate limited to a maximum of 2 requests per second

## [2.0.3] - 2024-05-04

### Fixed
Expand Down Expand Up @@ -79,12 +92,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

### Fixed

- Locked property error ([#1])
- Locked property error ([#1](https://github.com/argon-rbx/argon-roblox/issues/1))
- Sync confirmation cancellation not working
- Text inputs in the Settings widget

[#1]: https://github.com/argon-rbx/argon-roblox/issues/1

## [2.0.0-pre2] - 2024-03-19

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions src/App/.src.lua
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ function App:connect()
self.core:onSync(function(kind, data)
self.lastSync:set(os.time())
self.lastSyncKind:set(
kind == ('SyncChanges' or 'SyncDetails') and 'Server'
or (kind == 'Add' or kind == 'Change' or kind == 'Remove') and 'Client'
(kind == 'SyncChanges' or kind == 'SyncDetails') and 'Server'
or kind == 'SyncbackChanges' and 'Client'
or 'Unknown'
)

Expand Down
32 changes: 24 additions & 8 deletions src/Core/.src.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local Tree = require(script.Tree)
local Error = require(script.Error)

local CHANGES_TRESHOLD = 5
local SYNCBACK_RATE = 0.5

local Core = {
Status = {
Expand Down Expand Up @@ -285,9 +286,30 @@ function Core:__startSyncLoop()
end

function Core:__startSyncbackLoop()
local aggregateChanges = Changes.new()

return Promise.new(function(resolve)
task.spawn(function()
while self.status == Core.Status.Connected do
task.wait(SYNCBACK_RATE)

if aggregateChanges:isEmpty() then
continue
end

local changes = aggregateChanges
aggregateChanges = Changes.new()

self.client:write(changes):catch(function(err)
Log.warn('Failed to write changes to the server:', err)
end)

self.__sync('kind', changes)
end
end)

while self.status == Core.Status.Connected do
local event = self.watcher:awaitEvent() :: Types.WatcherEvent
local event = self.watcher:listen() :: Types.WatcherEvent

if self.processor.read.isPaused then
continue
Expand Down Expand Up @@ -318,13 +340,7 @@ function Core:__startSyncbackLoop()
end
end

if not changes:isEmpty() then
self.client:write(changes):catch(function(err)
Log.warn('Failed to write changes to the server:', err)
end)

self.__sync(kind, changes)
end
aggregateChanges:join(changes)
end

resolve()
Expand Down
3 changes: 3 additions & 0 deletions src/Core/Error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ local Error = {
NoInstanceAdd = 'Tried to add an instance whose parent that does not exist in the tree: $1',
NoInstanceUpdate = 'Tried to update an instance that does not exist in the tree: $1',
NoInstanceRemove = 'Tried to remove an instance that does not exist in the tree: $1',
NotCreatable = 'Tried to create an instance of class: $1 that is not creatable',
NotRemovable = 'Tried to remove an instance of class: $1 that is not removable',
HydrationFailed = 'Failed to hydrate root service, snapshot: $1',
}

local function eq(self: Error, other: Error): boolean
Expand Down
30 changes: 29 additions & 1 deletion src/Core/Processor/Write.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,28 @@ function WriteProcessor:applyAddition(snapshot: Types.AddedSnapshot)
return
end

local instance = Instance.new(snapshot.class)
local instance

if Dom.isCreatable(snapshot.class) then
instance = Instance.new(snapshot.class)
elseif parent == game then
local service = game:FindFirstChildOfClass(snapshot.class)

if service then
instance = service
else
local err = Error.new(Error.HydrationFailed, snapshot)
Log.warn(err)

return
end
else
local err = Error.new(Error.NotCreatable, snapshot.class)
Log.warn(err)

return
end

instance.Name = snapshot.name

for property, value in pairs(snapshot.properties) do
Expand Down Expand Up @@ -217,6 +238,13 @@ function WriteProcessor:applyRemoval(object: Types.Ref | Instance)
return
end

if not Dom.isCreatable(instance.ClassName) then
local err = Error.new(Error.NotRemovable, instance.ClassName)
Log.warn(err)

return
end

self.lastRemovedInstance = {
instance = instance:Clone(),
parent = instance.Parent,
Expand Down
6 changes: 5 additions & 1 deletion src/Watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ function Watcher:stop()
self.connections = {}
end

function Watcher:awaitEvent(): Types.WatcherEvent
function Watcher:listen(): Types.WatcherEvent
return self.signal:Wait()
end

function Watcher:__connectEvents(instance: Instance)
if instance == workspace.CurrentCamera then
return
end

local connections = {}

table.insert(
Expand Down

0 comments on commit 9a8a940

Please sign in to comment.