From 30fd62a46cac49e4c58a67317edb010a1d489984 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Fri, 9 Oct 2026 11:10:45 +0700 Subject: [PATCH] fix(couchbase): increment atomically under the tick context Increment read the counter and wrote it back, ignoring the tick context, and treated any read error as a missing document, so a timeout reset the counter to 1. A single binary increment is atomic, honours the context, and surfaces real errors. The cluster is also assigned only after a successful connect so a failed connect is not closed twice. --- adapter/couchbase.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/adapter/couchbase.go b/adapter/couchbase.go index d8dbbd5..c045913 100644 --- a/adapter/couchbase.go +++ b/adapter/couchbase.go @@ -98,33 +98,24 @@ func (a *couchbaseAdapter) Connect(ctx context.Context) error { if err := a.ensureScopeAndCollection(ctx, b); err != nil { return err } - a.cluster = cluster a.coll = b.Scope(a.scope).Collection(a.collName) if err := a.ensureDocument(ctx); err != nil { return err } + // Set only on success: the deferred cleanup closes a failed cluster, and + // Close must not close it again. + a.cluster = cluster connected = true return nil } -func (a *couchbaseAdapter) Increment(_ context.Context) (int64, error) { - docOut, err := a.coll.Get(a.docID, &gocb.GetOptions{}) +func (a *couchbaseAdapter) Increment(ctx context.Context) (int64, error) { + // One atomic server-side increment; ensureDocument seeded the counter. + res, err := a.coll.Binary().Increment(a.docID, &gocb.IncrementOptions{Delta: 1, Initial: 1, Context: ctx}) if err != nil { - // On first run the doc may not exist; seed at 1. - if _, upErr := a.coll.Upsert(a.docID, uint64(1), &gocb.UpsertOptions{}); upErr != nil { - return 0, upErr - } - return 1, nil - } - var current uint64 - if err := docOut.Content(¤t); err != nil { return 0, err } - current++ - if _, err := a.coll.Upsert(a.docID, current, &gocb.UpsertOptions{}); err != nil { - return 0, err - } - return int64(current), nil + return int64(res.Content()), nil } func (a *couchbaseAdapter) Close(_ context.Context) error {