Skip to content

Commit 20c1315

Browse files
committed
multi: allow LNC reconnects without restarting the proxy
LNC will automatically try to reconnect to the LND node whenever there is an error. Until now, losing the connection with the LND node made the proxy shut down. When using LNC, we can simply log that we received an error and let the library handle the reconnection. Because we do not want to overheat the server when the backend lnd node is down, we need to make sure that we add a timeout to the client calls.
1 parent 8cf43a4 commit 20c1315

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

aperture.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,18 @@ func (a *Aperture) Start(errChan chan error) error {
335335
"session: %w", err)
336336
}
337337

338+
// An LNC session will automatically try to reconnect to
339+
// the node whenever an error occurs. Instead of
340+
// shutting the proxy down we will just log the error.
341+
lncErrChan := make(chan error)
342+
go func() {
343+
for err := range lncErrChan {
344+
log.Errorf("lnc session error: %v", err)
345+
}
346+
}()
347+
338348
a.challenger, err = challenger.NewLNCChallenger(
339-
session, lncStore, genInvoiceReq, errChan,
349+
session, lncStore, genInvoiceReq, lncErrChan,
340350
)
341351
if err != nil {
342352
return fmt.Errorf("unable to start lnc "+

challenger/lnd.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import (
1313
"github.com/lightningnetwork/lnd/lntypes"
1414
)
1515

16+
const (
17+
// defaultLNDCallTimeout is the default timeout used for calls to the
18+
// LND client.
19+
defaultLNDCallTimeout = time.Second * 10
20+
)
21+
1622
// LndChallenger is a challenger that uses an lnd backend to create new LSAT
1723
// payment challenges.
1824
type LndChallenger struct {
@@ -199,12 +205,20 @@ func (l *LndChallenger) readInvoiceStream(
199205
log.Errorf("Received error from invoice subscription: "+
200206
"%v", err)
201207

208+
// If the challenger was shutting down, we will receive the
209+
// "client connection is closing" error for LNC connections
210+
// but there is no need to signal the error to the main goroutine.
211+
select {
212+
case <-l.quit:
213+
return
214+
default:
215+
}
216+
202217
// The connection is faulty, we can't continue to
203218
// function properly. Signal the error to the main
204219
// goroutine to force a shutdown/restart.
205220
select {
206221
case l.errChan <- err:
207-
case <-l.quit:
208222
default:
209223
}
210224

@@ -244,6 +258,7 @@ func (l *LndChallenger) readInvoiceStream(
244258
func (l *LndChallenger) Stop() {
245259
l.invoicesCancel()
246260
close(l.quit)
261+
close(l.errChan)
247262
l.wg.Wait()
248263
}
249264

@@ -263,7 +278,10 @@ func (l *LndChallenger) NewChallenge(price int64) (string, lntypes.Hash,
263278
}
264279

265280
ctx := l.clientCtx()
266-
response, err := l.client.AddInvoice(ctx, invoice)
281+
ctxt, cancel := context.WithTimeout(ctx, defaultLNDCallTimeout)
282+
defer cancel()
283+
284+
response, err := l.client.AddInvoice(ctxt, invoice)
267285
if err != nil {
268286
log.Errorf("Error adding invoice: %v", err)
269287
return "", lntypes.ZeroHash, err

0 commit comments

Comments
 (0)