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

feat: RpcModule::from_arc #1324

Merged
merged 2 commits into from Mar 16, 2024
Merged

feat: RpcModule::from_arc #1324

merged 2 commits into from Mar 16, 2024

Conversation

aatifsyed
Copy link
Contributor

I want to share state between my RpcModule and another service.
Because RpcModule only Derefs to Context, I have to have a second Arc, and my handlers have to accept Arc<Arc<Context>>.

This would allow me to clone the inner context, obviating the need for the double indirection.

Alternative approaches:

  • impl Deref<Target = Arc<Context>>. I think this is backwards-compatible.
  • RpcModule::new(_: impl Into<Arc<Context>>), which is backwards-compatible.
    I could clone the arc before passing it to the ctor, where it would stay without a new Arc

@aatifsyed aatifsyed requested a review from a team as a code owner March 15, 2024 16:25
Copy link
Member

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

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

hmm I may be slow but I suppose you want to something like:

let shared_state = shared_state();
let rpc1 = RpcModule::new(shared_state);
let shared_state = rpc1.context().clone();
let rpc2 = RpcModule::new(shared_state);

If that's the case I would prefer to add another API RpcModule::from_arc instead

let shared_state = Arc::new(shared_state());
let rpc1 = RpcModule::from_arc(shared_state.clone());
let rpc2 = RpcModule::from_arc(shared_state);

@aatifsyed aatifsyed changed the title feat: RpcModule::context -> &Arc<Context> feat: RpcModule::from_arc Mar 15, 2024
@aatifsyed
Copy link
Contributor Author

I've implemented your suggestion :)

Copy link
Member

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

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

Thanks, looks good to me

@niklasad1 niklasad1 merged commit 24d9da6 into paritytech:master Mar 16, 2024
10 checks passed
niklasad1 pushed a commit that referenced this pull request Mar 20, 2024
* feat: RpcModule::context

* feat: RpcModule::from_arc
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.

None yet

2 participants