From c00594e6807eea31a16bd3e84ea043efda435b88 Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Mon, 21 Jul 2014 14:22:09 -0700 Subject: [PATCH] server: fix timer leak --- server/peer_server.go | 20 ++++++++++---------- server/standby_server.go | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/server/peer_server.go b/server/peer_server.go index efa7371eb..eda20fc3d 100644 --- a/server/peer_server.go +++ b/server/peer_server.go @@ -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: } @@ -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: @@ -815,12 +817,10 @@ 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: } } } @@ -828,13 +828,13 @@ func (s *PeerServer) monitorTimeoutThreshold() { // 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. @@ -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. diff --git a/server/standby_server.go b/server/standby_server.go index ce9e93a9f..4c5e6c2b5 100644 --- a/server/standby_server.go +++ b/server/standby_server.go @@ -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 {