Skip to content
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

Sending error response on exceeding buffer capacity (ws) #626

Closed
Thegaram opened this issue Apr 30, 2021 · 5 comments
Closed

Sending error response on exceeding buffer capacity (ws) #626

Thegaram opened this issue Apr 30, 2021 · 5 comments

Comments

@Thegaram
Copy link

Using the ws server, when the response size exceeds max_out_buffer_capacity simply no response is sent. Would it be possible to detect this error and send an error JSON-RPC response in this case?

@Thegaram
Copy link
Author

Actually, the server will close the connection in this case. Still, it would be nice to override this behavior and send a JSON-RPC response. Here's a simple example to trigger this:

extern crate env_logger;

use jsonrpc_ws_server::*;
use jsonrpc_ws_server::jsonrpc_core::*;

const RESPONSE_PAYLOAD_SIZE: usize = 5 * 1024 * 1024;

fn main() {
    env_logger::init();

    let mut io = IoHandler::new();

    io.add_method("small", |_params| {
        let payload = (0..10).map(|_| "x").collect::<String>();
        Ok(Value::String(payload))
    });

    io.add_method("large", |_params| {
        let payload = (0..RESPONSE_PAYLOAD_SIZE).map(|_| "x").collect::<String>();
        Ok(Value::String(payload))
    });

    let server = ServerBuilder::new(io)
        .start(&"0.0.0.0:3030".parse().unwrap())
        .expect("Server must start with no issues");

    server.wait().unwrap()
}

@niklasad1
Copy link
Member

Yeah, I think it would be possible to detect that the output buffer is exceeded and send message to large or something assuming that it was a valid JSON-RPC request and the input buffer was not exceeded (the request) in case case

@Thegaram
Copy link
Author

Thegaram commented May 1, 2021

I believe this behavior is from the parity-ws crate. I was wondering if there's any way to "catch" this error and react to it?

The other option is to estimate the response size in my handler, which is certainly possible but it involves an extra serialization step for a potentially large object so I'd prefer to avoid it if possible.

@niklasad1
Copy link
Member

I believe this behavior is from the parity-ws crate. I was wondering if there's any way to "catch" this error and react to it?

I think you are right, https://github.com/paritytech/jsonrpc/blob/master/ws/src/session.rs#L281-#L290) seems where the error is caught.

The other option is to estimate the response size in my handler, which is certainly possible but it involves an extra serialization step for a potentially large object so I'd prefer to avoid it if possible.

If the connection is dropped by the transport library it's not much jsonrpc ws server can do, perhaps #605 is an option to work around this. I think we had to a similar workaround to estimate the size of the serialized payload in another repo but it may not be 100% accurate if the JSONRPC id is not known (ID can be a string and thus arbitrary length)

@Thegaram
Copy link
Author

Thegaram commented May 6, 2021

Thank you for the information!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants