-
Notifications
You must be signed in to change notification settings - Fork 177
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
ws server: respect max limit for received messages #537
Merged
Merged
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5f33afd
ws server: don't kill connection max limit exceeds
niklasad1 dedcdd8
Update ws-server/src/server.rs
niklasad1 88537ba
actually use max size in soketto
niklasad1 41112c6
Merge branch 'na-ws-dont-terminate-conn-exceed-max-limit' of github.c…
niklasad1 9e4a3cf
rewrite me
niklasad1 134adc7
improve logs
niklasad1 928f7e6
use soketto fix
niklasad1 8714518
Merge remote-tracking branch 'origin/master' into na-ws-dont-terminat…
niklasad1 f91b15f
rewrite me
niklasad1 eeb74ae
fix nit
niklasad1 4feaea2
revert unintentional change
niklasad1 dbfc139
use soketto 0.7.1
niklasad1 46ef75d
fix logger
niklasad1 f051909
Update ws-server/src/server.rs
niklasad1 ca21981
Update ws-server/src/server.rs
niklasad1 770bc1b
Update ws-server/src/server.rs
niklasad1 5e14dee
Update ws-server/src/server.rs
niklasad1 5ae38ff
Update ws-server/src/server.rs
niklasad1 d1326a0
Merge remote-tracking branch 'origin/master' into na-ws-dont-terminat…
niklasad1 8a3642a
Merge remote-tracking branch 'origin/na-ws-dont-terminate-conn-exceed…
niklasad1 9ebbc6d
fix build
niklasad1 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
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
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
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 |
---|---|---|
|
@@ -39,6 +39,7 @@ use futures_channel::mpsc; | |
use futures_util::future::FutureExt; | ||
use futures_util::io::{BufReader, BufWriter}; | ||
use futures_util::stream::{self, StreamExt}; | ||
use soketto::connection::Error as SokettoError; | ||
use soketto::handshake::{server::Response, Server as SokettoServer}; | ||
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs}; | ||
use tokio_util::compat::{Compat, TokioAsyncReadCompatExt}; | ||
|
@@ -195,6 +196,7 @@ async fn handshake(socket: tokio::net::TcpStream, mode: HandshakeResponse<'_>) - | |
Ok(()) | ||
} | ||
HandshakeResponse::Accept { conn_id, methods, resources, cfg, stop_monitor } => { | ||
tracing::debug!("Accepting new connection: {}", conn_id); | ||
let key = { | ||
let req = server.receive_request().await?; | ||
let host_check = cfg.allowed_hosts.verify("Host", Some(req.headers().host)); | ||
|
@@ -243,7 +245,9 @@ async fn background_task( | |
stop_server: StopMonitor, | ||
) -> Result<(), Error> { | ||
// And we can finally transition to a websocket background_task. | ||
let (mut sender, mut receiver) = server.into_builder().finish(); | ||
let mut builder = server.into_builder(); | ||
builder.set_max_message_size(max_request_body_size as usize); | ||
niklasad1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let (mut sender, mut receiver) = builder.finish(); | ||
let (tx, mut rx) = mpsc::unbounded::<String>(); | ||
let stop_server2 = stop_server.clone(); | ||
|
||
|
@@ -252,8 +256,10 @@ async fn background_task( | |
while !stop_server2.shutdown_requested() { | ||
match rx.next().await { | ||
Some(response) => { | ||
tracing::debug!("send: {}", response); | ||
let _ = sender.send_text(response).await; | ||
// TODO: check length of response https://github.com/paritytech/jsonrpsee/issues/536 | ||
tracing::debug!("send {} bytes", response.len()); | ||
tracing::trace!("send: {}", response); | ||
let _ = sender.send_text_owned(response).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. 👍 for skipping a clone :) |
||
let _ = sender.flush().await; | ||
} | ||
None => break, | ||
|
@@ -272,22 +278,38 @@ async fn background_task( | |
while !stop_server.shutdown_requested() { | ||
data.clear(); | ||
|
||
if let Err(e) = method_executors.select_with(receiver.receive_data(&mut data)).await { | ||
tracing::error!("Could not receive WS data: {:?}; closing connection", e); | ||
tx.close_channel(); | ||
return Err(e.into()); | ||
} | ||
if let Err(err) = method_executors.select_with(receiver.receive_data(&mut data)).await { | ||
match err { | ||
SokettoError::Closed => { | ||
tracing::debug!("Remote peer terminated the connection: {}", conn_id); | ||
tx.close_channel(); | ||
return Ok(()); | ||
} | ||
SokettoError::MessageTooLarge { current, maximum } => { | ||
tracing::warn!( | ||
"WS transport error: message is too big error ({} bytes, max is {})", | ||
current, | ||
maximum | ||
); | ||
send_error(Id::Null, &tx, ErrorCode::OversizedRequest.into()); | ||
continue; | ||
} | ||
// These errors can not be gracefully handled, so just log them and terminate the connection. | ||
err => { | ||
tracing::error!("WS transport error: {:?} => terminate connection {}", err, conn_id); | ||
niklasad1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tx.close_channel(); | ||
return Err(err.into()); | ||
} | ||
}; | ||
}; | ||
|
||
if data.len() > max_request_body_size as usize { | ||
tracing::warn!("Request is too big ({} bytes, max is {})", data.len(), max_request_body_size); | ||
send_error(Id::Null, &tx, ErrorCode::OversizedRequest.into()); | ||
continue; | ||
} | ||
tracing::debug!("recv {} bytes", data.len()); | ||
|
||
match data.get(0) { | ||
Some(b'{') => { | ||
if let Ok(req) = serde_json::from_slice::<Request>(&data) { | ||
tracing::debug!("recv: {:?}", req); | ||
tracing::debug!("recv call={}", req.method); | ||
niklasad1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tracing::trace!("recv: {:?}", req); | ||
niklasad1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if let Some(fut) = methods.execute_with_resources(&tx, req, conn_id, &resources) { | ||
method_executors.add(fut); | ||
} | ||
|
@@ -309,6 +331,8 @@ async fn background_task( | |
// complete batch response back to the client over `tx`. | ||
let (tx_batch, mut rx_batch) = mpsc::unbounded(); | ||
if let Ok(batch) = serde_json::from_slice::<Vec<Request>>(&d) { | ||
tracing::debug!("recv batch={}", batch.len()); | ||
niklasad1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tracing::trace!("recv: {:?}", batch); | ||
niklasad1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if !batch.is_empty() { | ||
let methods_stream = | ||
stream::iter(batch.into_iter().filter_map(|req| { | ||
|
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
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.
use
RUST_LOG env
variable