Skip to content

v1.52.0

Choose a tag to compare

@Deln0r Deln0r released this 23 Nov 15:27
· 6 commits to main since this release

Server code can now change documents the way a client does. A server write is applied to the server's copy and queued to every connection of the document: y-websocket and Hocuspocus, read-only viewers included. It is also stored in the Store, published on the Backplane and reported to OnChange.

Upgrade impact

  • On the client path, a stored update is now published on the Backplane before OnChange is called (it was after). An OnChange that panics no longer keeps the update from other instances.
  • Server.Close now waits for open Documents and server writes in progress, within its context.
  • cmd/yserve and cmd/ygo-server are unchanged.

Added

  • Server.Apply(ctx, docName, fn) runs fn in one write transaction and returns the update it made, or nil when nothing changed.
    • Roots come from ygo.MapIn and the other *In accessors.
    • Edits made before fn returns an error or panics are committed and sent. Then the error is returned, or the panic goes on.
  • Server.ApplyUpdate(ctx, docName, update) checks a V1 update, applies it to the server's copy and sends it as given.
    • The update must decode, with nothing after it (ErrInvalidUpdate).
    • Unlike a fan-out that does not apply, a client that connects later gets the update in its sync.
  • Server.OpenDocument(ctx, docName) returns a Document that holds the document resident until its Close, across its clients' disconnects. It has Apply, ApplyUpdate and State (the server's copy as one V1 update).
    • Close waits for calls in progress.
  • Server.CloseDocument(ctx, docName, force) takes a document out of the server.
    • It refuses while clients are connected (ErrDocumentHasConnections), unless forced.
    • A forced close shuts, with 1001, the document's y-websocket connections and the Hocuspocus sockets holding it. Those sockets' other documents reconnect, as providers do after any drop.
    • It waits for those connections and for open Documents, then evicts the document with a final flush. Connections that arrive meanwhile wait, then load the document afresh.
  • Options.OnServerWrite sees each write before it happens and can refuse it (ErrWriteRefused).
  • Each write's transaction has a new *server.ServerOrigin as its origin.
  • Documents no client holds are loaded for the write as a connection would load them, then evicted or parked as after a last disconnect.
    • On a server without a Store, a document a server write changed stays resident until CloseDocument or Close, so the write is not lost.
  • Errors:
    • a Store error after the write was applied and sent comes back wrapped in ErrNotStored;
    • after Close, the document API returns ErrServerClosed, including for Documents opened before it.

Tests

  • A server write reaches every recipient, with the update Apply returns:

    • a y-websocket client;
    • a Hocuspocus document;
    • a client of a second instance on the same Backplane;
    • a read-only viewer;
    • a client that has not finished its sync.

    It is stored and reported once.

  • Loading, retention and eviction of documents no client holds.

  • Edits before an error or a panic are sent; no change sends nothing.

  • A pending-only update is held and stored. Invalid and trailing bytes are refused.

  • OnServerWrite refusals, and forced and refused CloseDocument.

  • Held documents across disconnects, and shutdown while writes and Documents are open.

  • Panicking OnLoadDocument, OnDisconnect and OnChange hooks.

  • A second Hocuspocus key attaching while its document closes.

  • Concurrent writes racing connecting clients, and forced closes racing connections, all under the race detector.

  • 20 mutation controls, each of which fails a test.

Known and not fixed

  • A forced CloseDocument closes whole Hocuspocus sockets. Releasing one document of a socket from outside its read loop let a frame in progress land on the closed copy. A per-document close needs a session loop that takes close requests between frames (docs/tech-debt.md).

Full notes in CHANGELOG.md.