-
Notifications
You must be signed in to change notification settings - Fork 611
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
how to use tokio::spawn in rust client? #598
Comments
Hello and thank you for opening this issue! A few questions to follow up with
Thanks! |
Thanks for your reply
client source code
use fbthrift_demo_if::TestService;
use fbthrift_tcp::TcpTransport;
use fbthrift::CompactProtocol;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let stream = tokio::net::TcpStream::connect("127.0.0.1:6060").await?;
let transport = TcpTransport::new(stream);
let conn = <dyn fbthrift_demo_if::TestService>::new(CompactProtocol, transport);
let client1 = conn.clone();
let mut tasks = vec![];
tasks.push(tokio::spawn(async move {
let result = client1.method1(10).await;
println!("result: {}", result.unwrap());
}));
let client2 = conn.clone();
tasks.push(tokio::spawn(async move {
let result = client2.method1(11).await;
println!("result: {}", result.unwrap());
}));
for task in tasks {
task.await?;
}
Ok(())
} server source code
```namespace cpp2 demo
service TestService {
i32 method1(1: i32 req);
}
- DemoHandler.h
```c++
#pragma once
#include "gen-cpp2/TestService.h"
#include <folly/logging/xlog.h>
namespace demo {
namespace cpp2 {
class ExampleHandler : public TestServiceSvIf {
public:
folly::SemiFuture<int> semifuture_method1(int req) override {
XLOG(INFO) << "server: receive " << req;
return folly::makeSemiFuture(req + 1);
}
};
} // namespace cpp2
} // namespace demo
Yes,
After I cloned the connection for both queries, it works, too. use fbthrift_demo_if::TestService;
use fbthrift_tcp::TcpTransport;
use fbthrift::CompactProtocol;
use futures::future::try_join_all;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let stream = tokio::net::TcpStream::connect("127.0.0.1:6060").await?;
let transport = TcpTransport::new(stream);
let conn = <dyn fbthrift_demo_if::TestService>::new(CompactProtocol, transport);
let response = try_join_all(vec![conn.clone().method1(10), conn.clone().method1(11)]).await;
println!("{response:?}");
Ok(())
}
If necessary, I could mail both of the client and server to you. Thank you very much. |
Can you attach an archive of what you're trying to run here? I think I'm still missing some of the logic to setup your server and verify this. The client code looks potentially complete but I don't see how you're running the thrift server here. |
sorry about that, I wrote the server with C++. |
Are you sure about this revision? Can you drop the full SHA hash here? I can't pull this revision (I'm trying to setup a repro of your problem now) |
the above one is my local commit hash, I'm sorry for bothering you due to my carelessness.
commit 0fb315837c6cc5a823f759332f11d8a4885e826f (HEAD) |
Hello again, I just wanted to let you know I'm still working on setting up a repro sample. We've hit some hiccups building the thrift compiler, but we're still progressing. It may just take some time to investigate. As a last option, can you try wrapping the original client in the tokio sample in an let stream = tokio::net::TcpStream::connect("127.0.0.1:6060").await?;
let transport = TcpTransport::new(stream);
let conn = Arc::new(<dyn fbthrift_demo_if::TestService>::new(CompactProtocol, transport));
let client = conn.clone(); |
Thanks for you effort. But still not working. # cargo run -r
Running `target/release/client`
result: 11
^C |
@slawlor AFAK, within Meta you use srserver/srclient framework, do you have any plan to open source these components? |
Think of thrift itself as generic, general-purpose, not tied into meta-specific internal implementation details. And think of SR as the opposite - it depends on the generic thrift but then also connects it to internal implementation details such as service discovery, logging, and configuration. |
I would like to concurrently send requests to fbthrift server, so I created a client with the following code:
where the
TestService::method1
is very simple, it just adds one to the received numberthe client subspended after receive the first request's result:
but if I tweak the code with use futures::future::try_join_all
it works and I got the expected result:
Myy question is what is wrong with the
tokio::spawn
version?The text was updated successfully, but these errors were encountered: