mirror of
https://github.com/lightningnetwork/lnd.git
synced 2024-11-19 09:53:54 +01:00
lnd: consolidate peerManager and queryHandler
This commit is contained in:
parent
496d1e8edc
commit
b9c29ad9fc
43
server.go
43
server.go
@ -116,8 +116,7 @@ func (s *server) Start() {
|
|||||||
s.fundingMgr.Start()
|
s.fundingMgr.Start()
|
||||||
s.htlcSwitch.Start()
|
s.htlcSwitch.Start()
|
||||||
|
|
||||||
s.wg.Add(2)
|
s.wg.Add(1)
|
||||||
go s.peerManager()
|
|
||||||
go s.queryHandler()
|
go s.queryHandler()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,28 +153,6 @@ func (s *server) WaitForShutdown() {
|
|||||||
s.wg.Wait()
|
s.wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
// peerManager handles any requests to modify the server's internal state of
|
|
||||||
// all active peers. Additionally, any queries directed at peers will be
|
|
||||||
// handled by this goroutine.
|
|
||||||
//
|
|
||||||
// NOTE: This MUST be run as a goroutine.
|
|
||||||
func (s *server) peerManager() {
|
|
||||||
out:
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
// New peers.
|
|
||||||
case p := <-s.newPeers:
|
|
||||||
s.addPeer(p)
|
|
||||||
// Finished peers.
|
|
||||||
case p := <-s.donePeers:
|
|
||||||
s.removePeer(p)
|
|
||||||
case <-s.quit:
|
|
||||||
break out
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.wg.Done()
|
|
||||||
}
|
|
||||||
|
|
||||||
// addPeer adds the passed peer to the server's global state of all active
|
// addPeer adds the passed peer to the server's global state of all active
|
||||||
// peers.
|
// peers.
|
||||||
func (s *server) addPeer(p *peer) {
|
func (s *server) addPeer(p *peer) {
|
||||||
@ -195,6 +172,8 @@ func (s *server) addPeer(p *peer) {
|
|||||||
// removePeer removes the passed peer from the server's state of all active
|
// removePeer removes the passed peer from the server's state of all active
|
||||||
// peers.
|
// peers.
|
||||||
func (s *server) removePeer(p *peer) {
|
func (s *server) removePeer(p *peer) {
|
||||||
|
srvrLog.Debugf("removing peer %v", p)
|
||||||
|
|
||||||
if p == nil {
|
if p == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -249,15 +228,23 @@ type openChanResp struct {
|
|||||||
chanPoint *wire.OutPoint
|
chanPoint *wire.OutPoint
|
||||||
}
|
}
|
||||||
|
|
||||||
// queryHandler is a a goroutine dedicated to handling an queries or requests
|
// peerManager handles any requests to modify the server's internal state of
|
||||||
// to mutate the server's global state.
|
// all active peers, or query/mutate the server's global state. Additionally,
|
||||||
|
// any queries directed at peers will be handled by this goroutine.
|
||||||
//
|
//
|
||||||
// NOTE: This MUST be run as a goroutine.
|
// NOTE: This MUST be run as a goroutine.
|
||||||
func (s *server) queryHandler() {
|
func (s *server) queryHandler() {
|
||||||
// TODO(roabeef): consolidate with peerManager
|
|
||||||
out:
|
out:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
// New peers.
|
||||||
|
case p := <-s.newPeers:
|
||||||
|
s.addPeer(p)
|
||||||
|
|
||||||
|
// Finished peers.
|
||||||
|
case p := <-s.donePeers:
|
||||||
|
s.removePeer(p)
|
||||||
|
|
||||||
case query := <-s.queries:
|
case query := <-s.queries:
|
||||||
// TODO(roasbeef): make all goroutines?
|
// TODO(roasbeef): make all goroutines?
|
||||||
switch msg := query.(type) {
|
switch msg := query.(type) {
|
||||||
@ -342,6 +329,7 @@ func (s *server) handleConnectPeer(msg *connectPeerMsg) {
|
|||||||
peer, err := newPeer(conn, s, activeNetParams.Net, false)
|
peer, err := newPeer(conn, s, activeNetParams.Net, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
srvrLog.Errorf("unable to create peer %v", err)
|
srvrLog.Errorf("unable to create peer %v", err)
|
||||||
|
conn.Close()
|
||||||
msg.resp <- -1
|
msg.resp <- -1
|
||||||
msg.err <- err
|
msg.err <- err
|
||||||
return
|
return
|
||||||
@ -454,6 +442,7 @@ func (s *server) listener(l net.Listener) {
|
|||||||
peer, err := newPeer(conn, s, activeNetParams.Net, true)
|
peer, err := newPeer(conn, s, activeNetParams.Net, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
srvrLog.Errorf("unable to create peer: %v", err)
|
srvrLog.Errorf("unable to create peer: %v", err)
|
||||||
|
conn.Close()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user