@@ -342,8 +342,7 @@ type clusterNode struct {
342342 failing uint32 // atomic
343343 loaded uint32 // atomic
344344
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
347346 lastLatencyMeasurement int64 // atomic
348347}
349348
@@ -480,13 +479,12 @@ type clusterNodes struct {
480479 closed bool
481480 onNewNode []func (rdb * Client )
482481
483- _generation uint32 // atomic
482+ generation uint32 // atomic
484483}
485484
486485func newClusterNodes (opt * ClusterOptions ) * clusterNodes {
487486 return & clusterNodes {
488- opt : opt ,
489-
487+ opt : opt ,
490488 addrs : opt .Addrs ,
491489 nodes : make (map [string ]* clusterNode ),
492490 }
@@ -546,12 +544,11 @@ func (c *clusterNodes) Addrs() ([]string, error) {
546544}
547545
548546func (c * clusterNodes ) NextGeneration () uint32 {
549- return atomic .AddUint32 (& c ._generation , 1 )
547+ return atomic .AddUint32 (& c .generation , 1 )
550548}
551549
552550// GC removes unused nodes.
553551func (c * clusterNodes ) GC (generation uint32 ) {
554- //nolint:prealloc
555552 var collected []* clusterNode
556553
557554 c .mu .Lock ()
@@ -604,23 +601,20 @@ func (c *clusterNodes) GetOrCreate(addr string) (*clusterNode, error) {
604601 fn (node .Client )
605602 }
606603
607- c .addrs = appendIfNotExists (c .addrs , addr )
604+ c .addrs = appendIfNotExist (c .addrs , addr )
608605 c .nodes [addr ] = node
609606
610607 return node , nil
611608}
612609
613610func (c * clusterNodes ) get (addr string ) (* clusterNode , error ) {
614- var node * clusterNode
615- var err error
616611 c .mu .RLock ()
612+ defer c .mu .RUnlock ()
613+
617614 if c .closed {
618- err = pool .ErrClosed
619- } else {
620- node = c .nodes [addr ]
615+ return nil , pool .ErrClosed
621616 }
622- c .mu .RUnlock ()
623- return node , err
617+ return c .nodes [addr ], nil
624618}
625619
626620func (c * clusterNodes ) All () ([]* clusterNode , error ) {
@@ -651,8 +645,9 @@ func (c *clusterNodes) Random() (*clusterNode, error) {
651645//------------------------------------------------------------------------------
652646
653647type clusterSlot struct {
654- start , end int
655- nodes []* clusterNode
648+ start int
649+ end int
650+ nodes []* clusterNode
656651}
657652
658653type clusterSlotSlice []* clusterSlot
@@ -712,9 +707,9 @@ func newClusterState(
712707 nodes = append (nodes , node )
713708
714709 if i == 0 {
715- c .Masters = appendUniqueNode (c .Masters , node )
710+ c .Masters = appendIfNotExist (c .Masters , node )
716711 } else {
717- c .Slaves = appendUniqueNode (c .Slaves , node )
712+ c .Slaves = appendIfNotExist (c .Slaves , node )
718713 }
719714 }
720715
@@ -1273,7 +1268,7 @@ func (c *ClusterClient) loadState(ctx context.Context) (*clusterState, error) {
12731268 continue
12741269 }
12751270
1276- return newClusterState (c .nodes , slots , node . Client . opt . Addr )
1271+ return newClusterState (c .nodes , slots , addr )
12771272 }
12781273
12791274 /*
@@ -1995,7 +1990,7 @@ func (c *ClusterClient) MasterForKey(ctx context.Context, key string) (*Client,
19951990 if err != nil {
19961991 return nil , err
19971992 }
1998- return node .Client , err
1993+ return node .Client , nil
19991994}
20001995
20011996func (c * ClusterClient ) context (ctx context.Context ) context.Context {
@@ -2005,26 +2000,13 @@ func (c *ClusterClient) context(ctx context.Context) context.Context {
20052000 return context .Background ()
20062001}
20072002
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
20242007 }
2025- ss = append (ss , e )
20262008 }
2027- return ss
2009+ return append ( vals , newVal )
20282010}
20292011
20302012//------------------------------------------------------------------------------
0 commit comments