@@ -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
@@ -680,9 +675,7 @@ type clusterState struct {
680675 createdAt time.Time
681676}
682677
683- func newClusterState (
684- nodes * clusterNodes , slots []ClusterSlot , origin string ,
685- ) (* clusterState , error ) {
678+ func newClusterState (nodes * clusterNodes , slots []ClusterSlot , origin string ) (* clusterState , error ) {
686679 c := clusterState {
687680 nodes : nodes ,
688681
@@ -712,9 +705,9 @@ func newClusterState(
712705 nodes = append (nodes , node )
713706
714707 if i == 0 {
715- c .Masters = appendUniqueNode (c .Masters , node )
708+ c .Masters = appendIfNotExist (c .Masters , node )
716709 } else {
717- c .Slaves = appendUniqueNode (c .Slaves , node )
710+ c .Slaves = appendIfNotExist (c .Slaves , node )
718711 }
719712 }
720713
@@ -1273,7 +1266,7 @@ func (c *ClusterClient) loadState(ctx context.Context) (*clusterState, error) {
12731266 continue
12741267 }
12751268
1276- return newClusterState (c .nodes , slots , node . Client . opt . Addr )
1269+ return newClusterState (c .nodes , slots , addr )
12771270 }
12781271
12791272 /*
@@ -1932,11 +1925,7 @@ func cmdSlot(cmd Cmder, pos int, preferredRandomSlot int) int {
19321925 return hashtag .Slot (firstKey )
19331926}
19341927
1935- func (c * ClusterClient ) cmdNode (
1936- ctx context.Context ,
1937- cmdName string ,
1938- slot int ,
1939- ) (* clusterNode , error ) {
1928+ func (c * ClusterClient ) cmdNode (ctx context.Context , cmdName string , slot int ) (* clusterNode , error ) {
19401929 state , err := c .state .Get (ctx )
19411930 if err != nil {
19421931 return nil , err
@@ -2005,26 +1994,13 @@ func (c *ClusterClient) context(ctx context.Context) context.Context {
20051994 return context .Background ()
20061995}
20071996
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- }
1997+ func appendIfNotExist [T comparable ](vals []T , newVal T ) []T {
1998+ for _ , v := range vals {
1999+ if v == newVal {
2000+ return vals
20242001 }
2025- ss = append (ss , e )
20262002 }
2027- return ss
2003+ return append ( vals , newVal )
20282004}
20292005
20302006//------------------------------------------------------------------------------
0 commit comments