Skip to content

Commit 25efe67

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 aa9ca46 commit 25efe67

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
@@ -333,8 +333,18 @@ func (a *Aperture) Start(errChan chan error) error {
333333
"session: %w", err)
334334
}
335335

336+
// An LNC session will automatically try to reconnect to
337+
// the node whenever an error occurs. Instead of
338+
// shutting the proxy down we will just log the error.
339+
lncErrChan := make(chan error)
340+
go func() {
341+
for err := range lncErrChan {
342+
log.Errorf("lnc session error: %v", err)
343+
}
344+
}()
345+
336346
a.challenger, err = challenger.NewLNCChallenger(
337-
session, lncStore, genInvoiceReq, errChan,
347+
session, lncStore, genInvoiceReq, lncErrChan,
338348
)
339349
if err != nil {
340350
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 {
@@ -232,6 +238,7 @@ func (l *LndChallenger) readInvoiceStream(
232238
func (l *LndChallenger) Stop() {
233239
l.invoicesCancel()
234240
close(l.quit)
241+
close(l.errChan)
235242
l.wg.Wait()
236243
}
237244

@@ -251,7 +258,10 @@ func (l *LndChallenger) NewChallenge(price int64) (string, lntypes.Hash,
251258
}
252259

253260
ctx := l.clientCtx()
254-
response, err := l.client.AddInvoice(ctx, invoice)
261+
ctxt, cancel := context.WithTimeout(ctx, defaultLNDCallTimeout)
262+
defer cancel()
263+
264+
response, err := l.client.AddInvoice(ctxt, invoice)
255265
if err != nil {
256266
log.Errorf("Error adding invoice: %v", err)
257267
return "", lntypes.ZeroHash, err

0 commit comments

Comments
 (0)