Skip to content

Commit f2bd92b

Browse files
committed
Merge pull request #178 from oliver006/close_node_refresh
Use channel to stop the node.Refresh() goroutine
2 parents 549afc5 + ab252a5 commit f2bd92b

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

node.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ type Node struct {
1414
Host Host
1515
aliases []Host
1616

17-
cluster *Cluster
18-
pool *Pool
19-
refreshTicker *time.Ticker
17+
cluster *Cluster
18+
pool *Pool
19+
refreshDoneChan chan bool
2020

2121
mu sync.RWMutex
2222
closed bool
@@ -25,14 +25,14 @@ type Node struct {
2525

2626
func newNode(id string, aliases []Host, cluster *Cluster, pool *Pool) *Node {
2727
node := &Node{
28-
ID: id,
29-
Host: aliases[0],
30-
aliases: aliases,
31-
cluster: cluster,
32-
pool: pool,
33-
health: 100,
28+
ID: id,
29+
Host: aliases[0],
30+
aliases: aliases,
31+
cluster: cluster,
32+
pool: pool,
33+
health: 100,
34+
refreshDoneChan: make(chan bool),
3435
}
35-
3636
// Start node refresh loop
3737
refreshInterval := cluster.opts.NodeRefreshInterval
3838
if refreshInterval <= 0 {
@@ -41,9 +41,15 @@ func newNode(id string, aliases []Host, cluster *Cluster, pool *Pool) *Node {
4141
}
4242

4343
go func() {
44-
node.refreshTicker = time.NewTicker(refreshInterval)
45-
for _ = range node.refreshTicker.C {
46-
node.Refresh()
44+
45+
refreshTicker := time.NewTicker(refreshInterval)
46+
for {
47+
select {
48+
case <-refreshTicker.C:
49+
node.Refresh()
50+
case <-node.refreshDoneChan:
51+
return
52+
}
4753
}
4854
}()
4955

@@ -73,7 +79,7 @@ func (n *Node) Close(optArgs ...CloseOpts) error {
7379
}
7480
}
7581

76-
n.refreshTicker.Stop()
82+
n.refreshDoneChan <- true
7783
if n.pool != nil {
7884
n.pool.Close()
7985
}

0 commit comments

Comments
 (0)