Merge pull request #112 from goraft/minor-refactor-add-remove-peer
Minor refactor of AddPeer/RemovePeer.pull/820/head
commit
b71d2ecae9
25
server.go
25
server.go
|
@ -927,17 +927,14 @@ func (s *Server) processRequestVoteRequest(req *RequestVoteRequest) (*RequestVot
|
|||
// Adds a peer to the server.
|
||||
func (s *Server) AddPeer(name string, connectiongString string) error {
|
||||
s.debugln("server.peer.add: ", name, len(s.peers))
|
||||
defer s.writeConf()
|
||||
|
||||
// Do not allow peers to be added twice.
|
||||
if s.peers[name] != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Skip the Peer if it has the same name as the Server
|
||||
if s.name == name {
|
||||
return nil
|
||||
}
|
||||
|
||||
if s.name != name {
|
||||
peer := newPeer(s, name, connectiongString, s.heartbeatTimeout)
|
||||
|
||||
if s.State() == Leader {
|
||||
|
@ -945,8 +942,10 @@ func (s *Server) AddPeer(name string, connectiongString string) error {
|
|||
}
|
||||
|
||||
s.peers[peer.Name] = peer
|
||||
}
|
||||
|
||||
s.debugln("server.peer.conf.write: ", name)
|
||||
// Write the configuration to file.
|
||||
s.writeConf()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -955,14 +954,8 @@ func (s *Server) AddPeer(name string, connectiongString string) error {
|
|||
func (s *Server) RemovePeer(name string) error {
|
||||
s.debugln("server.peer.remove: ", name, len(s.peers))
|
||||
|
||||
defer s.writeConf()
|
||||
|
||||
if name == s.Name() {
|
||||
// when the removed node restart, it should be able
|
||||
// to know it has been removed before. So we need
|
||||
// to update knownCommitIndex
|
||||
return nil
|
||||
}
|
||||
// Skip the Peer if it has the same name as the Server
|
||||
if name != s.Name() {
|
||||
// Return error if peer doesn't exist.
|
||||
peer := s.peers[name]
|
||||
if peer == nil {
|
||||
|
@ -975,6 +968,10 @@ func (s *Server) RemovePeer(name string) error {
|
|||
}
|
||||
|
||||
delete(s.peers, name)
|
||||
}
|
||||
|
||||
// Write the configuration to file.
|
||||
s.writeConf()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue