|
| 1 | +package terminal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/lightninglabs/lndclient" |
| 9 | + "google.golang.org/grpc/codes" |
| 10 | + "google.golang.org/grpc/status" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + chainNotifierStartupMessage = "chain notifier RPC is still in the " + |
| 15 | + "process of starting" |
| 16 | +) |
| 17 | + |
| 18 | +// waitForChainNotifierReady blocks until lnd's chain notifier accepts a block |
| 19 | +// epoch subscription or the provided context is canceled. |
| 20 | +func waitForChainNotifierReady(ctx context.Context, |
| 21 | + notifier lndclient.ChainNotifierClient) error { |
| 22 | + |
| 23 | + const ( |
| 24 | + initialBackoff = 200 * time.Millisecond |
| 25 | + maxBackoff = 5 * time.Second |
| 26 | + streamGrace = 2 * time.Second |
| 27 | + ) |
| 28 | + |
| 29 | + backoff := initialBackoff |
| 30 | + |
| 31 | + for { |
| 32 | + select { |
| 33 | + case <-ctx.Done(): |
| 34 | + return ctx.Err() |
| 35 | + |
| 36 | + default: |
| 37 | + } |
| 38 | + |
| 39 | + attemptCtx, cancel := context.WithCancel(ctx) |
| 40 | + blockChan, errChan, err := notifier.RegisterBlockEpochNtfn(attemptCtx) |
| 41 | + if err != nil { |
| 42 | + cancel() |
| 43 | + |
| 44 | + isStartupErr := status.Code(err) == codes.Unavailable || |
| 45 | + strings.Contains(err.Error(), |
| 46 | + chainNotifierStartupMessage) |
| 47 | + if !isStartupErr { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + log.Warnf("Chain notifier RPC not ready, retrying in %v: %v", |
| 52 | + backoff, err) |
| 53 | + } else { |
| 54 | + timer := time.NewTimer(streamGrace) |
| 55 | + |
| 56 | + select { |
| 57 | + case <-blockChan: |
| 58 | + go drainReadinessNtfn( |
| 59 | + attemptCtx, cancel, blockChan, errChan, |
| 60 | + ) |
| 61 | + return nil |
| 62 | + |
| 63 | + case err = <-errChan: |
| 64 | + cancel() |
| 65 | + |
| 66 | + log.Warnf("Chain notifier stream ended early, "+ |
| 67 | + "retrying: %v", err) |
| 68 | + |
| 69 | + case <-timer.C: |
| 70 | + go drainReadinessNtfn( |
| 71 | + attemptCtx, cancel, blockChan, errChan, |
| 72 | + ) |
| 73 | + return nil |
| 74 | + |
| 75 | + case <-ctx.Done(): |
| 76 | + cancel() |
| 77 | + return ctx.Err() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + select { |
| 82 | + case <-time.After(backoff): |
| 83 | + |
| 84 | + case <-ctx.Done(): |
| 85 | + return ctx.Err() |
| 86 | + } |
| 87 | + |
| 88 | + backoff *= 2 |
| 89 | + if backoff > maxBackoff { |
| 90 | + backoff = maxBackoff |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// drainReadinessNtfn discards notifications until the daemon shuts down, |
| 96 | +// allowing the readiness subscription to stay open without affecting lnd logs. |
| 97 | +func drainReadinessNtfn(ctx context.Context, cancel func(), |
| 98 | + blockChan <-chan int32, errChan <-chan error) { |
| 99 | + |
| 100 | + defer cancel() |
| 101 | + |
| 102 | + for { |
| 103 | + select { |
| 104 | + case <-blockChan: |
| 105 | + |
| 106 | + case <-errChan: |
| 107 | + return |
| 108 | + |
| 109 | + case <-ctx.Done(): |
| 110 | + return |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments