Skip to content

Commit bbcd1b6

Browse files
committed
lndinit: use an exp backoff instead of connectionRetryInterval
1 parent 1df4ce3 commit bbcd1b6

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

cmd_wait_ready.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import (
1515
"google.golang.org/grpc/credentials"
1616
)
1717

18-
var (
19-
connectionRetryInterval = time.Millisecond * 250
18+
const (
19+
baseRetry = time.Millisecond * 250
20+
maxRetry = 10 * time.Second
2021
)
2122

2223
type waitReadyCommand struct {
@@ -72,8 +73,8 @@ func waitUntilStatus(rpcServer string, desiredState lnrpc.WalletState,
7273

7374
logger.Infof("Waiting for lnd to become ready (want state %v)", desiredState)
7475

75-
connectionRetryTicker := time.NewTicker(connectionRetryInterval)
7676
timeoutChan := time.After(timeout)
77+
retryDelay := baseRetry
7778

7879
connectionLoop:
7980
for {
@@ -83,13 +84,14 @@ connectionLoop:
8384
logger.Errorf("Connection to lnd not successful: %v", err)
8485

8586
select {
86-
case <-connectionRetryTicker.C:
87+
case <-time.After(retryDelay):
8788
case <-timeoutChan:
8889
return fmt.Errorf("timeout reached")
8990
case <-shutdown:
9091
return nil
9192
}
9293

94+
retryDelay = nextBackoff(retryDelay)
9395
continue
9496
}
9597

@@ -102,13 +104,14 @@ connectionLoop:
102104
err)
103105

104106
select {
105-
case <-connectionRetryTicker.C:
107+
case <-time.After(retryDelay):
106108
case <-timeoutChan:
107109
return fmt.Errorf("timeout reached")
108110
case <-shutdown:
109111
return nil
110112
}
111113

114+
retryDelay = nextBackoff(retryDelay)
112115
continue
113116
}
114117

@@ -127,19 +130,23 @@ connectionLoop:
127130
logger.Errorf("Error receiving status update: %v", err)
128131

129132
select {
130-
case <-connectionRetryTicker.C:
133+
case <-time.After(retryDelay):
131134
case <-timeoutChan:
132135
return fmt.Errorf("timeout reached")
133136
case <-shutdown:
134137
return nil
135138
}
136139

140+
retryDelay = nextBackoff(retryDelay)
137141
// Something went wrong, perhaps lnd shut down
138142
// before becoming active. Let's retry the whole
139143
// connection again.
140144
continue connectionLoop
141145
}
142146

147+
// We have a live stream now, so reset the backoff.
148+
retryDelay = baseRetry
149+
143150
logger.Infof("Received update from lnd, wallet status is now: "+
144151
"%v", msg.State)
145152

@@ -188,3 +195,14 @@ func getStatusConnection(rpcServer string) (lnrpc.StateClient, error) {
188195

189196
return lnrpc.NewStateClient(conn), nil
190197
}
198+
199+
// nextBackoff calculates the next duration to wait before attempting another
200+
// connection.
201+
func nextBackoff(d time.Duration) time.Duration {
202+
d *= 2
203+
if d > maxRetry {
204+
d = maxRetry
205+
}
206+
207+
return d
208+
}

0 commit comments

Comments
 (0)