-
Notifications
You must be signed in to change notification settings - Fork 20
kad: Providers part 4: refresh local providers #235
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
Merged
Merged
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
1048320
Introduce `ADD_PROVIDER` query
dmitry-markin 2ab84ae
Merge remote-tracking branch 'origin/master' into dm-provider-queries
dmitry-markin 778078b
Execute `ADD_PROVIDER` query
dmitry-markin 323a26f
Introduce local providers in `MemoryStore`
dmitry-markin 8946809
Add provider refresh interval to Kademlia config
dmitry-markin 474a58a
Merge remote-tracking branch 'origin/master' into dm-republish-providers
dmitry-markin d02acee
Move `FuturesStream` to a separate file
dmitry-markin 5432c9b
Refresh providers: dry-run without network queries
dmitry-markin feb493d
Remove `try_get_record()` and other `try_...()` non-async methods
dmitry-markin 982c73d
Move query ID generation from `KademliaHandle` to `Kademlia`
dmitry-markin 68c7a87
Republish providers
dmitry-markin f9ed3e7
Merge remote-tracking branch 'origin/master' into dm-republish-providers
dmitry-markin 764a3f2
Merge remote-tracking branch 'origin/master' into dm-add-providers
dmitry-markin 5e97484
Use getter `TransportService::local_peer_id()` instead of accessing d…
dmitry-markin 7caf290
Use getter `TransportService::local_peer_id()` instead of accessing d…
dmitry-markin 8314d95
Merge branch 'dm-add-providers' into dm-republish-providers
dmitry-markin e895a44
Revert "Remove `try_get_record()` and other `try_...()` non-async met…
dmitry-markin ce18594
Revert "Move query ID generation from `KademliaHandle` to `Kademlia`"
dmitry-markin 0fcd621
Use `AtomicUsize` to generate `QueryId` in both `KademliaHandle` and …
dmitry-markin 57f17ce
Use `open_substream_or_dial()` to add provider records to peers
dmitry-markin 8594103
Merge remote-tracking branch 'origin/master' into dm-add-providers
dmitry-markin 4861959
Fix refresh when we are the only provider
dmitry-markin e853d3b
Merge branch 'dm-add-providers' into dm-republish-providers
dmitry-markin 9e68d8a
Merge branch 'master' into dm-republish-providers
dmitry-markin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2024 litep2p developers | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the "Software"), | ||
// to deal in the Software without restriction, including without limitation | ||
// the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
// and/or sell copies of the Software, and to permit persons to whom the | ||
// Software is furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
use futures::{stream::FuturesUnordered, Stream, StreamExt}; | ||
|
||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll, Waker}, | ||
}; | ||
|
||
/// Wrapper around [`FuturesUnordered`] that wakes a task up automatically. | ||
/// The [`Stream`] implemented by [`FuturesStream`] never terminates and can be | ||
/// polled when contains no futures. | ||
#[derive(Default)] | ||
pub struct FuturesStream<F> { | ||
futures: FuturesUnordered<F>, | ||
waker: Option<Waker>, | ||
} | ||
|
||
impl<F> FuturesStream<F> { | ||
/// Create new [`FuturesStream`]. | ||
pub fn new() -> Self { | ||
Self { | ||
futures: FuturesUnordered::new(), | ||
waker: None, | ||
} | ||
} | ||
|
||
/// Push a future for processing. | ||
pub fn push(&mut self, future: F) { | ||
self.futures.push(future); | ||
|
||
if let Some(waker) = self.waker.take() { | ||
waker.wake(); | ||
} | ||
} | ||
} | ||
|
||
impl<F: Future> Stream for FuturesStream<F> { | ||
type Item = <F as Future>::Output; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let Poll::Ready(Some(result)) = self.futures.poll_next_unpin(cx) else { | ||
// We must save the current waker to wake up the task when new futures are inserted. | ||
// | ||
// Otherwise, simply returning `Poll::Pending` here would cause the task to never be | ||
// woken up again. | ||
// | ||
// We were previously relying on some other task from the `loop tokio::select!` to | ||
// finish. | ||
self.waker = Some(cx.waker().clone()); | ||
|
||
return Poll::Pending; | ||
}; | ||
|
||
Poll::Ready(Some(result)) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.