Skip to content

Commit edea59e

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 7f1411c commit edea59e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

aperture.go

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

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

challenger/lnd.go

Lines changed: 11 additions & 1 deletion
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 {
@@ -244,6 +250,7 @@ func (l *LndChallenger) readInvoiceStream(
244250
func (l *LndChallenger) Stop() {
245251
l.invoicesCancel()
246252
close(l.quit)
253+
close(l.errChan)
247254
l.wg.Wait()
248255
}
249256

@@ -263,7 +270,10 @@ func (l *LndChallenger) NewChallenge(price int64) (string, lntypes.Hash,
263270
}
264271

265272
ctx := l.clientCtx()
266-
response, err := l.client.AddInvoice(ctx, invoice)
273+
ctxt, cancel := context.WithTimeout(ctx, defaultLNDCallTimeout)
274+
defer cancel()
275+
276+
response, err := l.client.AddInvoice(ctxt, invoice)
267277
if err != nil {
268278
log.Errorf("Error adding invoice: %v", err)
269279
return "", lntypes.ZeroHash, err

0 commit comments

Comments
 (0)