Page MenuHomePhabricator

[keyserver] Adding Tunnelbroker-client napi-rs publish class method
ClosedPublic

Authored by max on Jan 30 2023, 2:41 AM.
Tags
None
Referenced Files
Unknown Object (File)
Fri, Apr 5, 2:42 PM
Unknown Object (File)
Fri, Apr 5, 2:34 PM
Unknown Object (File)
Mar 15 2024, 9:51 AM
Unknown Object (File)
Mar 15 2024, 9:51 AM
Unknown Object (File)
Mar 14 2024, 5:22 AM
Unknown Object (File)
Mar 12 2024, 11:40 PM
Unknown Object (File)
Mar 12 2024, 11:40 PM
Unknown Object (File)
Mar 12 2024, 11:40 PM

Details

Summary

This diff introduces a publish class method for the napi-rs bridge Tunnelbroker client class.
It uses a client-shared function from the D6284 to push a message to the mspc which is bound to the gRPC tx stream.

Linear task: ENG-2729

Test Plan
  1. Keyserver service is successfully built.
  2. Manually testing:
    • Start the Tunnelbroker server;
    • Create a TunnelbrokerClient in a node js codebase by passing the address and a callback function.
    • Call publish with the corresponding payload and device_id.
    • The expected result is that the message will be delivered to the stream with the corresponding device_id.

The concept was tested using the echo bidi stream and simple client in the testing repo.

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

max held this revision as a draft.
max edited the test plan for this revision. (Show Details)
max added reviewers: varun, jon.
max added inline comments.
keyserver/addons/rust-node-addon/src/tunnelbroker_client.rs
63 ↗(On Diff #21558)

Unfortunately, NAPI-RS async functions in a class can be marked only as unsafe.

max published this revision for review.Jan 30 2023, 3:12 AM
max edited the test plan for this revision. (Show Details)

Please ignore ESLint fails, for now, because the failure seems doesn't relate to the keyserver (it's successfully built).

Any idea why the CI failures are occurring then?

Any idea why the CI failures are occurring then?

This is a protoc issue that we had before. I think it should be ok after D6411 will be re-landed.

Can you rebase your diffs on that one to address the CI failures?

jon requested changes to this revision.Jan 31 2023, 9:08 AM
jon added inline comments.
keyserver/addons/rust-node-addon/src/tunnelbroker_client.rs
63–71 ↗(On Diff #21558)

I would move the unsafe to just the block which needs it.

Also avoids having to propagate the unsafe keyword to a call elsewhere

This revision now requires changes to proceed.Jan 31 2023, 9:08 AM

Updating on parent D6284 changes.

max added inline comments.
keyserver/addons/rust-node-addon/src/tunnelbroker_client.rs
63–71 ↗(On Diff #21558)

I would move the unsafe to just the block which needs it.

Unfortunately, the function itself should be marked as unsafe. When using your proposal:

&mut self in async napi methods should be marked as unsafe

Also avoids having to propagate the unsafe keyword to a call elsewhere

In our case, we must have a long-lived stream connection which is part of the class, and use the tx channel from the publish function. In this case, we should use this channel as a mut (actually we are not mutating anything, we are writing to the mpsc channel, but it's marked as mut).
I can't see any other options on how to make this without unsafe here, this is a napi-rs limitation for using the shared structures.

jon added inline comments.
keyserver/addons/rust-node-addon/src/tunnelbroker_client.rs
63–71 ↗(On Diff #21558)

I can't see any other options on how to make this without unsafe here, this is a napi-rs limitation for using the shared structures.

unsafe best practices is bit outside of my comfort zone, so I'll take your word that this seems to be best current path forward.

This revision is now accepted and ready to land.Feb 2 2023, 10:19 AM

Would be good to get multiple peoples' acceptance on unsafe code

This revision now requires review to proceed.Feb 2 2023, 11:44 AM
varun requested changes to this revision.Feb 2 2023, 12:35 PM

I see an async class method in the docs: https://napi.rs/docs/concepts/class#class-method

Are we sure we need unsafe here?

This revision now requires changes to proceed.Feb 2 2023, 12:35 PM
In D6460#195513, @varun wrote:

I see an async class method in the docs: https://napi.rs/docs/concepts/class#class-method

Are we sure we need unsafe here?

Unfortunately yes, because we are using mut &self here to access the stream in the class instance:
&mut self in async napi methods should be marked as unsafe.

In our case, we must have a long-lived stream connection which is part of the class, and use the tx channel from the publish function. In this case, we should use this channel as a mut (actually we are not mutating anything, we are writing to the mpsc channel, but it's marked as mut).
I can't see any other options on how to make this without unsafe here, this is a napi-rs limitation for using the shared structures.

Rebasing on parent changes.

varun requested changes to this revision.Feb 3 2023, 12:07 PM

I tried building your changes locally but I got the following error: error: &mut self is incompatible with async napi methods

The Sender (tx) can be cloned cheaply, so I was wondering if we could just clone to avoid having &mut self in the function signature

This revision now requires changes to proceed.Feb 3 2023, 12:07 PM
In D6460#195963, @varun wrote:

I tried building your changes locally but I got the following error: error: &mut self is incompatible with async napi methods

Did you rebase on the latest diff changes? I've successfully built using arc patch D6460 and then nix develop --accept-flake-config -c bash -c "cargo b".

The Sender (tx) can be cloned cheaply, so I was wondering if we could just clone to avoid having &mut self in the function signature

Despite that documentation saying that tx should be mut, I've did the trick and removed the mut from the parameter and it works without any errors and without unsafe. It's tested on the testing repo without unsafe and mut messages are successfully delivered, no any compiler errors, warnings.
We can now just remove mut and unsafe as well.
I've updated the diff to be without mut, unsafe, and cloning now, which is much much better. Thanks, @varun for a deep review!

Resigning not to block you while I'm off. Glad to hear it's working without unsafe!

Resigning not to block you while I'm off. Glad to hear it's working without unsafe!

Thank you @bartek! Have a good weekend!

awesome! nice job max

would you mind explaining how you avoided the mut requirement?

This revision is now accepted and ready to land.Feb 6 2023, 11:25 AM
In D6460#197482, @varun wrote:

awesome! nice job max

would you mind explaining how you avoided the mut requirement?

I've dug into the napi Rust implementation and found that despite the docs saying that in this case I can try to omit mut+unsafe and it will work, and it's worked )

Reopening this diff due to solving ENG-3048.

This revision is now accepted and ready to land.Feb 21 2023, 6:06 AM