Persist & replicate in parallel

master
Vitaliy Filippov 2024-05-09 13:49:22 +03:00
parent 2b6cc135d3
commit b4e0ebd600
1 changed files with 21 additions and 5 deletions

View File

@ -86,13 +86,29 @@ class AntiEtcd
async persistAndReplicate(msg)
{
if (this.persistence)
{
await this.persistence.persistChange(msg);
}
let res = [];
if (this.cluster)
{
await this.cluster.replicateChange(msg);
// We have to guarantee that replication is processed sequentially
// So we have to send messages without first awaiting for anything!
res.push(this.cluster.replicateChange(msg));
}
if (this.persistence)
{
res.push(this.persistence.persistChange(msg));
}
if (res.length)
{
res = await Promise.allSettled(res);
const errors = res.filter(r => r.status == 'rejected');
if (errors.length)
{
for (const e of errors)
{
console.error(e.reason);
}
throw errors[0].reason;
}
}
}