From 337dd934154b8f2a5c03ff5c633d6761214eb1a5 Mon Sep 17 00:00:00 2001 From: Prabhjot Singh Sethi Date: Mon, 14 Jul 2025 16:43:21 +0000 Subject: [PATCH] Enable majority write concern by default on the client Ensuring DB functions appropriately while mongodb being deployed in HA mode Signed-off-by: Prabhjot Singh Sethi --- db/mongo.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/db/mongo.go b/db/mongo.go index 09ed6f0..f48154b 100644 --- a/db/mongo.go +++ b/db/mongo.go @@ -16,8 +16,10 @@ import ( "go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/v2/mongo/options" + "go.mongodb.org/mongo-driver/v2/mongo/writeconcern" "github.com/go-core-stack/core/errors" + "github.com/go-core-stack/core/utils" ) type mongoCollection struct { @@ -375,6 +377,16 @@ func NewMongoClient(conf *MongoConfig) (StoreClient, error) { Password: conf.Password, }) + // by default ensure majority write concern and journal to be true + // for HA to function appropriately + // + // while sync package might require a better consistency than just + // majority, so we need to carefully evaluate right configuration + // for the same + wc := writeconcern.Majority() + wc.Journal = utils.BoolP(true) + clientOptions.SetWriteConcern(wc) + client, err := mongo.Connect(clientOptions) if err != nil { return nil, err