-
Notifications
You must be signed in to change notification settings - Fork 49
P2P: Change ClientPool
to PeerSet
#337
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 all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
533e59b
add WeakClient
Boog900 b5dd497
todo
Boog900 b92e62d
client pool -> peer set
Boog900 59e346a
more peer set changes
Boog900 9ddf88d
Merge branch 'main' into peer-set-2
Boog900 d827bf5
fix cuprated builds
Boog900 326d44f
add docs
Boog900 f73071e
more docs + better disconnect handling
Boog900 252bbea
Merge branch 'main' into peer-set-2
Boog900 692d4b2
more docs
Boog900 ee362e3
fix imports
Boog900 4f14bc8
review fixes
Boog900 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,114 @@ | ||
use std::task::{ready, Context, Poll}; | ||
|
||
use futures::channel::oneshot; | ||
use tokio::sync::{mpsc, OwnedSemaphorePermit}; | ||
use tokio_util::sync::PollSemaphore; | ||
use tower::Service; | ||
|
||
use cuprate_helper::asynch::InfallibleOneshotReceiver; | ||
|
||
use crate::{ | ||
client::{connection, PeerInformation}, | ||
NetworkZone, PeerError, PeerRequest, PeerResponse, SharedError, | ||
}; | ||
|
||
/// A weak handle to a [`Client`](super::Client). | ||
/// | ||
/// When this is dropped the peer will not be disconnected. | ||
pub struct WeakClient<N: NetworkZone> { | ||
/// Information on the connected peer. | ||
pub info: PeerInformation<N::Addr>, | ||
|
||
/// The channel to the [`Connection`](connection::Connection) task. | ||
pub(super) connection_tx: mpsc::WeakSender<connection::ConnectionTaskRequest>, | ||
|
||
/// The semaphore that limits the requests sent to the peer. | ||
pub(super) semaphore: PollSemaphore, | ||
/// A permit for the semaphore, will be [`Some`] after `poll_ready` returns ready. | ||
pub(super) permit: Option<OwnedSemaphorePermit>, | ||
|
||
/// The error slot shared between the [`Client`] and [`Connection`](connection::Connection). | ||
pub(super) error: SharedError<PeerError>, | ||
} | ||
|
||
impl<N: NetworkZone> WeakClient<N> { | ||
/// Internal function to set an error on the [`SharedError`]. | ||
fn set_err(&self, err: PeerError) -> tower::BoxError { | ||
let err_str = err.to_string(); | ||
match self.error.try_insert_err(err) { | ||
Ok(()) => err_str, | ||
Err(e) => e.to_string(), | ||
} | ||
.into() | ||
} | ||
} | ||
|
||
impl<Z: NetworkZone> Service<PeerRequest> for WeakClient<Z> { | ||
type Response = PeerResponse; | ||
type Error = tower::BoxError; | ||
type Future = InfallibleOneshotReceiver<Result<Self::Response, Self::Error>>; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
if let Some(err) = self.error.try_get_err() { | ||
return Poll::Ready(Err(err.to_string().into())); | ||
} | ||
|
||
if self.connection_tx.strong_count() == 0 { | ||
let err = self.set_err(PeerError::ClientChannelClosed); | ||
return Poll::Ready(Err(err)); | ||
} | ||
|
||
if self.permit.is_some() { | ||
return Poll::Ready(Ok(())); | ||
} | ||
|
||
let permit = ready!(self.semaphore.poll_acquire(cx)) | ||
.expect("Client semaphore should not be closed!"); | ||
|
||
self.permit = Some(permit); | ||
|
||
Poll::Ready(Ok(())) | ||
} | ||
|
||
#[expect(clippy::significant_drop_tightening)] | ||
fn call(&mut self, request: PeerRequest) -> Self::Future { | ||
let permit = self | ||
.permit | ||
.take() | ||
.expect("poll_ready did not return ready before call to call"); | ||
|
||
let (tx, rx) = oneshot::channel(); | ||
let req = connection::ConnectionTaskRequest { | ||
response_channel: tx, | ||
request, | ||
permit: Some(permit), | ||
}; | ||
|
||
match self.connection_tx.upgrade() { | ||
None => { | ||
self.set_err(PeerError::ClientChannelClosed); | ||
|
||
let resp = Err(PeerError::ClientChannelClosed.into()); | ||
drop(req.response_channel.send(resp)); | ||
} | ||
Some(sender) => { | ||
if let Err(e) = sender.try_send(req) { | ||
// The connection task could have closed between a call to `poll_ready` and the call to | ||
// `call`, which means if we don't handle the error here the receiver would panic. | ||
use mpsc::error::TrySendError; | ||
|
||
match e { | ||
TrySendError::Closed(req) | TrySendError::Full(req) => { | ||
self.set_err(PeerError::ClientChannelClosed); | ||
|
||
let resp = Err(PeerError::ClientChannelClosed.into()); | ||
drop(req.response_channel.send(resp)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
rx.into() | ||
} | ||
} |
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.
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.