-
Notifications
You must be signed in to change notification settings - Fork 173
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(server): remove MethodSinkPermit
to fix backpressure issue on concurrent subscriptions
#1126
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
|
@@ -12,9 +12,7 @@ use hyper::upgrade::Upgraded; | |
use jsonrpsee_core::server::helpers::{ | ||
batch_response_error, prepare_error, BatchResponseBuilder, MethodResponse, MethodSink, | ||
}; | ||
use jsonrpsee_core::server::{ | ||
BoundedSubscriptions, CallOrSubscription, MethodCallback, MethodSinkPermit, Methods, SubscriptionState, | ||
}; | ||
use jsonrpsee_core::server::{BoundedSubscriptions, CallOrSubscription, MethodCallback, Methods, SubscriptionState}; | ||
use jsonrpsee_core::tracing::{rx_log_from_json, tx_log_from_str}; | ||
use jsonrpsee_core::traits::IdProvider; | ||
use jsonrpsee_core::{Error, JsonRawValue}; | ||
|
@@ -263,14 +261,19 @@ pub(crate) async fn background_task<L: Logger>(sender: Sender, mut receiver: Rec | |
let result = loop { | ||
data.clear(); | ||
|
||
let sink_permit = match wait_for_permit(&sink, stopped).await { | ||
Some((permit, stop)) => { | ||
stopped = stop; | ||
permit | ||
} | ||
None => break Ok(Shutdown::ConnectionClosed), | ||
// This is a guard to ensure that the underlying socket is only read if there is space in | ||
// the buffer for messages to be sent back to them. | ||
// | ||
// Thus, this check enforces that if the client can't keep up with receiving messages, | ||
// then no new messages will be read from them. | ||
// | ||
// TCP retransmission mechanism will take of the rest and adjust the window size accordingly. | ||
let Some(stop) = wait_until_connection_buffer_has_capacity(&sink, stopped).await else { | ||
break Ok(Shutdown::ConnectionClosed) | ||
}; | ||
|
||
stopped = stop; | ||
|
||
match try_recv(&mut receiver, &mut data, stopped).await { | ||
Receive::Shutdown => break Ok(Shutdown::Stopped), | ||
Receive::Ok(stop) => { | ||
|
@@ -290,7 +293,9 @@ pub(crate) async fn background_task<L: Logger>(sender: Sender, mut receiver: Rec | |
current, | ||
maximum | ||
); | ||
sink_permit.send_error(Id::Null, reject_too_big_request(max_request_body_size)); | ||
if sink.send_error(Id::Null, reject_too_big_request(max_request_body_size)).await.is_err() { | ||
break Ok(Shutdown::ConnectionClosed); | ||
} | ||
|
||
continue; | ||
} | ||
|
@@ -310,7 +315,6 @@ pub(crate) async fn background_task<L: Logger>(sender: Sender, mut receiver: Rec | |
max_log_length, | ||
max_response_body_size, | ||
sink: sink.clone(), | ||
sink_permit, | ||
id_provider: id_provider.clone(), | ||
logger: logger.clone(), | ||
data: std::mem::take(&mut data), | ||
|
@@ -403,21 +407,18 @@ enum Receive<S> { | |
Ok(S), | ||
} | ||
|
||
// Wait until there is a slot in the bounded channel. | ||
// | ||
// This will force the client to read socket on the other side | ||
// otherwise the socket will not be read again. | ||
// Wait until there is capacity in connection buffer to send one message. | ||
// | ||
// Fails if the server was stopped. | ||
async fn wait_for_permit<S>(sink: &MethodSink, stopped: S) -> Option<(MethodSinkPermit, S)> | ||
async fn wait_until_connection_buffer_has_capacity<S>(sink: &MethodSink, stopped: S) -> Option<S> | ||
where | ||
S: Future<Output = ()> + Unpin, | ||
{ | ||
let reserve = sink.reserve(); | ||
let reserve = sink.has_capacity(); | ||
tokio::pin!(reserve); | ||
|
||
match futures_util::future::select(reserve, stopped).await { | ||
Either::Left((Ok(sink), s)) => Some((sink, s)), | ||
Either::Left((Ok(_), s)) => Some(s), | ||
_ => None, | ||
} | ||
} | ||
|
@@ -461,7 +462,6 @@ struct ExecuteCallParams<L: Logger> { | |
max_response_body_size: u32, | ||
max_log_length: u32, | ||
sink: MethodSink, | ||
sink_permit: MethodSinkPermit, | ||
logger: L, | ||
} | ||
|
||
|
@@ -471,7 +471,6 @@ async fn execute_unchecked_call<L: Logger>(params: ExecuteCallParams<L>) { | |
conn_id, | ||
data, | ||
sink, | ||
sink_permit, | ||
max_response_body_size, | ||
max_log_length, | ||
methods, | ||
|
@@ -505,7 +504,7 @@ async fn execute_unchecked_call<L: Logger>(params: ExecuteCallParams<L>) { | |
|
||
CallOrSubscription::Call(r) => { | ||
logger.on_response(&r.result, request_start, TransportProtocol::WebSocket); | ||
sink_permit.send_raw(r.result); | ||
_ = sink.send(r.result).await; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will only fail if the connection is closed. |
||
} | ||
} | ||
} | ||
|
@@ -518,7 +517,7 @@ async fn execute_unchecked_call<L: Logger>(params: ExecuteCallParams<L>) { | |
ErrorObject::borrowed(BATCHES_NOT_SUPPORTED_CODE, &BATCHES_NOT_SUPPORTED_MSG, None), | ||
); | ||
logger.on_response(&response.result, request_start, TransportProtocol::WebSocket); | ||
sink_permit.send_raw(response.result); | ||
_ = sink.send(response.result).await; | ||
return; | ||
} | ||
BatchRequestConfig::Limit(limit) => limit as usize, | ||
|
@@ -542,11 +541,11 @@ async fn execute_unchecked_call<L: Logger>(params: ExecuteCallParams<L>) { | |
if let Some(response) = response { | ||
tx_log_from_str(&response, max_log_length); | ||
logger.on_response(&response, request_start, TransportProtocol::WebSocket); | ||
sink_permit.send_raw(response); | ||
_ = sink.send(response).await; | ||
} | ||
} | ||
_ => { | ||
sink_permit.send_error(Id::Null, ErrorCode::ParseError.into()); | ||
_ = sink.send_error(Id::Null, ErrorCode::ParseError.into()).await; | ||
} | ||
}; | ||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand the point of acquiring the permis above now? Might it be safer to not try to reserve a slot at all, and just to
.await
until there is an opening each time we want to send an actual message?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without "the reserve" the server will read the underlying socket and then the client is not "forced" to read its end of the socket to send new messages.
That's is the entire reason of reserve here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise, it will not propagated all the way down to the TCP level when the window size is adjusted etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get your concern, this permit could interfere with the stuff trying to send, so we could do:
but feels a bit annoying to waste cycles doing busy looping :)