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

rpc module: refactor subscriptions to return impl IntoSubscriptionResponse #1034

Merged
merged 65 commits into from
Mar 23, 2023

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Feb 28, 2023

The goal with this PR is to simplify the subscription API from the server-side of things.

Previously the subscriptions were changed to return Result for ergonomics but the errors were never used and only logged after the latest refactoring.

One problem with the current API is that "result error bailing" is hard to get right and might cause the error not to sent the client.

This PR adds a new trait IntoSubscriptionCloseResponse which can be implemented for customized types and it is implemented for Result<(), StringError> and () by jsonrpsee

Result

Once an error occurs it's sent out a subscription error notification

    module
        .register_subscription("my_sub", "my_sub", "my_unsub", |_, pending, _| async move {
            let mut sink = pending.accept().await?;
            sink.send("foo").await?;
            Ok(())
        })
        .unwrap();

The outcome is that the proc macro APIs can't assume that the return type will be Result<(), Error> anymore instead it could be any type that implement IntoSubscriptionResponse

@niklasad1 niklasad1 force-pushed the na-subscription-api-option-result branch from 8062295 to 3140865 Compare March 1, 2023 10:31
@niklasad1 niklasad1 changed the title WIP: refactor subscription to return Option<Result<>> rpc module: refactor subscription to return Option<Result<>> Mar 1, 2023
tests/tests/rpc_module.rs Outdated Show resolved Hide resolved
This changes the subscription API again to return `Result<(), Option<SubscriptionMessage>>`
to work a little smoother with the combinators and it introduces an extention trait
to make convert it `Result<T, Error>` to `Result<T, Option<SubscriptionMessage>>`

This trait is implemented for types where it is possible and for custom types and tricky situations the
user has to implement themselves.

For instance it's not possible to implement on `TrySendError` because that depends on the use-case
i.e. whether the channel is full should be regarded as an error or not.
@niklasad1 niklasad1 changed the title rpc module: refactor subscription to return Option<Result<>> rpc module: refactor subscriptions to return Result<(), Option> Mar 3, 2023
core/src/server/mod.rs Outdated Show resolved Hide resolved
@niklasad1 niklasad1 marked this pull request as ready for review March 3, 2023 09:40
@niklasad1 niklasad1 requested a review from a team as a code owner March 3, 2023 09:40
@niklasad1 niklasad1 changed the title rpc module: refactor subscriptions to return Result<(), Option> rpc module: refactor subscriptions to return impl IntoSubscriptionResponse Mar 13, 2023
server/src/tests/ws.rs Outdated Show resolved Hide resolved
proc-macros/src/lib.rs Outdated Show resolved Hide resolved
proc-macros/src/lib.rs Outdated Show resolved Hide resolved
benches/helpers.rs Outdated Show resolved Hide resolved
core/src/server/error.rs Outdated Show resolved Hide resolved
core/src/server/error.rs Outdated Show resolved Hide resolved
proc-macros/src/helpers.rs Outdated Show resolved Hide resolved
@@ -502,3 +495,36 @@ async fn bounded_subscription_works() {
assert_eq!(item, exp);
}
}

#[tokio::test]
async fn serialize_sub_error_adds_extra_string_quotes() {
Copy link
Member Author

@niklasad1 niklasad1 Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsdw @lexnv

This is a test to illustrate that StringError will be serialized with extra string quotes such as "error":"{...}" even if the value is JSON

We could actually parse the string before adding the "<string>" but I came to the solution that is too opinionated and likely better for folks to implement their own type then.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so basically fi the string returned is actually JSON already we'll always be assumign it's a string (I think that's fair; we are expecting "standard" errors to be returned here and not errors that are actually also valid JSON strings

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that does make sense! In the end, we will be wrapping the message (may it be a string or already a valid json) with "{ msg }". I'm guessing this is not the common behavior and we don't need to provide a sort of wrapped error to ensure we don't add the extra quotes.
I do wonder, do we have in substrate subscriptions that return directly a valid json for the error? I'd expect that to be unlikely

Copy link
Member Author

@niklasad1 niklasad1 Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder, do we have in substrate subscriptions that return directly a valid json for the error? I'd expect that to be unlikely

No, substrate will likely use Option<Msg> or a custom message type because all substrate subscriptions has defined states then it doesn't make sense with Result with this new pattern.

Thus, in practice Result should only by used when one doesn't have defined messages when something fails in the subscription callback such as parsing messages and some other custom logic to be indicated to the "connected client"

Copy link
Contributor

@lexnv lexnv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Amazing work here!

Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff! :)

proc-macros/src/lib.rs Outdated Show resolved Hide resolved
proc-macros/src/lib.rs Outdated Show resolved Hide resolved
@niklasad1 niklasad1 merged commit 7144be5 into master Mar 23, 2023
@niklasad1 niklasad1 deleted the na-subscription-api-option-result branch March 23, 2023 20:05
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

Successfully merging this pull request may close these issues.

3 participants