@@ -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
9597type 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.
165170func (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
0 commit comments