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

rpc module: register_async_method only allows Copy closures #947

Closed
Code0x2 opened this issue Nov 23, 2022 · 3 comments · Fixed by #948
Closed

rpc module: register_async_method only allows Copy closures #947

Code0x2 opened this issue Nov 23, 2022 · 3 comments · Fixed by #948

Comments

@Code0x2
Copy link

Code0x2 commented Nov 23, 2022

Im creating a method that will take the values, send it into a channel, and then await its response, by using register_async_method. However, it doesnt let me utilize the mpsc::UnboundedSender because it doesnt implement copy, which is a bound on the register_async_method callback. Is there some other way I can go about sending values down a channel?

I tried register_method, but i can send that message into a channel just fine. But the problem is i want to await its response from the other end of the channel and i dont want to use a blocking recv method to do so.

@niklasad1
Copy link
Member

hey @Code0x2

Can you give an example what you are trying to do?
We have this bound on the closure that may you way run into.

I think it should work if you clone the sender and move into closure by async move but I could be wrong.

@niklasad1
Copy link
Member

niklasad1 commented Nov 23, 2022

Yeah, alright seems like we need to change the bound to Clone instead of Copy of the closure for that to work.

However, you could move that mpsc::UnboundedSender to the RpcModule or use the proc macros and then implement that trait for a type that has the mpsc::UnboundedSender as field.

Example with proc macros:

use std::net::SocketAddr;

use futures::SinkExt;
use jsonrpsee::core::{async_trait, Error};
use jsonrpsee::proc_macros::rpc;
use jsonrpsee::server::ServerBuilder;

#[rpc(server, client, namespace = "state")]
pub trait Rpc {
	/// Async method call example.
	#[method(name = "getKeys")]
	async fn say_hello(&self) -> Result<u32, Error>;
}

pub struct RpcServerImpl {
	sender: futures::channel::mpsc::UnboundedSender<()>,
}

#[async_trait]
impl RpcServer for RpcServerImpl {
	async fn say_hello(&self) -> Result<u32, Error> {
		self.sender.clone().send(()).await.unwrap();
		Ok(11)
	}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
	let _ = run_server().await?;

	loop {}
}

async fn run_server() -> anyhow::Result<SocketAddr> {
	let server = ServerBuilder::default().build("127.0.0.1:0").await?;

	let (tx, rx) = futures::channel::mpsc::unbounded();

	let addr = server.local_addr()?;
	let handle = server.start(RpcServer::into_rpc(RpcServerImpl { sender: tx }))?;

	tokio::spawn(handle.stopped());

	Ok(addr)
}

@niklasad1 niklasad1 changed the title Cant send values into channels from async method rpc module: register_async_method only allows Copy closures Nov 23, 2022
@Code0x2
Copy link
Author

Code0x2 commented Nov 23, 2022

Great this fixes the problem. Thanks for the quick fix and merge!

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 a pull request may close this issue.

2 participants