fix(server): wrap updates applying in a transaction (#4922)

This commit is contained in:
liuyi
2023-11-13 16:49:30 +08:00
committed by 李华桥
parent 76b585d1ef
commit c44a9a4903

View File

@@ -8,7 +8,13 @@ import {
import { Snapshot, Update } from '@prisma/client'; import { Snapshot, Update } from '@prisma/client';
import { chunk } from 'lodash-es'; import { chunk } from 'lodash-es';
import { defer, retry } from 'rxjs'; import { defer, retry } from 'rxjs';
import { applyUpdate, Doc, encodeStateAsUpdate, encodeStateVector } from 'yjs'; import {
applyUpdate,
Doc,
encodeStateAsUpdate,
encodeStateVector,
transact,
} from 'yjs';
import { Config } from '../../config'; import { Config } from '../../config';
import { Metrics } from '../../metrics/metrics'; import { Metrics } from '../../metrics/metrics';
@@ -84,6 +90,7 @@ export class DocManager implements OnModuleInit, OnModuleDestroy {
const next = () => { const next = () => {
const updates = chunks.shift(); const updates = chunks.shift();
if (updates?.length) { if (updates?.length) {
transact(doc, () => {
updates.forEach(u => { updates.forEach(u => {
try { try {
applyUpdate(doc, u); applyUpdate(doc, u);
@@ -95,6 +102,7 @@ export class DocManager implements OnModuleInit, OnModuleDestroy {
); );
} }
}); });
});
// avoid applying too many updates in single round which will take the whole cpu time like dead lock // avoid applying too many updates in single round which will take the whole cpu time like dead lock
setImmediate(() => { setImmediate(() => {