Skip to content

Commit 0407505

Browse files
committed
autopilotserver: thread context through
1 parent 1a30bdb commit 0407505

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

autopilotserver/client.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/btcsuite/btcd/btcec/v2"
1515
"github.com/lightninglabs/lightning-terminal/autopilotserverrpc"
16+
"github.com/lightninglabs/taproot-assets/fn"
1617
"github.com/lightningnetwork/lnd/tor"
1718
"google.golang.org/grpc"
1819
"google.golang.org/grpc/credentials"
@@ -88,8 +89,9 @@ type Client struct {
8889

8990
featurePerms *featurePerms
9091

91-
quit chan struct{}
92-
wg sync.WaitGroup
92+
quit chan struct{}
93+
wg sync.WaitGroup
94+
cancel fn.Option[context.CancelFunc]
9395
}
9496

9597
type session struct {
@@ -124,16 +126,19 @@ func NewClient(cfg *Config) (Autopilot, error) {
124126
}
125127

126128
// Start kicks off all the goroutines required by the Client.
127-
func (c *Client) Start(opts ...func(cfg *Config)) error {
129+
func (c *Client) Start(ctx context.Context, opts ...func(cfg *Config)) error {
128130
var startErr error
129131
c.start.Do(func() {
130132
log.Infof("Starting Autopilot Client")
131133

134+
ctx, cancel := context.WithCancel(ctx)
135+
c.cancel = fn.Some(cancel)
136+
132137
for _, o := range opts {
133138
o(c.cfg)
134139
}
135140

136-
version, err := c.getMinVersion(context.Background())
141+
version, err := c.getMinVersion(ctx)
137142
if err != nil {
138143
startErr = err
139144
return
@@ -154,8 +159,8 @@ func (c *Client) Start(opts ...func(cfg *Config)) error {
154159
}
155160

156161
c.wg.Add(2)
157-
go c.activateSessionsForever()
158-
go c.updateFeaturePermsForever()
162+
go c.activateSessionsForever(ctx)
163+
go c.updateFeaturePermsForever(ctx)
159164
})
160165

161166
return startErr
@@ -164,6 +169,7 @@ func (c *Client) Start(opts ...func(cfg *Config)) error {
164169
// Stop cleans up any resources or goroutines managed by the Client.
165170
func (c *Client) Stop() {
166171
c.stop.Do(func() {
172+
c.cancel.WhenSome(func(fn context.CancelFunc) { fn() })
167173
close(c.quit)
168174
c.wg.Wait()
169175
})
@@ -222,10 +228,9 @@ func (c *Client) SessionRevoked(ctx context.Context, pubKey *btcec.PublicKey) {
222228

223229
// activateSessionsForever periodically ensures that each of our active
224230
// autopilot sessions are known by the autopilot to be active.
225-
func (c *Client) activateSessionsForever() {
231+
func (c *Client) activateSessionsForever(ctx context.Context) {
226232
defer c.wg.Done()
227233

228-
ctx := context.Background()
229234
ticker := time.NewTicker(c.cfg.PingCadence)
230235
defer ticker.Stop()
231236

@@ -273,10 +278,9 @@ func (c *Client) activateSessionsForever() {
273278
// feature permissions list.
274279
//
275280
// NOTE: this MUST be called in a goroutine.
276-
func (c *Client) updateFeaturePermsForever() {
281+
func (c *Client) updateFeaturePermsForever(ctx context.Context) {
277282
defer c.wg.Done()
278283

279-
ctx := context.Background()
280284
ticker := time.NewTicker(time.Second)
281285
defer ticker.Stop()
282286

autopilotserver/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestAutopilotClient(t *testing.T) {
3232
PingCadence: time.Second,
3333
})
3434
require.NoError(t, err)
35-
require.NoError(t, client.Start())
35+
require.NoError(t, client.Start(ctx))
3636
t.Cleanup(client.Stop)
3737

3838
privKey, err := btcec.NewPrivateKey()

autopilotserver/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Autopilot interface {
4545
SessionRevoked(ctx context.Context, key *btcec.PublicKey)
4646

4747
// Start kicks off the goroutines of the client.
48-
Start(opts ...func(cfg *Config)) error
48+
Start(ctx context.Context, opts ...func(cfg *Config)) error
4949

5050
// Stop cleans up any resources held by the client.
5151
Stop()

terminal.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,8 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context,
10051005
}
10061006
}
10071007

1008-
if err = g.autopilotClient.Start(withLndVersion); err != nil {
1008+
err = g.autopilotClient.Start(ctx, withLndVersion)
1009+
if err != nil {
10091010
return fmt.Errorf("could not start the autopilot "+
10101011
"client: %v", err)
10111012
}

0 commit comments

Comments
 (0)