Skip to content

Commit

Permalink
delay afterStoreDocument to allow redis to fully finish syncing when …
Browse files Browse the repository at this point in the history
…using directConnection (similaly to onDisconnect) (#819)
  • Loading branch information
janthurau authored Apr 25, 2024
1 parent 5df52d7 commit 420e4e2
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/extension-redis/src/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export class Redis implements Extension {
/**
* Release the Redis lock, so other instances can store documents.
*/
async afterStoreDocument({ documentName }: afterStoreDocumentPayload) {
async afterStoreDocument({ documentName, socketId }: afterStoreDocumentPayload) {
this.locks.get(this.lockKey(documentName))?.unlock()
.catch(() => {
// Not able to unlock Redis. The lock will expire after ${lockTimeout} ms.
Expand All @@ -252,6 +252,14 @@ export class Redis implements Extension {
.finally(() => {
this.locks.delete(this.lockKey(documentName))
})

// if the change was initiated by a directConnection, we need to delay this hook to make sure sync can finish first.
// for provider connections, this usually happens in the onDisconnect hook
if (socketId === 'server') {
await new Promise(resolve => {
setTimeout(() => resolve(''), this.configuration.disconnectDelay)
})
}
}

/**
Expand Down

0 comments on commit 420e4e2

Please sign in to comment.