Skip to content

Commit

Permalink
fix(libp2p): Use Instant instead of KeepAlive in swap/src/network/swa…
Browse files Browse the repository at this point in the history
…p_setup/alice.rs

See: libp2p/rust-libp2p#3844
  • Loading branch information
binarybaron committed Oct 23, 2024
1 parent 03367ba commit 01e8810
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions swap/src/network/swap_setup/alice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ use libp2p::{Multiaddr, PeerId};
use std::collections::VecDeque;
use std::fmt::Debug;
use std::task::Poll;
use std::time::Duration;
use std::time::{Instant, Duration};
use uuid::Uuid;
use futures::AsyncWriteExt;

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
Expand Down Expand Up @@ -219,8 +220,12 @@ pub struct Handler<LR> {
latest_rate: LR,
resume_only: bool,

timeout: Duration,
keep_alive: KeepAlive,
// This is the timeout for the negotiation phase where Alice and Bob exchange messages
negotiation_timeout: Duration,

// If set to None, we will keep the connection alive indefinitely
// If set to Some, we will keep the connection alive until the given instant
keep_alive_until: Option<Instant>,
}

impl<LR> Handler<LR> {
Expand All @@ -239,8 +244,8 @@ impl<LR> Handler<LR> {
env_config,
latest_rate,
resume_only,
timeout: Duration::from_secs(120),
keep_alive: KeepAlive::Until(Instant::now() + Duration::from_secs(10)),
negotiation_timeout: Duration::from_secs(120),
keep_alive_until: Some(Instant::now() + Duration::from_secs(30)),
}
}
}
Expand Down Expand Up @@ -268,8 +273,10 @@ where
}

fn connection_keep_alive(&self) -> bool {
// self.keep_alive
false
match self.keep_alive_until {
None => true,
Some(keep_alive_until) => Instant::now() < keep_alive_until,
}
}

#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -313,7 +320,9 @@ where
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(substream) => {
self.keep_alive = KeepAlive::Yes;
self.keep_alive_until = None;

let mut substream = substream.protocol;

let (sender, receiver) = bmrng::channel_with_timeout::<bitcoin::Amount, WalletSnapshot>(
1,
Expand All @@ -325,7 +334,7 @@ where
let latest_rate = self.latest_rate.latest_rate();
let env_config = self.env_config;

let protocol = tokio::time::timeout(self.timeout, async move {
let protocol = tokio::time::timeout(self.negotiation_timeout, async move {
let request = swap_setup::read_cbor_message::<SpotPriceRequest>(&mut substream)
.await
.context("Failed to read spot price request")?;
Expand Down Expand Up @@ -449,7 +458,7 @@ where
Ok((swap_id, state3))
});

let max_seconds = self.timeout.as_secs();
let max_seconds = self.negotiation_timeout.as_secs();
self.inbound_stream = OptionFuture::from(Some(
async move {
protocol.await.with_context(|| {
Expand Down

0 comments on commit 01e8810

Please sign in to comment.