chore: Reduce lock contention in collaboration server (#10778)

* chore: Reduce lock contention in collaboration server

* Review suggestions
This commit is contained in:
Tom Moor
2025-12-03 20:54:27 -05:00
committed by GitHub
parent d02f35b770
commit 6976f01d7d

View File

@@ -29,8 +29,27 @@ export default class PersistenceExtension implements Extension {
return;
}
// First, try to find the document without a lock to check if it has state
const documentWithoutLock = await Document.unscoped().findOne({
attributes: ["state"],
rejectOnEmpty: true,
where: {
id: documentId,
},
});
// If the document already has state, we can return it without needing a transaction
if (documentWithoutLock.state) {
const ydoc = new Y.Doc();
Logger.info("database", `Document ${documentId} is in database state`);
Y.applyUpdate(ydoc, documentWithoutLock.state);
return ydoc;
}
// If the document doesn't have state yet, we need to acquire a lock and create it
return await sequelize.transaction(async (transaction) => {
const document = await Document.scope("withState").findOne({
const document = await Document.unscoped().findOne({
attributes: ["id", "state", "content", "text"],
transaction,
lock: transaction.LOCK.UPDATE,
rejectOnEmpty: true,
@@ -38,8 +57,9 @@ export default class PersistenceExtension implements Extension {
id: documentId,
},
});
let ydoc;
// Double-check the state in case another process created it
if (document.state) {
ydoc = new Y.Doc();
Logger.info("database", `Document ${documentId} is in database state`);