You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit fe8cc3b
Browse filesBrowse the repository at this point in the historyBrowse files
perf(coderd/x/chatd): make chat stream reads lock-free
ChatMachine.ReadLock took a FOR SHARE row lock (GetChatByIDForShare) to
give multi-statement readers a consistent snapshot. The stream loop runs
this on every snapshot_version bump for every subscriber, so the read
serialized against the FOR UPDATE writers and showed up as heavy lock
wait under load.
Run the readers in a read-only REPEATABLE READ transaction instead, as
ChatMachine.ReadSnapshot. Snapshot isolation gives the same
multi-statement consistency without a row lock, so reads no longer block
or get blocked by writers. Ordering is still handled by the
snapshot_version watermark, so an older snapshot triggers a later
refetch rather than an inconsistent read.
The lock statement also doubled as an existence check that returned
ErrChatNotFound before the callback ran. Nothing needs it: every caller
reads the chat as its first statement and already handles sql.ErrNoRows
(the task loaders map it to errTaskExpectedExit, the HTTP handler maps
it to 404), so ReadSnapshot opens the transaction and runs the callback,
saving a round trip per stream sync.
Two concurrency tests pin the guarantees: an open ReadSnapshot neither
blocks a concurrent Update nor observes its commit (and the next read
does), and a ReadSnapshot completes while another transaction holds the
row's FOR UPDATE lock. Both fail against the FOR SHARE implementation.
0 commit comments