@@ -364,8 +364,7 @@ type clusterNode struct {
364
364
failing uint32 // atomic
365
365
loaded uint32 // atomic
366
366
367
- // last time the latency measurement was performed for the node, stored in nanoseconds
368
- // from epoch
367
+ // last time the latency measurement was performed for the node, stored in nanoseconds from epoch
369
368
lastLatencyMeasurement int64 // atomic
370
369
}
371
370
@@ -502,13 +501,12 @@ type clusterNodes struct {
502
501
closed bool
503
502
onNewNode []func (rdb * Client )
504
503
505
- _generation uint32 // atomic
504
+ generation uint32 // atomic
506
505
}
507
506
508
507
func newClusterNodes (opt * ClusterOptions ) * clusterNodes {
509
508
return & clusterNodes {
510
- opt : opt ,
511
-
509
+ opt : opt ,
512
510
addrs : opt .Addrs ,
513
511
nodes : make (map [string ]* clusterNode ),
514
512
}
@@ -568,12 +566,11 @@ func (c *clusterNodes) Addrs() ([]string, error) {
568
566
}
569
567
570
568
func (c * clusterNodes ) NextGeneration () uint32 {
571
- return atomic .AddUint32 (& c ._generation , 1 )
569
+ return atomic .AddUint32 (& c .generation , 1 )
572
570
}
573
571
574
572
// GC removes unused nodes.
575
573
func (c * clusterNodes ) GC (generation uint32 ) {
576
- //nolint:prealloc
577
574
var collected []* clusterNode
578
575
579
576
c .mu .Lock ()
@@ -626,23 +623,20 @@ func (c *clusterNodes) GetOrCreate(addr string) (*clusterNode, error) {
626
623
fn (node .Client )
627
624
}
628
625
629
- c .addrs = appendIfNotExists (c .addrs , addr )
626
+ c .addrs = appendIfNotExist (c .addrs , addr )
630
627
c .nodes [addr ] = node
631
628
632
629
return node , nil
633
630
}
634
631
635
632
func (c * clusterNodes ) get (addr string ) (* clusterNode , error ) {
636
- var node * clusterNode
637
- var err error
638
633
c .mu .RLock ()
634
+ defer c .mu .RUnlock ()
635
+
639
636
if c .closed {
640
- err = pool .ErrClosed
641
- } else {
642
- node = c .nodes [addr ]
637
+ return nil , pool .ErrClosed
643
638
}
644
- c .mu .RUnlock ()
645
- return node , err
639
+ return c .nodes [addr ], nil
646
640
}
647
641
648
642
func (c * clusterNodes ) All () ([]* clusterNode , error ) {
@@ -673,8 +667,9 @@ func (c *clusterNodes) Random() (*clusterNode, error) {
673
667
//------------------------------------------------------------------------------
674
668
675
669
type clusterSlot struct {
676
- start , end int
677
- nodes []* clusterNode
670
+ start int
671
+ end int
672
+ nodes []* clusterNode
678
673
}
679
674
680
675
type clusterSlotSlice []* clusterSlot
@@ -734,9 +729,9 @@ func newClusterState(
734
729
nodes = append (nodes , node )
735
730
736
731
if i == 0 {
737
- c .Masters = appendUniqueNode (c .Masters , node )
732
+ c .Masters = appendIfNotExist (c .Masters , node )
738
733
} else {
739
- c .Slaves = appendUniqueNode (c .Slaves , node )
734
+ c .Slaves = appendIfNotExist (c .Slaves , node )
740
735
}
741
736
}
742
737
@@ -1295,7 +1290,7 @@ func (c *ClusterClient) loadState(ctx context.Context) (*clusterState, error) {
1295
1290
continue
1296
1291
}
1297
1292
1298
- return newClusterState (c .nodes , slots , node . Client . opt . Addr )
1293
+ return newClusterState (c .nodes , slots , addr )
1299
1294
}
1300
1295
1301
1296
/*
@@ -2017,7 +2012,7 @@ func (c *ClusterClient) MasterForKey(ctx context.Context, key string) (*Client,
2017
2012
if err != nil {
2018
2013
return nil , err
2019
2014
}
2020
- return node .Client , err
2015
+ return node .Client , nil
2021
2016
}
2022
2017
2023
2018
func (c * ClusterClient ) context (ctx context.Context ) context.Context {
@@ -2027,26 +2022,13 @@ func (c *ClusterClient) context(ctx context.Context) context.Context {
2027
2022
return context .Background ()
2028
2023
}
2029
2024
2030
- func appendUniqueNode (nodes []* clusterNode , node * clusterNode ) []* clusterNode {
2031
- for _ , n := range nodes {
2032
- if n == node {
2033
- return nodes
2034
- }
2035
- }
2036
- return append (nodes , node )
2037
- }
2038
-
2039
- func appendIfNotExists (ss []string , es ... string ) []string {
2040
- loop:
2041
- for _ , e := range es {
2042
- for _ , s := range ss {
2043
- if s == e {
2044
- continue loop
2045
- }
2025
+ func appendIfNotExist [T comparable ](vals []T , newVal T ) []T {
2026
+ for _ , v := range vals {
2027
+ if v == newVal {
2028
+ return vals
2046
2029
}
2047
- ss = append (ss , e )
2048
2030
}
2049
- return ss
2031
+ return append ( vals , newVal )
2050
2032
}
2051
2033
2052
2034
//------------------------------------------------------------------------------
0 commit comments