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

Module API refactor #412

Merged
merged 8 commits into from
Jul 12, 2021
Merged

Module API refactor #412

merged 8 commits into from
Jul 12, 2021

Conversation

maciejhirsz
Copy link
Contributor

Since we want to support both HTTP and WS servers sharing the same Methods internally, it makes more sense to force the user to pre-merge all their modules into one before cloning it and passing it to their respective servers. This PR changes some of the module API to make this the expected behavior.

  • Removed register_module on both WS and HTTP server, instead when starting a server you can pass a single Into<Methods> type.
  • RpcModule<T> implements Deref<Target = Methods> and DerefMut to keep module merging logic DRY.
  • Creating modules via proc macro should be infallible (can be checked statically) so into_rpc need not return a Result.
  • method_names returns an Iterator instead of a Vec.

Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

Still a draft but here are my thought so far:

  • I think that in the back of our heads we had in mind a setup flow where a ref to the Server would be passed around to different subsystems and each would register_module() to add "their" RPC calls. Instead what substrate does is the opposite, the subsystems are all asked for their sets of RPC calls and when they're all collected they are added to the server in a single place. (This architecture is what is making initialization tricky to do in more than one place, such as adding specific RPC modules on the basis of config files that are processed in different places. This is hackfix-solved by passing around closures.)
  • This work here makes it more obvious that setting up the server and the methods to call are quite separate things. I think it makes sense (and the DRYing is welcome obviously).
  • A UI-test for a bad macro setup would be nice, one proving that the static method check works as expected.
  • I might be wrong but I'm not sure we have good tests for setting up and starting both a ws and http server; would be good to have one.

http-server/src/server.rs Outdated Show resolved Hide resolved
@maciejhirsz maciejhirsz marked this pull request as ready for review July 12, 2021 18:23
Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

I like it. Minor grumbles.

proc-macros/src/new/render_server.rs Outdated Show resolved Hide resolved
proc-macros/src/new/render_server.rs Outdated Show resolved Hide resolved
#[method(name = "foo")]
async fn foo(&self) -> u8;

#[method(name = "foo")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, but this is the easy case. Let's do two separate modules?

Copy link
Contributor Author

@maciejhirsz maciejhirsz Jul 12, 2021

Choose a reason for hiding this comment

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

Separate modules need to be merged on runtime, and merge can return regular errors, there is no way to check them on compile time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good pt!

Co-authored-by: David <dvdplm@gmail.com>
@dvdplm dvdplm merged commit 8db65b4 into master Jul 12, 2021
@dvdplm dvdplm deleted the mh-start-with-module branch July 12, 2021 18:58
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.

2 participants