@@ -342,8 +342,7 @@ type clusterNode struct {
342
342
failing uint32 // atomic
343
343
loaded uint32 // atomic
344
344
345
- // last time the latency measurement was performed for the node, stored in nanoseconds
346
- // from epoch
345
+ // last time the latency measurement was performed for the node, stored in nanoseconds from epoch
347
346
lastLatencyMeasurement int64 // atomic
348
347
}
349
348
@@ -480,13 +479,12 @@ type clusterNodes struct {
480
479
closed bool
481
480
onNewNode []func (rdb * Client )
482
481
483
- _generation uint32 // atomic
482
+ generation uint32 // atomic
484
483
}
485
484
486
485
func newClusterNodes (opt * ClusterOptions ) * clusterNodes {
487
486
return & clusterNodes {
488
- opt : opt ,
489
-
487
+ opt : opt ,
490
488
addrs : opt .Addrs ,
491
489
nodes : make (map [string ]* clusterNode ),
492
490
}
@@ -546,12 +544,11 @@ func (c *clusterNodes) Addrs() ([]string, error) {
546
544
}
547
545
548
546
func (c * clusterNodes ) NextGeneration () uint32 {
549
- return atomic .AddUint32 (& c ._generation , 1 )
547
+ return atomic .AddUint32 (& c .generation , 1 )
550
548
}
551
549
552
550
// GC removes unused nodes.
553
551
func (c * clusterNodes ) GC (generation uint32 ) {
554
- //nolint:prealloc
555
552
var collected []* clusterNode
556
553
557
554
c .mu .Lock ()
@@ -604,23 +601,20 @@ func (c *clusterNodes) GetOrCreate(addr string) (*clusterNode, error) {
604
601
fn (node .Client )
605
602
}
606
603
607
- c .addrs = appendIfNotExists (c .addrs , addr )
604
+ c .addrs = appendIfNotExist (c .addrs , addr )
608
605
c .nodes [addr ] = node
609
606
610
607
return node , nil
611
608
}
612
609
613
610
func (c * clusterNodes ) get (addr string ) (* clusterNode , error ) {
614
- var node * clusterNode
615
- var err error
616
611
c .mu .RLock ()
612
+ defer c .mu .RUnlock ()
613
+
617
614
if c .closed {
618
- err = pool .ErrClosed
619
- } else {
620
- node = c .nodes [addr ]
615
+ return nil , pool .ErrClosed
621
616
}
622
- c .mu .RUnlock ()
623
- return node , err
617
+ return c .nodes [addr ], nil
624
618
}
625
619
626
620
func (c * clusterNodes ) All () ([]* clusterNode , error ) {
@@ -651,8 +645,9 @@ func (c *clusterNodes) Random() (*clusterNode, error) {
651
645
//------------------------------------------------------------------------------
652
646
653
647
type clusterSlot struct {
654
- start , end int
655
- nodes []* clusterNode
648
+ start int
649
+ end int
650
+ nodes []* clusterNode
656
651
}
657
652
658
653
type clusterSlotSlice []* clusterSlot
@@ -712,9 +707,9 @@ func newClusterState(
712
707
nodes = append (nodes , node )
713
708
714
709
if i == 0 {
715
- c .Masters = appendUniqueNode (c .Masters , node )
710
+ c .Masters = appendIfNotExist (c .Masters , node )
716
711
} else {
717
- c .Slaves = appendUniqueNode (c .Slaves , node )
712
+ c .Slaves = appendIfNotExist (c .Slaves , node )
718
713
}
719
714
}
720
715
@@ -1273,7 +1268,7 @@ func (c *ClusterClient) loadState(ctx context.Context) (*clusterState, error) {
1273
1268
continue
1274
1269
}
1275
1270
1276
- return newClusterState (c .nodes , slots , node . Client . opt . Addr )
1271
+ return newClusterState (c .nodes , slots , addr )
1277
1272
}
1278
1273
1279
1274
/*
@@ -1995,7 +1990,7 @@ func (c *ClusterClient) MasterForKey(ctx context.Context, key string) (*Client,
1995
1990
if err != nil {
1996
1991
return nil , err
1997
1992
}
1998
- return node .Client , err
1993
+ return node .Client , nil
1999
1994
}
2000
1995
2001
1996
func (c * ClusterClient ) context (ctx context.Context ) context.Context {
@@ -2005,26 +2000,13 @@ func (c *ClusterClient) context(ctx context.Context) context.Context {
2005
2000
return context .Background ()
2006
2001
}
2007
2002
2008
- func appendUniqueNode (nodes []* clusterNode , node * clusterNode ) []* clusterNode {
2009
- for _ , n := range nodes {
2010
- if n == node {
2011
- return nodes
2012
- }
2013
- }
2014
- return append (nodes , node )
2015
- }
2016
-
2017
- func appendIfNotExists (ss []string , es ... string ) []string {
2018
- loop:
2019
- for _ , e := range es {
2020
- for _ , s := range ss {
2021
- if s == e {
2022
- continue loop
2023
- }
2003
+ func appendIfNotExist [T comparable ](vals []T , newVal T ) []T {
2004
+ for _ , v := range vals {
2005
+ if v == newVal {
2006
+ return vals
2024
2007
}
2025
- ss = append (ss , e )
2026
2008
}
2027
- return ss
2009
+ return append ( vals , newVal )
2028
2010
}
2029
2011
2030
2012
//------------------------------------------------------------------------------
0 commit comments