@@ -31,6 +31,11 @@ pub(crate) struct PubSubService<T> {
31
31
32
32
/// The request manager.
33
33
pub ( crate ) in_flights : RequestManager ,
34
+
35
+ /// Number of retries. Default is 10.
36
+ ///
37
+ /// Every retry is made at an interval of 3 seconds.
38
+ pub ( crate ) retries : u32 ,
34
39
}
35
40
36
41
impl < T : PubSubConnect > PubSubService < T > {
@@ -45,6 +50,7 @@ impl<T: PubSubConnect> PubSubService<T> {
45
50
reqs,
46
51
subs : SubscriptionManager :: default ( ) ,
47
52
in_flights : Default :: default ( ) ,
53
+ retries : 10 ,
48
54
} ;
49
55
this. spawn ( ) ;
50
56
Ok ( PubSubFrontend :: new ( tx) )
@@ -190,6 +196,31 @@ impl<T: PubSubConnect> PubSubService<T> {
190
196
Ok ( ( ) )
191
197
}
192
198
199
+ /// Attempt to reconnect with retries
200
+ async fn reconnect_with_retries ( & mut self ) -> TransportResult < ( ) > {
201
+ let mut retry_count = 0 ;
202
+ loop {
203
+ match self . reconnect ( ) . await {
204
+ Ok ( ( ) ) => break Ok ( ( ) ) ,
205
+ Err ( e) => {
206
+ retry_count += 1 ;
207
+ if retry_count >= self . retries {
208
+ error ! (
209
+ "Reconnect failed after {} attempts, shutting down: {e}" ,
210
+ retry_count
211
+ ) ;
212
+ break Err ( e) ;
213
+ }
214
+ warn ! (
215
+ "Reconnection attempt {}/{} failed: {}. Retrying in 3 seconds..." ,
216
+ retry_count, self . retries, e
217
+ ) ;
218
+ tokio:: time:: sleep ( tokio:: time:: Duration :: from_secs ( 3 ) ) . await ;
219
+ }
220
+ }
221
+ }
222
+ }
223
+
193
224
/// Spawn the service.
194
225
pub ( crate ) fn spawn ( mut self ) {
195
226
let fut = async move {
@@ -205,16 +236,14 @@ impl<T: PubSubConnect> PubSubService<T> {
205
236
if let Err ( e) = self . handle_item( item) {
206
237
break Err ( e)
207
238
}
208
- } else if let Err ( e) = self . reconnect( ) . await {
209
- error!( "Reconnect failed, shutting down: {e}" ) ;
239
+ } else if let Err ( e) = self . reconnect_with_retries( ) . await {
210
240
break Err ( e)
211
241
}
212
242
}
213
243
214
244
_ = & mut self . handle. error => {
215
245
error!( "Pubsub service backend error." ) ;
216
- if let Err ( e) = self . reconnect( ) . await {
217
- error!( "Reconnect failed, shutting down: {e}" ) ;
246
+ if let Err ( e) = self . reconnect_with_retries( ) . await {
218
247
break Err ( e)
219
248
}
220
249
}
0 commit comments