@@ -194,17 +194,15 @@ where
194
194
/// be retried.
195
195
#[ derive( Clone , Copy ) ]
196
196
struct PaymentAttempts < T : Time > {
197
- /// This should be >= 1 as we only insert PaymentAttempts for a PaymentHash if there is at least
198
- /// one attempt.
199
- count : usize ,
197
+ retry_count : usize ,
200
198
/// This field is only used when retry is [`Retry::Timeout`] which is only build with feature std
201
199
first_attempted_at : T
202
200
}
203
201
204
202
impl < T : Time > PaymentAttempts < T > {
205
203
fn new ( ) -> Self {
206
204
PaymentAttempts {
207
- count : 1 ,
205
+ retry_count : 0 ,
208
206
first_attempted_at : T :: now ( )
209
207
}
210
208
}
@@ -216,13 +214,13 @@ impl<T: Time> Debug for PaymentAttempts<T> {
216
214
return write ! (
217
215
f,
218
216
"attempts: {}" ,
219
- self . count
217
+ self . retry_count + 1
220
218
) ;
221
219
#[ cfg( not( feature = "no-std" ) ) ]
222
220
return write ! (
223
221
f,
224
222
"attempts: {}, duration: {:?}" ,
225
- self . count ,
223
+ self . retry_count + 1 ,
226
224
T :: now( ) . duration_since( self . first_attempted_at)
227
225
) ;
228
226
}
@@ -280,8 +278,7 @@ pub enum Retry {
280
278
impl Retry {
281
279
fn is_retryable_now < T : Time > ( & self , attempts : & PaymentAttempts < T > ) -> bool {
282
280
match self {
283
- Retry :: Attempts ( 0 ) => false ,
284
- Retry :: Attempts ( max_retry_count) => max_retry_count + 1 > attempts. count ,
281
+ Retry :: Attempts ( max_retry_count) => max_retry_count > & attempts. retry_count ,
285
282
#[ cfg( feature = "std" ) ]
286
283
Retry :: Timeout ( max_duration) => * max_duration >= T :: now ( ) . duration_since ( attempts. first_attempted_at )
287
284
}
@@ -438,7 +435,7 @@ where
438
435
let mut payment_cache = self . payment_cache . lock ( ) . unwrap ( ) ;
439
436
let payment_attempts = payment_cache. get_mut ( & payment_hash) . unwrap ( ) ;
440
437
if self . retry . is_retryable_now ( payment_attempts) {
441
- payment_attempts. count += 1 ;
438
+ payment_attempts. retry_count += 1 ;
442
439
core:: mem:: drop ( payment_cache) ;
443
440
Ok ( self . pay_internal ( params, payment_hash, send_payment) ?)
444
441
} else {
@@ -471,12 +468,17 @@ where
471
468
let attempts = * self . payment_cache . lock ( ) . unwrap ( )
472
469
. entry ( payment_hash)
473
470
. and_modify ( |attempts| {
474
- attempts. count += 1 ;
471
+ attempts. retry_count += 1 ;
475
472
} )
476
473
. or_insert ( PaymentAttempts :: new ( ) ) ;
477
474
475
+ let prevoius_retry_count = match attempts. retry_count {
476
+ 0 => 0 ,
477
+ retry_count => retry_count - 1
478
+ } ;
479
+
478
480
if ! self . retry . is_retryable_now ( & PaymentAttempts {
479
- count : attempts . count - 1 ,
481
+ retry_count : prevoius_retry_count ,
480
482
..attempts
481
483
} ) {
482
484
log_trace ! ( self . logger, "Payment {} exceeded maximum attempts; not retrying ({:?})" , log_bytes!( payment_hash. 0 ) , attempts) ;
@@ -584,7 +586,7 @@ where
584
586
let mut payment_cache = self . payment_cache . lock ( ) . unwrap ( ) ;
585
587
let attempts = payment_cache
586
588
. remove ( payment_hash)
587
- . map_or ( 1 , |attempts| attempts. count + 1 ) ;
589
+ . map_or ( 1 , |attempts| attempts. retry_count + 1 ) ;
588
590
log_trace ! ( self . logger, "Payment {} succeeded (attempts: {})" , log_bytes!( payment_hash. 0 ) , attempts) ;
589
591
} ,
590
592
_ => { } ,
0 commit comments