Repository navigation
v1.52.0
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
BackplanebeforeOnChangeis called (it was after). AnOnChangethat panics no longer keeps the update from other instances. Server.Closenow waits for openDocuments and server writes in progress, within its context.cmd/yserveandcmd/ygo-serverare unchanged.
Added
Server.Apply(ctx, docName, fn)runsfnin one write transaction and returns the update it made, or nil when nothing changed.- Roots come from
ygo.MapInand the other*Inaccessors. - Edits made before
fnreturns an error or panics are committed and sent. Then the error is returned, or the panic goes on.
- Roots come from
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.
- The update must decode, with nothing after it (
Server.OpenDocument(ctx, docName)returns aDocumentthat holds the document resident until itsClose, across its clients' disconnects. It hasApply,ApplyUpdateandState(the server's copy as one V1 update).Closewaits 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.
- It refuses while clients are connected (
Options.OnServerWritesees each write before it happens and can refuse it (ErrWriteRefused).- Each write's transaction has a new
*server.ServerOriginas 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 untilCloseDocumentorClose, so the write is not lost.
- On a server without a
- Errors:
- a
Storeerror after the write was applied and sent comes back wrapped inErrNotStored; - after
Close, the document API returnsErrServerClosed, including for Documents opened before it.
- a
Tests
-
A server write reaches every recipient, with the update
Applyreturns:- 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.
-
OnServerWriterefusals, and forced and refusedCloseDocument. -
Held documents across disconnects, and shutdown while writes and Documents are open.
-
Panicking
OnLoadDocument,OnDisconnectandOnChangehooks. -
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
CloseDocumentcloses 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.