From f4358acffcb236879a7467b3b13b6ce5d211a536 Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com> Date: Wed, 18 Jun 2025 15:18:51 +0300 Subject: [PATCH] [CAE-1046] fix(loading): cache the loaded flag for slave nodes (#3410) * fix(loading): cache the loaded flag for slave nodes * fix(lint): make linter happy --- osscluster.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/osscluster.go b/osscluster.go index 0dce50a4..55017d8b 100644 --- a/osscluster.go +++ b/osscluster.go @@ -340,6 +340,7 @@ type clusterNode struct { latency uint32 // atomic generation uint32 // atomic failing uint32 // atomic + loaded uint32 // atomic // last time the latency measurement was performed for the node, stored in nanoseconds // from epoch @@ -406,6 +407,7 @@ func (n *clusterNode) Latency() time.Duration { func (n *clusterNode) MarkAsFailing() { atomic.StoreUint32(&n.failing, uint32(time.Now().Unix())) + atomic.StoreUint32(&n.loaded, 0) } func (n *clusterNode) Failing() bool { @@ -449,11 +451,21 @@ func (n *clusterNode) SetLastLatencyMeasurement(t time.Time) { } func (n *clusterNode) Loading() bool { + loaded := atomic.LoadUint32(&n.loaded) + if loaded == 1 { + return false + } + + // check if the node is loading ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) defer cancel() err := n.Client.Ping(ctx).Err() - return err != nil && isLoadingError(err) + loading := err != nil && isLoadingError(err) + if !loading { + atomic.StoreUint32(&n.loaded, 1) + } + return loading } //------------------------------------------------------------------------------