From b4e0ebd600bb0890ea6a7139f064148a55bdb914 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Thu, 9 May 2024 13:49:22 +0300 Subject: [PATCH] Persist & replicate in parallel --- antietcd.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/antietcd.js b/antietcd.js index 1be51f7..101cf03 100644 --- a/antietcd.js +++ b/antietcd.js @@ -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; + } } }