-
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
replace WS and HTTP servers
with a server that supports both WS and HTTP
#863
Conversation
ws server support both WS and HTTP
WS and HTTP servers
with a server that supports both WS and HTTP
2d4b34b
to
4bbb81c
Compare
benches
|
server/src/transport/ws.rs
Outdated
/// | ||
/// Returns `(MethodResponse, None)` on every call that isn't a subscription | ||
/// Otherwise `(MethodResponse, Some(PendingSubscriptionCallTx)`. | ||
pub(crate) async fn execute_call<L: WsLogger>(c: Call<'_, L>) -> MethodResult { |
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.
How much similarity is there between these methods and the same in http.rs
; is it worth trying to reuse more functionality from within them or are the yactually fairly distinct?
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.
yeah those are super similar but WS
stuff has some extra stuff such as subscriptions
but I think we could rework that in a follow-up PR.
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.
Makes sense to me; definitely no rush, just something I pondered when I saw it!
47fa4b2
to
bc054f8
Compare
CORS has been removed to tower middleware and doesn't need to supported anymore
WS and HTTP servers
with a server that supports both WS and HTTP
WS and HTTP servers
with a server that supports both WS and HTTP
@@ -0,0 +1,6 @@ | |||
//! Host filtering. |
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.
Is the idea that in a followup PR we might be able to move this stuff into a tower middleware?
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.
yeah, if it will become cleaner
it's so little code now so I'm okay keeping it here as well.
// WebSocket. | ||
{ | ||
let client = WsClientBuilder::default().build(format!("ws://{}", addr)).await?; | ||
let response: String = client.request("say_hello", rpc_params![]).await?; | ||
println!("[main]: ws response: {:?}", response); | ||
let _response: Result<String, _> = client.request("unknown_method", rpc_params![]).await; | ||
let _ = client.request::<String, _>("say_hello", rpc_params![]).await?; | ||
} | ||
|
||
let client = HttpClientBuilder::default().build(&url)?; | ||
let response: String = client.request("say_hello", rpc_params![]).await?; | ||
println!("[main]: response: {:?}", response); | ||
let _response: Result<String, _> = client.request("unknown_method", rpc_params![]).await; | ||
let _: String = client.request("say_hello", rpc_params![]).await?; | ||
tokio::time::sleep(std::time::Duration::from_secs(1)).await; | ||
|
||
// HTTP. | ||
{ | ||
let client = HttpClientBuilder::default().build(format!("http://{}", addr))?; | ||
let response: String = client.request("say_hello", rpc_params![]).await?; | ||
println!("[main]: http response: {:?}", response); | ||
let _response: Result<String, _> = client.request("unknown_method", rpc_params![]).await; | ||
let _ = client.request::<String, _>("say_hello", rpc_params![]).await?; | ||
} |
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.
Nit: I wonder whether it's worth having a separate http_and_ws.rs
example to show that you can do this, since this example is, I guess, focused on middleware!
Doesn't need doing in this PR though; just a thought :)
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.
Oh actually I see what you're getting at here; middleware works for either http or ws. Let's leave it be :)
server/src/server.rs
Outdated
// NOTE: this task will finish after the HTTP request have been finished. | ||
// | ||
// Thus, if it was a Websocket Upgrade request the background task will be spawned separately. | ||
let mut stop_handle2 = stop_handle.clone(); |
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.
Nit: does it need cloning? it's not being used anywhere elseby the looks of it :)
Is the comment saying that the async
block below finishes when the http request is read and handled but that ws will lead to task being spawned?
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.
it's in a loop so the clone is needed :(
it was just a side-note to previous behavior i.e, the task would live as long as the ws connection task
was alive but now "this task" finished once the HTTP request is finished but I removed the comment.
this is because the hyper callback
just a spawns the task directly without access to FutureDriver
Looks great! Just to document; me and Niklas chatted about having a method on Asdie from that I'm good to tick this PR; it looks awesome! |
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let mut shutdown_waiter = ShutdownWaiter(self.0.clone()); | ||
/// Wait for the server to stopped.. | ||
pub async fn stopped(self) { |
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.
we could call this wait
or something not sure :)
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 like stopped
offhand just because it is consistent with stopped
and stop
!
tokio::select! { | ||
res = &mut conn => { | ||
match res { | ||
Ok(_) => tracing::info!("Accepting new connection {}/{}", curr_conns, max_conns), |
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.
shall we log the peer addr
here as well? I reckon that it might be useful
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.
That sounds like a good idea, we can always come with another PR
Wondering if we should incorporate the peer addr
or connection address
to uniquely identify which logs are coming from which connections?
It might help us group errors and warnings that have a common root cause
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.
yeah, it was one thing missing from the logs when I tried to debug these Transport(i/o error: unexpected end of file)
errors that we have seen plenty of
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.
let's do it in another PR
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.
Looks amazing; great job!
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.
LGMT! 👍 Nice job unifying ws/http!
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Closes #821 and #869