Skip to content

Commit

Permalink
refactor: make conditional refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
proost committed Sep 18, 2024
1 parent 1181ffb commit 027671b
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,43 +389,47 @@ func (c *clusterClient) runClusterTopologyRefreshment() {
case <-c.stopCh:
return
case <-ticker.C:
result, err := c.getClusterTopology()
if err != nil {
c.lazyRefresh()
continue
}
c.conditionalRefresh()
}
}
}

groups := result.parse(c.opt.TLSConfig != nil)
func (c *clusterClient) conditionalRefresh() {
result, err := c.getClusterTopology()
if err != nil {
c.lazyRefresh()
return
}

conns := c.newConns(groups)
groups := result.parse(c.opt.TLSConfig != nil)

isChanged := false
c.mu.RLock()
// check if the new topology is different from the current one
for addr, cc := range conns {
old, ok := c.conns[addr]
if !ok || old.replica != cc.replica {
isChanged = true
break
}
}
conns := c.newConns(groups)

// check if the current topology is different from the new one
if !isChanged {
for addr := range c.conns {
if _, ok := conns[addr]; !ok {
isChanged = true
break
}
}
}
c.mu.RUnlock()
isChanged := false
c.mu.RLock()
// check if the new topology is different from the current one
for addr, cc := range conns {
old, ok := c.conns[addr]
if !ok || old.replica != cc.replica {
isChanged = true
break
}
}

if isChanged {
c.lazyRefresh()
// check if the current topology is different from the new one
if !isChanged {
for addr := range c.conns {
if _, ok := conns[addr]; !ok {
isChanged = true
break
}
}
}
c.mu.RUnlock()

if isChanged {
c.lazyRefresh()
}
}

func (c *clusterClient) _pick(slot uint16, toReplica bool) (p conn) {
Expand Down

0 comments on commit 027671b

Please sign in to comment.