node, p2p, internal: Add ability to remove peers via admin interface
This commit is contained in:
@ -142,6 +142,7 @@ type Server struct {
|
||||
|
||||
quit chan struct{}
|
||||
addstatic chan *discover.Node
|
||||
removestatic chan *discover.Node
|
||||
posthandshake chan *conn
|
||||
addpeer chan *conn
|
||||
delpeer chan *Peer
|
||||
@ -257,6 +258,14 @@ func (srv *Server) AddPeer(node *discover.Node) {
|
||||
}
|
||||
}
|
||||
|
||||
// RemovePeer disconnects from the given node
|
||||
func (srv *Server) RemovePeer(node *discover.Node) {
|
||||
select {
|
||||
case srv.removestatic <- node:
|
||||
case <-srv.quit:
|
||||
}
|
||||
}
|
||||
|
||||
// Self returns the local node's endpoint information.
|
||||
func (srv *Server) Self() *discover.Node {
|
||||
srv.lock.Lock()
|
||||
@ -327,6 +336,7 @@ func (srv *Server) Start() (err error) {
|
||||
srv.delpeer = make(chan *Peer)
|
||||
srv.posthandshake = make(chan *conn)
|
||||
srv.addstatic = make(chan *discover.Node)
|
||||
srv.removestatic = make(chan *discover.Node)
|
||||
srv.peerOp = make(chan peerOpFunc)
|
||||
srv.peerOpDone = make(chan struct{})
|
||||
|
||||
@ -395,6 +405,7 @@ type dialer interface {
|
||||
newTasks(running int, peers map[discover.NodeID]*Peer, now time.Time) []task
|
||||
taskDone(task, time.Time)
|
||||
addStatic(*discover.Node)
|
||||
removeStatic(*discover.Node)
|
||||
}
|
||||
|
||||
func (srv *Server) run(dialstate dialer) {
|
||||
@ -458,6 +469,15 @@ running:
|
||||
// it will keep the node connected.
|
||||
glog.V(logger.Detail).Infoln("<-addstatic:", n)
|
||||
dialstate.addStatic(n)
|
||||
case n := <-srv.removestatic:
|
||||
// This channel is used by RemovePeer to send a
|
||||
// disconnect request to a peer and begin the
|
||||
// stop keeping the node connected
|
||||
glog.V(logger.Detail).Infoln("<-removestatic:", n)
|
||||
dialstate.removeStatic(n)
|
||||
if p, ok := peers[n.ID]; ok {
|
||||
p.Disconnect(DiscRequested)
|
||||
}
|
||||
case op := <-srv.peerOp:
|
||||
// This channel is used by Peers and PeerCount.
|
||||
op(peers)
|
||||
|
Reference in New Issue
Block a user