@@ -364,8 +364,7 @@ type clusterNode struct {
364364 failing uint32 // atomic
365365 loaded uint32 // atomic
366366
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
369368 lastLatencyMeasurement int64 // atomic
370369}
371370
@@ -502,13 +501,12 @@ type clusterNodes struct {
502501 closed bool
503502 onNewNode []func (rdb * Client )
504503
505- _generation uint32 // atomic
504+ generation uint32 // atomic
506505}
507506
508507func newClusterNodes (opt * ClusterOptions ) * clusterNodes {
509508 return & clusterNodes {
510- opt : opt ,
511-
509+ opt : opt ,
512510 addrs : opt .Addrs ,
513511 nodes : make (map [string ]* clusterNode ),
514512 }
@@ -568,12 +566,11 @@ func (c *clusterNodes) Addrs() ([]string, error) {
568566}
569567
570568func (c * clusterNodes ) NextGeneration () uint32 {
571- return atomic .AddUint32 (& c ._generation , 1 )
569+ return atomic .AddUint32 (& c .generation , 1 )
572570}
573571
574572// GC removes unused nodes.
575573func (c * clusterNodes ) GC (generation uint32 ) {
576- //nolint:prealloc
577574 var collected []* clusterNode
578575
579576 c .mu .Lock ()
@@ -626,23 +623,20 @@ func (c *clusterNodes) GetOrCreate(addr string) (*clusterNode, error) {
626623 fn (node .Client )
627624 }
628625
629- c .addrs = appendIfNotExists (c .addrs , addr )
626+ c .addrs = appendIfNotExist (c .addrs , addr )
630627 c .nodes [addr ] = node
631628
632629 return node , nil
633630}
634631
635632func (c * clusterNodes ) get (addr string ) (* clusterNode , error ) {
636- var node * clusterNode
637- var err error
638633 c .mu .RLock ()
634+ defer c .mu .RUnlock ()
635+
639636 if c .closed {
640- err = pool .ErrClosed
641- } else {
642- node = c .nodes [addr ]
637+ return nil , pool .ErrClosed
643638 }
644- c .mu .RUnlock ()
645- return node , err
639+ return c .nodes [addr ], nil
646640}
647641
648642func (c * clusterNodes ) All () ([]* clusterNode , error ) {
@@ -673,8 +667,9 @@ func (c *clusterNodes) Random() (*clusterNode, error) {
673667//------------------------------------------------------------------------------
674668
675669type clusterSlot struct {
676- start , end int
677- nodes []* clusterNode
670+ start int
671+ end int
672+ nodes []* clusterNode
678673}
679674
680675type clusterSlotSlice []* clusterSlot
@@ -734,9 +729,9 @@ func newClusterState(
734729 nodes = append (nodes , node )
735730
736731 if i == 0 {
737- c .Masters = appendUniqueNode (c .Masters , node )
732+ c .Masters = appendIfNotExist (c .Masters , node )
738733 } else {
739- c .Slaves = appendUniqueNode (c .Slaves , node )
734+ c .Slaves = appendIfNotExist (c .Slaves , node )
740735 }
741736 }
742737
@@ -1295,7 +1290,7 @@ func (c *ClusterClient) loadState(ctx context.Context) (*clusterState, error) {
12951290 continue
12961291 }
12971292
1298- return newClusterState (c .nodes , slots , node . Client . opt . Addr )
1293+ return newClusterState (c .nodes , slots , addr )
12991294 }
13001295
13011296 /*
@@ -2017,7 +2012,7 @@ func (c *ClusterClient) MasterForKey(ctx context.Context, key string) (*Client,
20172012 if err != nil {
20182013 return nil , err
20192014 }
2020- return node .Client , err
2015+ return node .Client , nil
20212016}
20222017
20232018func (c * ClusterClient ) context (ctx context.Context ) context.Context {
@@ -2027,26 +2022,13 @@ func (c *ClusterClient) context(ctx context.Context) context.Context {
20272022 return context .Background ()
20282023}
20292024
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
20462029 }
2047- ss = append (ss , e )
20482030 }
2049- return ss
2031+ return append ( vals , newVal )
20502032}
20512033
20522034//------------------------------------------------------------------------------
0 commit comments