fix(peer_server): check running status before start/stop

This makes peer server more robust.
This commit is contained in:
Yicheng Qin 2014-05-07 12:44:48 -07:00
parent cf25650b3c
commit 206881bfec

View File

@ -64,6 +64,7 @@ type PeerServer struct {
stopNotify chan bool stopNotify chan bool
removeNotify chan bool removeNotify chan bool
started bool
closeChan chan bool closeChan chan bool
routineGroup sync.WaitGroup routineGroup sync.WaitGroup
timeoutThresholdChan chan interface{} timeoutThresholdChan chan interface{}
@ -240,6 +241,10 @@ func (s *PeerServer) findCluster(discoverURL string, peers []string) {
func (s *PeerServer) Start(snapshot bool, discoverURL string, peers []string) error { func (s *PeerServer) Start(snapshot bool, discoverURL string, peers []string) error {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
if s.started {
return nil
}
s.started = true
// LoadSnapshot // LoadSnapshot
if snapshot { if snapshot {
@ -283,6 +288,11 @@ func (s *PeerServer) Start(snapshot bool, discoverURL string, peers []string) er
func (s *PeerServer) Stop() { func (s *PeerServer) Stop() {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
if !s.started {
return
}
s.started = false
close(s.closeChan) close(s.closeChan)
// TODO(yichengq): it should also call async stop for raft server, // TODO(yichengq): it should also call async stop for raft server,
// but this functionality has not been implemented. // but this functionality has not been implemented.
@ -293,6 +303,12 @@ func (s *PeerServer) Stop() {
func (s *PeerServer) asyncRemove() { func (s *PeerServer) asyncRemove() {
s.Lock() s.Lock()
if !s.started {
s.Unlock()
return
}
s.started = false
close(s.closeChan) close(s.closeChan)
// TODO(yichengq): it should also call async stop for raft server, // TODO(yichengq): it should also call async stop for raft server,
// but this functionality has not been implemented. // but this functionality has not been implemented.