@@ -22,9 +22,14 @@ use crate::routing::router::{PaymentParameters, Route, RouteHop, RouteParameters
22
22
use crate :: util:: errors:: APIError ;
23
23
use crate :: util:: events;
24
24
use crate :: util:: logger:: Logger ;
25
+ use crate :: util:: time:: Time ;
26
+ #[ cfg( all( not( feature = "no-std" ) , test) ) ]
27
+ use crate :: util:: time:: tests:: SinceEpoch ;
25
28
26
29
use core:: cmp;
30
+ use core:: fmt:: { self , Display , Formatter } ;
27
31
use core:: ops:: Deref ;
32
+
28
33
use crate :: prelude:: * ;
29
34
use crate :: sync:: Mutex ;
30
35
@@ -182,6 +187,48 @@ impl PendingOutboundPayment {
182
187
}
183
188
}
184
189
190
+ pub ( crate ) type PaymentAttempts = PaymentAttemptsUsingTime < ConfiguredTime > ;
191
+
192
+ /// Storing minimal payment attempts information required for determining if a outbound payment can
193
+ /// be retried.
194
+ pub ( crate ) struct PaymentAttemptsUsingTime < T : Time > {
195
+ /// This count will be incremented only after the result of the attempt is known. When it's 0,
196
+ /// it means the result of the first attempt is not known yet.
197
+ pub ( crate ) count : usize ,
198
+ /// This field is only used when retry is `Retry::Timeout` which is only build with feature std
199
+ first_attempted_at : T
200
+ }
201
+
202
+ #[ cfg( not( any( feature = "no-std" , test) ) ) ]
203
+ type ConfiguredTime = std:: time:: Instant ;
204
+ #[ cfg( feature = "no-std" ) ]
205
+ type ConfiguredTime = crate :: util:: time:: Eternity ;
206
+ #[ cfg( all( not( feature = "no-std" ) , test) ) ]
207
+ type ConfiguredTime = SinceEpoch ;
208
+
209
+ impl < T : Time > PaymentAttemptsUsingTime < T > {
210
+ pub ( crate ) fn new ( ) -> Self {
211
+ PaymentAttemptsUsingTime {
212
+ count : 0 ,
213
+ first_attempted_at : T :: now ( )
214
+ }
215
+ }
216
+ }
217
+
218
+ impl < T : Time > Display for PaymentAttemptsUsingTime < T > {
219
+ fn fmt ( & self , f : & mut Formatter ) -> Result < ( ) , fmt:: Error > {
220
+ #[ cfg( feature = "no-std" ) ]
221
+ return write ! ( f, "attempts: {}" , self . count) ;
222
+ #[ cfg( not( feature = "no-std" ) ) ]
223
+ return write ! (
224
+ f,
225
+ "attempts: {}, duration: {}s" ,
226
+ self . count,
227
+ T :: now( ) . duration_since( self . first_attempted_at) . as_secs( )
228
+ ) ;
229
+ }
230
+ }
231
+
185
232
/// If a payment fails to send, it can be in one of several states. This enum is returned as the
186
233
/// Err() type describing which state the payment is in, see the description of individual enum
187
234
/// states for more.
0 commit comments