diff options
author | Felix Lange <fjl@twurst.com> | 2016-07-29 18:34:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-29 18:34:28 +0800 |
commit | b8ba80bff70bedb59385e35f11de18679f3b4967 (patch) | |
tree | 41031b7d529ce302f45770c63ec5827a99d4bd5b /p2p | |
parent | 8ec6ccc54d1da7bbaa87bd0b489b1778ea729c90 (diff) | |
parent | 4c3da0f2e1063ccb32933faff4601d5f5e0f987c (diff) | |
download | dexon-b8ba80bff70bedb59385e35f11de18679f3b4967.tar dexon-b8ba80bff70bedb59385e35f11de18679f3b4967.tar.gz dexon-b8ba80bff70bedb59385e35f11de18679f3b4967.tar.bz2 dexon-b8ba80bff70bedb59385e35f11de18679f3b4967.tar.lz dexon-b8ba80bff70bedb59385e35f11de18679f3b4967.tar.xz dexon-b8ba80bff70bedb59385e35f11de18679f3b4967.tar.zst dexon-b8ba80bff70bedb59385e35f11de18679f3b4967.zip |
Merge pull request #2740 from Firescar96/removepeer
node, p2p, internal: Add ability to remove peers via admin interface
Diffstat (limited to 'p2p')
-rw-r--r-- | p2p/dial.go | 5 | ||||
-rw-r--r-- | p2p/server.go | 20 | ||||
-rw-r--r-- | p2p/server_test.go | 2 |
3 files changed, 27 insertions, 0 deletions
diff --git a/p2p/dial.go b/p2p/dial.go index c0e703d7d..691b8539e 100644 --- a/p2p/dial.go +++ b/p2p/dial.go @@ -121,6 +121,11 @@ func (s *dialstate) addStatic(n *discover.Node) { s.static[n.ID] = &dialTask{flags: staticDialedConn, dest: n} } +func (s *dialstate) removeStatic(n *discover.Node) { + // This removes a task so future attempts to connect will not be made. + delete(s.static, n.ID) +} + func (s *dialstate) newTasks(nRunning int, peers map[discover.NodeID]*Peer, now time.Time) []task { var newtasks []task isDialing := func(id discover.NodeID) bool { diff --git a/p2p/server.go b/p2p/server.go index 880aa7cf1..8e3cd93f9 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -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) diff --git a/p2p/server_test.go b/p2p/server_test.go index deb34f5bb..313d086ec 100644 --- a/p2p/server_test.go +++ b/p2p/server_test.go @@ -301,6 +301,8 @@ func (tg taskgen) taskDone(t task, now time.Time) { } func (tg taskgen) addStatic(*discover.Node) { } +func (tg taskgen) removeStatic(*discover.Node) { +} type testTask struct { index int |