From fb4121ba6c492edd82df07c07a6cfd98a0680750 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 4 Jun 2017 22:18:12 -0700 Subject: [PATCH] server: Peers method now grabs data directly with read mutex This commit modifies the Peers method on the server such that a caller is able to query this method without the main serger goroutines started. This is a small component in a larger change which will let us start the RPC server independently of the server. --- server.go | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/server.go b/server.go index 186266e2..3d7be8f7 100644 --- a/server.go +++ b/server.go @@ -989,12 +989,6 @@ type disconnectPeerMsg struct { err chan error } -// listPeersMsg is a message sent to the server in order to obtain a listing -// of all currently active channels. -type listPeersMsg struct { - resp chan []*peer -} - // openChanReq is a message sent to the server in order to request the // initiation of a channel funding workflow to the peer with either the specified // relative peer ID, or a global lightning ID. @@ -1103,8 +1097,6 @@ out: s.handleDisconnectPeer(msg) case *connectPeerMsg: s.handleConnectPeer(msg) - case *listPeersMsg: - s.handleListPeers(msg) case *openChanReq: s.handleOpenChanReq(msg) } @@ -1118,21 +1110,6 @@ out: s.wg.Done() } -// handleListPeers sends a lice of all currently active peers to the original -// caller. -func (s *server) handleListPeers(msg *listPeersMsg) { - s.peersMtx.RLock() - - peers := make([]*peer, 0, len(s.peersByID)) - for _, peer := range s.peersByID { - peers = append(peers, peer) - } - - s.peersMtx.RUnlock() - - msg.resp <- peers -} - // handleConnectPeer attempts to establish a connection to the address enclosed // within the passed connectPeerMsg. This function is *async*, a goroutine will // be spawned in order to finish the request, and respond to the caller. @@ -1333,9 +1310,14 @@ func (s *server) OpenChannel(peerID int32, nodeKey *btcec.PublicKey, // Peers returns a slice of all active peers. func (s *server) Peers() []*peer { - resp := make(chan []*peer, 1) + s.peersMtx.RLock() - s.queries <- &listPeersMsg{resp} + peers := make([]*peer, 0, len(s.peersByID)) + for _, peer := range s.peersByID { + peers = append(peers, peer) + } - return <-resp + s.peersMtx.RUnlock() + + return peers }