@@ -15,8 +15,9 @@ import (
15
15
"google.golang.org/grpc/credentials"
16
16
)
17
17
18
- var (
19
- connectionRetryInterval = time .Millisecond * 250
18
+ const (
19
+ baseRetry = time .Millisecond * 250
20
+ maxRetry = 10 * time .Second
20
21
)
21
22
22
23
type waitReadyCommand struct {
@@ -72,8 +73,8 @@ func waitUntilStatus(rpcServer string, desiredState lnrpc.WalletState,
72
73
73
74
logger .Infof ("Waiting for lnd to become ready (want state %v)" , desiredState )
74
75
75
- connectionRetryTicker := time .NewTicker (connectionRetryInterval )
76
76
timeoutChan := time .After (timeout )
77
+ retryDelay := baseRetry
77
78
78
79
connectionLoop:
79
80
for {
@@ -83,13 +84,14 @@ connectionLoop:
83
84
logger .Errorf ("Connection to lnd not successful: %v" , err )
84
85
85
86
select {
86
- case <- connectionRetryTicker . C :
87
+ case <- time . After ( retryDelay ) :
87
88
case <- timeoutChan :
88
89
return fmt .Errorf ("timeout reached" )
89
90
case <- shutdown :
90
91
return nil
91
92
}
92
93
94
+ retryDelay = nextBackoff (retryDelay )
93
95
continue
94
96
}
95
97
@@ -102,13 +104,14 @@ connectionLoop:
102
104
err )
103
105
104
106
select {
105
- case <- connectionRetryTicker . C :
107
+ case <- time . After ( retryDelay ) :
106
108
case <- timeoutChan :
107
109
return fmt .Errorf ("timeout reached" )
108
110
case <- shutdown :
109
111
return nil
110
112
}
111
113
114
+ retryDelay = nextBackoff (retryDelay )
112
115
continue
113
116
}
114
117
@@ -127,19 +130,23 @@ connectionLoop:
127
130
logger .Errorf ("Error receiving status update: %v" , err )
128
131
129
132
select {
130
- case <- connectionRetryTicker . C :
133
+ case <- time . After ( retryDelay ) :
131
134
case <- timeoutChan :
132
135
return fmt .Errorf ("timeout reached" )
133
136
case <- shutdown :
134
137
return nil
135
138
}
136
139
140
+ retryDelay = nextBackoff (retryDelay )
137
141
// Something went wrong, perhaps lnd shut down
138
142
// before becoming active. Let's retry the whole
139
143
// connection again.
140
144
continue connectionLoop
141
145
}
142
146
147
+ // We have a live stream now, so reset the backoff.
148
+ retryDelay = baseRetry
149
+
143
150
logger .Infof ("Received update from lnd, wallet status is now: " +
144
151
"%v" , msg .State )
145
152
@@ -188,3 +195,14 @@ func getStatusConnection(rpcServer string) (lnrpc.StateClient, error) {
188
195
189
196
return lnrpc .NewStateClient (conn ), nil
190
197
}
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