Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Send full list on the first request #1

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::VecDeque;
use std::fmt;
use std::sync::Arc;
use std::task::{ready, Context, Poll};
use std::time::{Duration, Instant};

use asynchronous_codec::FramedWrite;
use blockstore::{Blockstore, BlockstoreError};
Expand Down Expand Up @@ -29,6 +30,8 @@ use crate::utils::convert_cid;
use crate::wantlist::{Wantlist, WantlistState};
use crate::{BitswapError, BitswapEvent, Result, ToBehaviourEvent, ToHandlerEvent};

const SEND_FULL_INTERVAL: Duration = Duration::from_secs(30);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct QueryId(u64);

Expand Down Expand Up @@ -77,6 +80,7 @@ where
struct PeerState<const S: usize> {
sending: Arc<Mutex<SendingState>>,
wantlist: WantlistState<S>,
last_send_full_tm: Option<Instant>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -113,6 +117,7 @@ where
PeerState {
sending: Arc::new(Mutex::new(SendingState::Ready)),
wantlist: WantlistState::new(),
last_send_full_tm: None,
},
);

Expand Down Expand Up @@ -264,6 +269,7 @@ where
for (peer, state) in self.peers.iter_mut() {
let mut sending_state = state.sending.lock().unwrap();

// Decide if full list is needed or not.
let send_full = match &*sending_state {
SendingState::Sending => {
if Arc::strong_count(&state.sending) == 1 {
Expand All @@ -277,7 +283,13 @@ where
continue;
}
}
SendingState::Ready => false,
SendingState::Ready => match state.last_send_full_tm {
// Send full list if interval time is elapsed.
Some(tm) => tm.elapsed() >= SEND_FULL_INTERVAL,
// Send full list the first time.
None => true,
},
// State is poisoned, send full list to recover.
SendingState::Poisoned => true,
};

Expand All @@ -299,6 +311,10 @@ where
continue;
}

if wantlist.full {
state.last_send_full_tm = Some(Instant::now());
}

self.queue.push_back(ToSwarm::NotifyHandler {
peer_id: peer.to_owned(),
handler: NotifyHandler::Any,
Expand Down