movex icon indicating copy to clipboard operation
movex copied to clipboard

[Master/Client Connection] Optimize the `resyncLocalState` on ack response

Open GabrielCTroia opened this issue 7 months ago • 0 comments

What

Currently, when dispatching an Action (i.e. "emitActionDispatch" event), the following steps take place:

  1. the Client calculates the next checked state and renders it
  2. the Client sends the emitActionDispatch & waits for the Master's ack response with the calculated state checksum
  3. once received it checks it against the local state checksum & in case they don't match:
  • sends another call to the Master asking for the latest Master state, in order to sync it.
  1. waits for the master's response before it renders it to the UI

But these steps can be reduced a bit like so:

  1. the Client calculates the next checked state and renders it, as before
  2. the Client sends the emitActionDispatch event including the local Checksum in its payload and the matching get's calculated on the Master now
  3. The Master always returns the next checked state, not just the checksum.
  4. The client verifies the 2 checksums match, and in case they don't match, it simply replaces the local one with the master one already present in the emitActionDispatch ack response. No need to send any other requests and wait for a response

This simplifies the flow while also not creating more traffic jam, because the client eagerly renders the local state, therefore in case of a mismatch, the fresh state renders is even faster.

Bonus

An extra optimization is possible, where the server checks the 2 checksums and only sends the next state in case there is a diff.

Why

To optimize the network traffic and simplify the flow

GabrielCTroia avatar Jul 20 '24 17:07 GabrielCTroia