From a0b67da590fe9ec3543b93d225c77aa5e102bfa7 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Wed, 28 Apr 2021 17:59:31 +0200 Subject: [PATCH] loop: wait for chain notifier server to start --- executor.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/executor.go b/executor.go index b0ace93..8008c09 100644 --- a/executor.go +++ b/executor.go @@ -3,6 +3,7 @@ package loop import ( "context" "fmt" + "strings" "sync" "sync/atomic" "time" @@ -52,10 +53,37 @@ func newExecutor(cfg *executorConfig) *executor { func (s *executor) run(mainCtx context.Context, statusChan chan<- SwapInfo) error { - blockEpochChan, blockErrorChan, err := - s.lnd.ChainNotifier.RegisterBlockEpochNtfn(mainCtx) - if err != nil { - return err + var ( + err error + blockEpochChan <-chan int32 + blockErrorChan <-chan error + ) + + for { + blockEpochChan, blockErrorChan, err = + s.lnd.ChainNotifier.RegisterBlockEpochNtfn(mainCtx) + if err != nil { + if strings.Contains(err.Error(), + "in the process of starting") { + + log.Warnf("LND chain notifier server not " + + "ready yet, retrying with delay") + + // Give chain notifier some time to start and + // try to re-attempt block epoch subscription. + select { + case <-time.After(500 * time.Millisecond): + continue + + case <-mainCtx.Done(): + return err + } + } + + return err + } + + break } // Before starting, make sure we have an up to date block height.