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:
parent
26810fe928
commit
89c3c5319f
@ -1,6 +1,7 @@
|
||||
package autopilot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@ -485,6 +486,18 @@ func (a *Agent) controller() {
|
||||
log.Infof("Triggering attachment directive dispatch, "+
|
||||
"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
|
||||
// nodes that we currently have channels with so we avoid
|
||||
// duplicate edges.
|
||||
@ -505,14 +518,13 @@ func (a *Agent) controller() {
|
||||
numChans, nodesToSkip,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("Unable to select candidates for "+
|
||||
return fmt.Errorf("Unable to select candidates for "+
|
||||
"attachment: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(chanCandidates) == 0 {
|
||||
log.Infof("No eligible candidates to connect to")
|
||||
continue
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
// available to future heuristic selections.
|
||||
a.pendingMtx.Lock()
|
||||
defer a.pendingMtx.Unlock()
|
||||
if uint16(len(a.pendingOpens)) >= a.cfg.Constraints.MaxPendingOpens {
|
||||
a.pendingMtx.Unlock()
|
||||
log.Debugf("Reached cap of %v pending "+
|
||||
"channel opens, will retry "+
|
||||
"after success/failure",
|
||||
a.cfg.Constraints.MaxPendingOpens)
|
||||
continue
|
||||
return nil
|
||||
}
|
||||
|
||||
// For each recommended attachment directive, we'll launch a
|
||||
@ -550,8 +562,7 @@ func (a *Agent) controller() {
|
||||
a.wg.Add(1)
|
||||
go a.executeDirective(chanCandidate)
|
||||
}
|
||||
a.pendingMtx.Unlock()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// executeDirective attempts to connect to the channel candidate specified by
|
||||
|
Loading…
Reference in New Issue
Block a user