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

[client]: improve batch request API #910

Merged
merged 19 commits into from
Nov 7, 2022

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Oct 20, 2022

Close #909

After some discussions with Kian about the API in #909 it simply quite annoying for users to deal with i.e, to iterate over all responses to check whether it was successful but useful in some contexts such as if you want to exactly which requests failed.

After some suggestions by James, I reworked the API with an iterator-like API.

The responses still doesn't expose the request ID mainly because jsonrpsee controls the request IDs and changing that quite a big change and doesn't work well with the current BatchRequestBuilder.

Thus, the only way to map the requests to reponses is to copy the batch request and then iterate over the BatchResponse::into_iter together to figure it which calls that failed such as:

	let mut batch_request = BatchRequestBuilder::new();
	batch_request.insert("say_hello", rpc_params![]).unwrap();
	batch_request.insert("say_goodbye", rpc_params![0_u64, 1, 2]).unwrap();
	batch_request.insert("get_swag", rpc_params![]).unwrap();

        let response = client.batch_request(batch_request.clone()).await.unwrap();

        for (request, response) in batch_request.zip(response.iter()) {
            match response {
               Ok(r) => {
                   println!("request: {:?} succeed with response: {}", request, r); 
               }
               Err(err) => {
                   println!("request: {:?} failed with response: {}", request, err); 
               }
            }
        }

Ideally, the BatchRequestBuilder would inject the request IDs and allow users to add their own IDs but because we use the ID range as digest for each batch request it would make things harder.

Thus, I think this is good enough API for now.

@niklasad1 niklasad1 changed the title [WIP]: improve client batch request API improve client batch request API Oct 21, 2022
@niklasad1 niklasad1 marked this pull request as ready for review October 21, 2022 08:21
@niklasad1 niklasad1 requested a review from a team as a code owner October 21, 2022 08:21
@niklasad1 niklasad1 changed the title improve client batch request API WIP: improve client batch request API Oct 26, 2022
@niklasad1 niklasad1 changed the title WIP: improve client batch request API [client]: improve batch request API Oct 27, 2022
benches/bench.rs Outdated Show resolved Hide resolved
benches/bench.rs Outdated Show resolved Hide resolved
core/src/client/mod.rs Outdated Show resolved Hide resolved
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.

Looks great to me, ncie one!

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!

@niklasad1 niklasad1 merged commit 824c369 into master Nov 7, 2022
@niklasad1 niklasad1 deleted the na-client-fix-parse-batch-with-err branch November 7, 2022 14:38
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.

[client]: batch request API is non-ergonomic
3 participants