Skip to content

Commit

Permalink
Merge pull request #900 from unihorn/fix-timer
Browse files Browse the repository at this point in the history
server: fix timer leak
  • Loading branch information
xiang90 committed Jul 21, 2014
2 parents 919cd38 + c00594e commit bdeb96b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
20 changes: 10 additions & 10 deletions server/peer_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,9 +772,9 @@ func (s *PeerServer) startRoutine(f func()) {
func (s *PeerServer) monitorSnapshot() {
for {
timer := time.NewTimer(s.snapConf.checkingInterval)
defer timer.Stop()
select {
case <-s.closeChan:
timer.Stop()
return
case <-timer.C:
}
Expand Down Expand Up @@ -807,6 +807,8 @@ func (s *PeerServer) monitorSync() {
// monitorTimeoutThreshold groups timeout threshold events together and prints
// them as a single log line.
func (s *PeerServer) monitorTimeoutThreshold() {
ticker := time.NewTicker(ThresholdMonitorTimeout)
defer ticker.Stop()
for {
select {
case <-s.closeChan:
Expand All @@ -815,26 +817,24 @@ func (s *PeerServer) monitorTimeoutThreshold() {
log.Infof("%s: warning: heartbeat near election timeout: %v", s.Config.Name, value)
}

timer := time.NewTimer(ThresholdMonitorTimeout)
defer timer.Stop()
select {
case <-s.closeChan:
return
case <-timer.C:
case <-ticker.C:
}
}
}

// monitorActiveSize has the leader periodically check the status of cluster
// nodes and swaps them out for standbys as needed.
func (s *PeerServer) monitorActiveSize() {
ticker := time.NewTicker(ActiveMonitorTimeout)
defer ticker.Stop()
for {
timer := time.NewTimer(ActiveMonitorTimeout)
defer timer.Stop()
select {
case <-s.closeChan:
return
case <-timer.C:
case <-ticker.C:
}

// Ignore while this peer is not a leader.
Expand Down Expand Up @@ -864,13 +864,13 @@ func (s *PeerServer) monitorActiveSize() {

// monitorPeerActivity has the leader periodically for dead nodes and demotes them.
func (s *PeerServer) monitorPeerActivity() {
ticker := time.NewTicker(PeerActivityMonitorTimeout)
defer ticker.Stop()
for {
timer := time.NewTimer(PeerActivityMonitorTimeout)
defer timer.Stop()
select {
case <-s.closeChan:
return
case <-timer.C:
case <-ticker.C:
}

// Ignore while this peer is not a leader.
Expand Down
6 changes: 3 additions & 3 deletions server/standby_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ func (s *StandbyServer) redirectRequests(w http.ResponseWriter, r *http.Request)
// monitorCluster assumes that the machine has tried to join the cluster and
// failed, so it waits for the interval at the beginning.
func (s *StandbyServer) monitorCluster() {
ticker := time.NewTicker(time.Duration(int64(s.SyncInterval * float64(time.Second))))
defer ticker.Stop()
for {
timer := time.NewTimer(time.Duration(int64(s.SyncInterval * float64(time.Second))))
defer timer.Stop()
select {
case <-s.closeChan:
return
case <-timer.C:
case <-ticker.C:
}

if err := s.syncCluster(nil); err != nil {
Expand Down

0 comments on commit bdeb96b

Please sign in to comment.