@@ -142,6 +142,7 @@ type Server struct {
142142
143143 quit chan struct {}
144144 addstatic chan * discover.Node
145+ removestatic chan * discover.Node
145146 posthandshake chan * conn
146147 addpeer chan * conn
147148 delpeer chan * Peer
@@ -257,6 +258,14 @@ func (srv *Server) AddPeer(node *discover.Node) {
257258 }
258259}
259260
261+ // RemovePeer disconnects from the given node
262+ func (srv * Server ) RemovePeer (node * discover.Node ) {
263+ select {
264+ case srv .removestatic <- node :
265+ case <- srv .quit :
266+ }
267+ }
268+
260269// Self returns the local node's endpoint information.
261270func (srv * Server ) Self () * discover.Node {
262271 srv .lock .Lock ()
@@ -327,6 +336,7 @@ func (srv *Server) Start() (err error) {
327336 srv .delpeer = make (chan * Peer )
328337 srv .posthandshake = make (chan * conn )
329338 srv .addstatic = make (chan * discover.Node )
339+ srv .removestatic = make (chan * discover.Node )
330340 srv .peerOp = make (chan peerOpFunc )
331341 srv .peerOpDone = make (chan struct {})
332342
@@ -395,6 +405,7 @@ type dialer interface {
395405 newTasks (running int , peers map [discover.NodeID ]* Peer , now time.Time ) []task
396406 taskDone (task , time.Time )
397407 addStatic (* discover.Node )
408+ removeStatic (* discover.Node )
398409}
399410
400411func (srv * Server ) run (dialstate dialer ) {
@@ -458,6 +469,15 @@ running:
458469 // it will keep the node connected.
459470 glog .V (logger .Detail ).Infoln ("<-addstatic:" , n )
460471 dialstate .addStatic (n )
472+ case n := <- srv .removestatic :
473+ // This channel is used by RemovePeer to send a
474+ // disconnect request to a peer and begin the
475+ // stop keeping the node connected
476+ glog .V (logger .Detail ).Infoln ("<-removestatic:" , n )
477+ dialstate .removeStatic (n )
478+ if p , ok := peers [n .ID ]; ok {
479+ p .Disconnect (DiscRequested )
480+ }
461481 case op := <- srv .peerOp :
462482 // This channel is used by Peers and PeerCount.
463483 op (peers )
0 commit comments