autopilot: start threading contexts through

The `GraphSource` interface in the `autopilot` package is directly
implemented by the `graphdb.KVStore` and so we will eventually thread
contexts through to this interface. So in this commit, we start updating
the autopilot system to thread contexts through in preparation for
passing the context through to any calls made to the GraphSource.

Two context.TODOs are added here which will be addressed in follow up
commits.
This commit is contained in:
Elle Mouton
2025-04-09 06:25:29 +02:00
committed by ziggie
parent ca52834795
commit cd4a59071d
13 changed files with 146 additions and 71 deletions

View File

@@ -1,11 +1,13 @@
package autopilot
import (
"context"
"fmt"
"sync"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/fn/v2"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
@@ -53,8 +55,9 @@ type Manager struct {
// disabled.
pilot *Agent
quit chan struct{}
wg sync.WaitGroup
quit chan struct{}
wg sync.WaitGroup
cancel fn.Option[context.CancelFunc]
sync.Mutex
}
@@ -80,6 +83,7 @@ func (m *Manager) Stop() error {
log.Errorf("Unable to stop pilot: %v", err)
}
m.cancel.WhenSome(func(fn context.CancelFunc) { fn() })
close(m.quit)
m.wg.Wait()
})
@@ -96,7 +100,7 @@ func (m *Manager) IsActive() bool {
// StartAgent creates and starts an autopilot agent from the Manager's
// config.
func (m *Manager) StartAgent() error {
func (m *Manager) StartAgent(ctx context.Context) error {
m.Lock()
defer m.Unlock()
@@ -104,6 +108,8 @@ func (m *Manager) StartAgent() error {
if m.pilot != nil {
return nil
}
ctx, cancel := context.WithCancel(ctx)
m.cancel = fn.Some(cancel)
// Next, we'll fetch the current state of open channels from the
// database to use as initial state for the auto-pilot agent.
@@ -119,7 +125,7 @@ func (m *Manager) StartAgent() error {
return err
}
if err := pilot.Start(); err != nil {
if err := pilot.Start(ctx); err != nil {
return err
}
@@ -163,6 +169,8 @@ func (m *Manager) StartAgent() error {
return
case <-m.quit:
return
case <-ctx.Done():
return
}
}
@@ -233,6 +241,8 @@ func (m *Manager) StartAgent() error {
return
case <-m.quit:
return
case <-ctx.Done():
return
}
}
}()