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

JSON-RPC: retrieve events via long polling #4341

Merged
merged 5 commits into from
Apr 22, 2023

Conversation

link2xt
Copy link
Collaborator

@link2xt link2xt commented Apr 20, 2023

This way is more compatible to JSON-RPC libraries
that do not support receiving notifications from the server and allows describing event types in the OpenRPC specification.

Event thread converting events to notifications in the FFI is removed, so it is now possible to construct a dc_jsonrpc_instance_t while still retrieving events via dc_event_emitter_t.

@link2xt link2xt force-pushed the link2xt/jsonrpc-event-long-polling branch 2 times, most recently from d2703c8 to 67ee039 Compare April 20, 2023 12:19
@link2xt
Copy link
Collaborator Author

link2xt commented Apr 20, 2023

With this PR merged we are retrieving events one-by-one, but this can further be improved by introducing a batched wait_next_events() call described in #4328.

@link2xt link2xt force-pushed the link2xt/jsonrpc-event-long-polling branch 2 times, most recently from fcde9ed to 9aeccb9 Compare April 20, 2023 16:07
@link2xt
Copy link
Collaborator Author

link2xt commented Apr 20, 2023

This is mostly ready except for the TypeScript part.

@Simon-Laux Maybe you have some suggestions on how the TypeScript part of it should work, otherwise I will try to do the minimum to get the tests pass but it may be far from idiomatic Asynchronous JavaScript.

@@ -165,6 +166,16 @@ impl CommandApi {
get_info()
}

/// Get the next event.
async fn get_next_event(&self) -> Result<Event> {
let event_emitter = self.accounts.read().await.get_event_emitter();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

"Event emitter" is just a channel receiver inside. This line basically clones a receiver so we don't hold any locks on the self.accounts while waiting for events, otherwise it would be impossible to get a write lock to e.g. add an account because event loop is running.

@link2xt link2xt force-pushed the link2xt/jsonrpc-event-long-polling branch 2 times, most recently from 1cf259f to 457d149 Compare April 21, 2023 00:46
@link2xt link2xt requested a review from Simon-Laux April 21, 2023 08:04
@link2xt link2xt marked this pull request as ready for review April 21, 2023 08:04
@link2xt
Copy link
Collaborator Author

link2xt commented Apr 21, 2023

Websocket example is still not fixed, but everything else is ready for review.

main_cancel.cancelled().await;
accounts.read().await.stop_io().await;
drop(accounts);
drop(state);
let (r0, r1, r2, r3) = tokio::join!(events_task, send_task, sigterm_task, recv_task);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed join!, it is not necessary since we already use spawn. Just waiting for each task in a sequence.

async def wait_for_event(self, account_id: int) -> Optional[dict]:
"""Waits for the next event from the given account and returns it."""
if account_id in self.event_queues:
return await self.event_queues[account_id].get()
return None
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was a bug I think, now creating a queue for an account if it did not have any events before.

This way is more compatible to JSON-RPC libraries
that do not support receiving notifications from the server
and allows describing event types in the OpenRPC specification.

Event thread converting events to notifications in the FFI
is removed, so it is now possible to construct a dc_jsonrpc_instance_t
while still retrieving events via dc_event_emitter_t.
@link2xt link2xt force-pushed the link2xt/jsonrpc-event-long-polling branch from 7a1ea11 to 2cd82d7 Compare April 21, 2023 12:12
@link2xt link2xt force-pushed the link2xt/jsonrpc-event-long-polling branch from 07cde3c to 4a0817d Compare April 21, 2023 13:33
Copy link
Member

@Simon-Laux Simon-Laux left a comment

Choose a reason for hiding this comment

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

lgtm and works in desktop without any adjustments

@link2xt link2xt merged commit a1f3f67 into master Apr 22, 2023
@link2xt link2xt deleted the link2xt/jsonrpc-event-long-polling branch April 22, 2023 15:34
adbenitez added a commit to deltachat/deltachat-rpc-client-go that referenced this pull request May 2, 2023
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