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: alias don't switch from check to scan after timeout #18

Merged
merged 1 commit into from
Nov 8, 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
37 changes: 35 additions & 2 deletions src/service/alias_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ impl AliasServiceRequester {

pub async fn find<A: Into<AliasId>>(&self, alias: A) -> Option<AliasFoundLocation> {
let alias: AliasId = alias.into();
log::debug!("[AliasServiceRequester] find alias {alias}");
log::info!("[AliasServiceRequester] find alias {alias}");
let (tx, rx) = oneshot::channel();
self.tx.send(AliasControl::Find(alias, tx)).expect("alias service main channal should work");
rx.await.ok()?
let res = rx.await.ok()?;
log::info!("[AliasServiceRequester] find alias {alias} => result {res:?}");
res
}

pub async fn open_stream<A: Into<AliasId>>(&self, alias: A, over_service: P2pServiceRequester, meta: Vec<u8>) -> anyhow::Result<AliasStreamLocation> {
Expand Down Expand Up @@ -231,11 +233,14 @@ impl AliasServiceInternal {
match req.state {
FindRequestState::CheckHint(requested_at, ref mut _hash_set) => {
if requested_at + HINT_TIMEOUT_MS <= now {
log::info!("[AliasServiceInternal] check hint timeout {alias_id} => switch to scan");
self.outs.push_back(InternalOutput::Broadcast(AliasMessage::Scan(*alias_id)));
req.state = FindRequestState::Scan(now);
}
}
FindRequestState::Scan(requested_at) => {
if requested_at + SCAN_TIMEOUT_MS <= now {
log::info!("[AliasServiceInternal] find scan timeout {alias_id}");
timeout_reqs.push(*alias_id);
while let Some(tx) = req.waits.pop() {
tx.send(None).print_on_err2("");
Expand All @@ -251,6 +256,7 @@ impl AliasServiceInternal {
}

fn on_msg(&mut self, now: u64, from: PeerId, msg: AliasMessage) {
log::info!("[AliasServiceInternal] on msg from {from}, {msg:?}");
match msg {
AliasMessage::NotifySet(alias_id) => {
let slot = self.cache.get_or_insert_mut(alias_id, || HashSet::new());
Expand Down Expand Up @@ -539,6 +545,33 @@ mod test {
assert_eq!(outputs, vec![InternalOutput::Broadcast(AliasMessage::Scan(alias_id))]);
}

#[test]
fn test_find_cached_alias_timeout_switch_to_scan() {
let mut ctx = TestContext::new();
let alias_id = AliasId(1);
let peer_addr = PeerId(1);

// Add alias to cache
ctx.internal.on_msg(ctx.now, peer_addr, AliasMessage::NotifySet(alias_id));

// Create a oneshot channel for the find response
let (tx, _rx) = oneshot::channel();

// Test finding the cached alias
ctx.internal.on_control(ctx.now, AliasControl::Find(alias_id, tx));

// Verify unicast message to check with cached peer
let outputs = ctx.collect_outputs();
assert_eq!(outputs, vec![InternalOutput::Unicast(peer_addr, AliasMessage::Check(alias_id))]);

// Simulate timeout
ctx.advance_time(HINT_TIMEOUT_MS + 1);
ctx.internal.on_tick(ctx.now);

let outputs = ctx.collect_outputs();
assert_eq!(outputs, vec![InternalOutput::Broadcast(AliasMessage::Scan(alias_id))]);
}

#[test]
fn test_find_timeout() {
let mut ctx = TestContext::new();
Expand Down
Loading