autopilot/agent: split opening logic into own method

This commit takes the logic after the autopilot agent has decided that it
needs to open more channels, and moves it into a new method openChan.
This commit is contained in:
Johan T. Halseth committed 2018-12-06 14:26:03 +01:00
1 parent 26810fe928
commit 89c3c5319f
1 file changed
+18 -7
+18 -7
View File
@@ -1,6 +1,7 @@
package autopilot package autopilot
import ( import (
"fmt"
"net" "net"
"sync" "sync"
"sync/atomic" "sync/atomic"
@@ -485,6 +486,18 @@ func (a *Agent) controller() {
log.Infof("Triggering attachment directive dispatch, "+ log.Infof("Triggering attachment directive dispatch, "+
"total_funds=%v", a.totalBalance) "total_funds=%v", a.totalBalance)
err := a.openChans(availableFunds, numChans, totalChans)
if err != nil {
log.Errorf("Unable to open channels: %v", err)
}
}
}
// openChans queries the agent's heuristic for a set of channel candidates, and
// attempts to open channels to them.
func (a *Agent) openChans(availableFunds btcutil.Amount, numChans uint32,
totalChans []Channel) error {
// We're to attempt an attachment so we'll obtain the set of // We're to attempt an attachment so we'll obtain the set of
// nodes that we currently have channels with so we avoid // nodes that we currently have channels with so we avoid
// duplicate edges. // duplicate edges.
@@ -505,14 +518,13 @@ func (a *Agent) controller() {
numChans, nodesToSkip, numChans, nodesToSkip,
) )
if err != nil { if err != nil {
log.Errorf("Unable to select candidates for "+ return fmt.Errorf("Unable to select candidates for "+
"attachment: %v", err) "attachment: %v", err)
continue
} }
if len(chanCandidates) == 0 { if len(chanCandidates) == 0 {
log.Infof("No eligible candidates to connect to") log.Infof("No eligible candidates to connect to")
continue return nil
} }
log.Infof("Attempting to execute channel attachment "+ log.Infof("Attempting to execute channel attachment "+
@@ -525,13 +537,13 @@ func (a *Agent) controller() {
// connections succeed, we will they will be ignored and made // connections succeed, we will they will be ignored and made
// available to future heuristic selections. // available to future heuristic selections.
a.pendingMtx.Lock() a.pendingMtx.Lock()
defer a.pendingMtx.Unlock()
if uint16(len(a.pendingOpens)) >= a.cfg.Constraints.MaxPendingOpens { if uint16(len(a.pendingOpens)) >= a.cfg.Constraints.MaxPendingOpens {
a.pendingMtx.Unlock()
log.Debugf("Reached cap of %v pending "+ log.Debugf("Reached cap of %v pending "+
"channel opens, will retry "+ "channel opens, will retry "+
"after success/failure", "after success/failure",
a.cfg.Constraints.MaxPendingOpens) a.cfg.Constraints.MaxPendingOpens)
continue return nil
} }
// For each recommended attachment directive, we'll launch a // For each recommended attachment directive, we'll launch a
@@ -550,8 +562,7 @@ func (a *Agent) controller() {
a.wg.Add(1) a.wg.Add(1)
go a.executeDirective(chanCandidate) go a.executeDirective(chanCandidate)
} }
a.pendingMtx.Unlock() return nil
}
} }
// executeDirective attempts to connect to the channel candidate specified by // executeDirective attempts to connect to the channel candidate specified by