Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type Node struct {
Host Host
aliases []Host

cluster *Cluster
pool *Pool
refreshTicker *time.Ticker
cluster *Cluster
pool *Pool
refreshDoneChan chan bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a chan struct{} here instead. Not much difference but theres no reason not to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda the same but I slightly prefer the bool chan as it somewhat better communicates the Done == true fact. No strong feelings though.


mu sync.RWMutex
closed bool
Expand All @@ -25,14 +25,14 @@ type Node struct {

func newNode(id string, aliases []Host, cluster *Cluster, pool *Pool) *Node {
node := &Node{
ID: id,
Host: aliases[0],
aliases: aliases,
cluster: cluster,
pool: pool,
health: 100,
ID: id,
Host: aliases[0],
aliases: aliases,
cluster: cluster,
pool: pool,
health: 100,
refreshDoneChan: make(chan bool),
}

// Start node refresh loop
refreshInterval := cluster.opts.NodeRefreshInterval
if refreshInterval <= 0 {
Expand All @@ -41,9 +41,15 @@ func newNode(id string, aliases []Host, cluster *Cluster, pool *Pool) *Node {
}

go func() {
node.refreshTicker = time.NewTicker(refreshInterval)
for _ = range node.refreshTicker.C {
node.Refresh()

refreshTicker := time.NewTicker(refreshInterval)
for {
select {
case <-refreshTicker.C:
node.Refresh()
case <-node.refreshDoneChan:
return
}
}
}()

Expand Down Expand Up @@ -73,7 +79,7 @@ func (n *Node) Close(optArgs ...CloseOpts) error {
}
}

n.refreshTicker.Stop()
n.refreshDoneChan <- true
if n.pool != nil {
n.pool.Close()
}
Expand Down