@@ -13,6 +13,7 @@ import (
13
13
14
14
"github.com/btcsuite/btcd/btcec/v2"
15
15
"github.com/lightninglabs/lightning-terminal/autopilotserverrpc"
16
+ "github.com/lightninglabs/taproot-assets/fn"
16
17
"github.com/lightningnetwork/lnd/tor"
17
18
"google.golang.org/grpc"
18
19
"google.golang.org/grpc/credentials"
@@ -88,8 +89,9 @@ type Client struct {
88
89
89
90
featurePerms * featurePerms
90
91
91
- quit chan struct {}
92
- wg sync.WaitGroup
92
+ quit chan struct {}
93
+ wg sync.WaitGroup
94
+ cancel fn.Option [context.CancelFunc ]
93
95
}
94
96
95
97
type session struct {
@@ -124,16 +126,19 @@ func NewClient(cfg *Config) (Autopilot, error) {
124
126
}
125
127
126
128
// 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 {
128
130
var startErr error
129
131
c .start .Do (func () {
130
132
log .Infof ("Starting Autopilot Client" )
131
133
134
+ ctx , cancel := context .WithCancel (ctx )
135
+ c .cancel = fn .Some (cancel )
136
+
132
137
for _ , o := range opts {
133
138
o (c .cfg )
134
139
}
135
140
136
- version , err := c .getMinVersion (context . Background () )
141
+ version , err := c .getMinVersion (ctx )
137
142
if err != nil {
138
143
startErr = err
139
144
return
@@ -154,8 +159,8 @@ func (c *Client) Start(opts ...func(cfg *Config)) error {
154
159
}
155
160
156
161
c .wg .Add (2 )
157
- go c .activateSessionsForever ()
158
- go c .updateFeaturePermsForever ()
162
+ go c .activateSessionsForever (ctx )
163
+ go c .updateFeaturePermsForever (ctx )
159
164
})
160
165
161
166
return startErr
@@ -164,6 +169,7 @@ func (c *Client) Start(opts ...func(cfg *Config)) error {
164
169
// Stop cleans up any resources or goroutines managed by the Client.
165
170
func (c * Client ) Stop () {
166
171
c .stop .Do (func () {
172
+ c .cancel .WhenSome (func (fn context.CancelFunc ) { fn () })
167
173
close (c .quit )
168
174
c .wg .Wait ()
169
175
})
@@ -222,10 +228,9 @@ func (c *Client) SessionRevoked(ctx context.Context, pubKey *btcec.PublicKey) {
222
228
223
229
// activateSessionsForever periodically ensures that each of our active
224
230
// autopilot sessions are known by the autopilot to be active.
225
- func (c * Client ) activateSessionsForever () {
231
+ func (c * Client ) activateSessionsForever (ctx context. Context ) {
226
232
defer c .wg .Done ()
227
233
228
- ctx := context .Background ()
229
234
ticker := time .NewTicker (c .cfg .PingCadence )
230
235
defer ticker .Stop ()
231
236
@@ -273,10 +278,9 @@ func (c *Client) activateSessionsForever() {
273
278
// feature permissions list.
274
279
//
275
280
// NOTE: this MUST be called in a goroutine.
276
- func (c * Client ) updateFeaturePermsForever () {
281
+ func (c * Client ) updateFeaturePermsForever (ctx context. Context ) {
277
282
defer c .wg .Done ()
278
283
279
- ctx := context .Background ()
280
284
ticker := time .NewTicker (time .Second )
281
285
defer ticker .Stop ()
282
286
0 commit comments