Skip to content

Commit e853793

Browse files
Copy Retry from invoice module to outbound_payment module
Also configure it such that in std tests, it will use SinceEpoch instead of Instant so time can be manually advanced.
1 parent 0672900 commit e853793

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lightning/src/ln/outbound_payment.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,37 @@ impl PendingOutboundPayment {
188188
}
189189
}
190190

191+
/// Strategies available to retry payment path failures.
192+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
193+
pub enum Retry {
194+
/// Max number of attempts to retry payment.
195+
///
196+
/// Note that this is the number of *path* failures, not full payment retries. For multi-path
197+
/// payments, if this is less than the total number of paths, we will never even retry all of the
198+
/// payment's paths.
199+
Attempts(usize),
200+
#[cfg(not(feature = "no-std"))]
201+
/// Time elapsed before abandoning retries for a payment.
202+
Timeout(core::time::Duration),
203+
}
204+
205+
impl Retry {
206+
pub(crate) fn is_retryable_now(&self, attempts: &PaymentAttempts) -> bool {
207+
match (self, attempts) {
208+
(Retry::Attempts(max_retry_count), PaymentAttempts { count, .. }) => {
209+
if *max_retry_count == 0 { return false }
210+
*max_retry_count >= count.load(Ordering::Acquire)
211+
},
212+
#[cfg(all(not(feature = "no-std"), not(test)))]
213+
(Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
214+
*max_duration >= std::time::Instant::now().duration_since(*first_attempted_at),
215+
#[cfg(all(not(feature = "no-std"), test))]
216+
(Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
217+
*max_duration >= SinceEpoch::now().duration_since(*first_attempted_at),
218+
}
219+
}
220+
}
221+
191222
pub(crate) type PaymentAttempts = PaymentAttemptsUsingTime<ConfiguredTime>;
192223

193224
/// Storing minimal payment attempts information required for determining if a outbound payment can

0 commit comments

Comments
 (0)