Skip to content

Commit 308bc3c

Browse files
committed
refactor: Replace all calls to backoff::future::retry with ::retry_notify to be able to log how long we'll wait until next retry
1 parent 4625f94 commit 308bc3c

File tree

2 files changed

+64
-32
lines changed

2 files changed

+64
-32
lines changed

swap/src/asb/event_loop.rs

+32-20
Original file line numberDiff line numberDiff line change
@@ -717,28 +717,40 @@ impl EventLoopHandle {
717717

718718
let transfer_proof = self.build_transfer_proof_request(msg);
719719

720-
backoff::future::retry(backoff, || async {
721-
// Create a oneshot channel to receive the acknowledgment of the transfer proof
722-
let (singular_sender, singular_receiver) = oneshot::channel();
723-
724-
if let Err(err) = sender.send((self.peer, transfer_proof.clone(), singular_sender)) {
725-
let err = anyhow!(err).context("Failed to communicate transfer proof through event loop channel");
726-
tracing::error!(%err, swap_id = %self.swap_id, "Failed to send transfer proof");
727-
return Err(backoff::Error::permanent(err));
728-
}
729-
730-
match singular_receiver.await {
731-
Ok(Ok(())) => Ok(()),
732-
Ok(Err(err)) => {
733-
tracing::warn!(%err, "Failed to send transfer proof due to a network error. We will retry");
734-
Err(backoff::Error::transient(anyhow!(err)))
720+
backoff::future::retry_notify(
721+
backoff,
722+
|| async {
723+
// Create a oneshot channel to receive the acknowledgment of the transfer proof
724+
let (singular_sender, singular_receiver) = oneshot::channel();
725+
726+
if let Err(err) = sender.send((self.peer, transfer_proof.clone(), singular_sender))
727+
{
728+
return Err(backoff::Error::permanent(anyhow!(err).context(
729+
"Failed to communicate transfer proof through event loop channel",
730+
)));
735731
}
736-
Err(_) => {
737-
Err(backoff::Error::permanent(anyhow!("The sender channel should never be closed without sending a response")))
732+
733+
match singular_receiver.await {
734+
Ok(Ok(())) => Ok(()),
735+
Ok(Err(err)) => Err(backoff::Error::transient(
736+
anyhow!(err)
737+
.context("A network error occurred while sending the transfer proof"),
738+
)),
739+
Err(_) => Err(backoff::Error::permanent(anyhow!(
740+
"The sender channel should never be closed without sending a response"
741+
))),
738742
}
739-
}
740-
})
741-
.await?;
743+
},
744+
|e, wait_time: Duration| {
745+
tracing::warn!(
746+
swap_id = %self.swap_id,
747+
error = ?e,
748+
"Failed to send transfer proof. We will retry in {} seconds",
749+
wait_time.as_secs_f64()
750+
)
751+
},
752+
)
753+
.await?;
742754

743755
self.transfer_proof_sender.take();
744756

swap/src/cli/event_loop.rs

+32-12
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,12 @@ impl EventLoopHandle {
406406

407407
let backoff = Self::create_retry_config(EXECUTION_SETUP_PROTOCOL_TIMEOUT);
408408

409-
backoff::future::retry(backoff, || async {
409+
backoff::future::retry_notify(backoff, || async {
410410
match self.execution_setup_sender.send_receive(swap.clone()).await {
411411
Ok(Ok(state2)) => Ok(state2),
412412
// These are errors thrown by the swap_setup/bob behaviour
413413
Ok(Err(err)) => {
414-
tracing::warn!(%err, "Failed to setup swap. Will retry");
415-
Err(backoff::Error::transient(err))
414+
Err(backoff::Error::transient(err.context("A network error occurred while setting up the swap")))
416415
}
417416
// This will happen if we don't establish a connection to Alice within the timeout of the MPSC channel
418417
// The protocol does not dial Alice it self
@@ -424,6 +423,12 @@ impl EventLoopHandle {
424423
unreachable!("We never drop the receiver of the execution setup channel, so this should never happen")
425424
}
426425
}
426+
}, |err, wait_time: Duration| {
427+
tracing::warn!(
428+
error = ?err,
429+
"Failed to setup swap. We will retry in {} seconds",
430+
wait_time.as_secs_f64()
431+
)
427432
})
428433
.await
429434
.context("Failed to setup swap after retries")
@@ -448,17 +453,22 @@ impl EventLoopHandle {
448453

449454
let backoff = Self::create_retry_config(REQUEST_RESPONSE_PROTOCOL_TIMEOUT);
450455

451-
backoff::future::retry(backoff, || async {
456+
backoff::future::retry_notify(backoff, || async {
452457
match self.quote_sender.send_receive(()).await {
453458
Ok(Ok(quote)) => Ok(quote),
454459
Ok(Err(err)) => {
455-
tracing::warn!(%err, "Failed to request quote due to network error. Will retry");
456-
Err(backoff::Error::transient(err))
460+
Err(backoff::Error::transient(anyhow!(err).context("A network error occurred while requesting a quote")))
457461
}
458462
Err(_) => {
459463
unreachable!("We initiate the quote channel without a timeout and store both the sender and receiver in the same struct, so this should never happen");
460464
}
461465
}
466+
}, |err, wait_time: Duration| {
467+
tracing::warn!(
468+
error = ?err,
469+
"Failed to request quote. We will retry in {} seconds",
470+
wait_time.as_secs_f64()
471+
)
462472
})
463473
.await
464474
.context("Failed to request quote after retries")
@@ -469,17 +479,22 @@ impl EventLoopHandle {
469479

470480
let backoff = Self::create_retry_config(REQUEST_RESPONSE_PROTOCOL_TIMEOUT);
471481

472-
backoff::future::retry(backoff, || async {
482+
backoff::future::retry_notify(backoff, || async {
473483
match self.cooperative_xmr_redeem_sender.send_receive(()).await {
474484
Ok(Ok(response)) => Ok(response),
475485
Ok(Err(err)) => {
476-
tracing::warn!(%err, "Failed to request cooperative XMR redeem due to network error. Will retry");
477-
Err(backoff::Error::transient(err))
486+
Err(backoff::Error::transient(anyhow!(err).context("A network error occurred while requesting cooperative XMR redeem")))
478487
}
479488
Err(_) => {
480489
unreachable!("We initiate the cooperative xmr redeem channel without a timeout and store both the sender and receiver in the same struct, so this should never happen");
481490
}
482491
}
492+
}, |err, wait_time: Duration| {
493+
tracing::warn!(
494+
error = ?err,
495+
"Failed to request cooperative XMR redeem. We will retry in {} seconds",
496+
wait_time.as_secs_f64()
497+
)
483498
})
484499
.await
485500
.context("Failed to request cooperative XMR redeem after retries")
@@ -497,17 +512,22 @@ impl EventLoopHandle {
497512
.with_max_interval(REQUEST_RESPONSE_PROTOCOL_TIMEOUT)
498513
.build();
499514

500-
backoff::future::retry(backoff, || async {
515+
backoff::future::retry_notify(backoff, || async {
501516
match self.encrypted_signature_sender.send_receive(tx_redeem_encsig.clone()).await {
502517
Ok(Ok(_)) => Ok(()),
503518
Ok(Err(err)) => {
504-
tracing::warn!(%err, "Failed to send encrypted signature due to a network error. Will retry");
505-
Err(backoff::Error::transient(err))
519+
Err(backoff::Error::transient(anyhow!(err).context("A network error occurred while sending the encrypted signature")))
506520
}
507521
Err(_) => {
508522
unreachable!("We initiate the encrypted signature channel without a timeout and store both the sender and receiver in the same struct, so this should never happen");
509523
}
510524
}
525+
}, |err, wait_time: Duration| {
526+
tracing::warn!(
527+
error = ?err,
528+
"Failed to send encrypted signature. We will retry in {} seconds",
529+
wait_time.as_secs_f64()
530+
)
511531
})
512532
.await
513533
.context("Failed to send encrypted signature after retries")

0 commit comments

Comments
 (0)