Browse Source

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

This makes peer server more robust.
Yicheng Qin 11 years ago
parent
commit
206881bfec
1 changed files with 16 additions and 0 deletions
  1. 16 0
      server/peer_server.go

+ 16 - 0
server/peer_server.go

@@ -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.