defiant
defiant

Reputation: 3341

How to run both server and client using Tonic in Rust?

I am trying to send a request to a grpc server from another grpc server. But the problem is I don't seem to get any ideas about how to access share the the data.

Lets say I have 3 apps, app1, app2, and app3 app2 and app3 are both written in rust using tonic.

  1. So app1 sends a grpc request to app2 - this is working
  2. app2 receives the request and processes the data - this is working as well
  3. app2 needs to send a grpc request to app3 - I don't know how to do this
  4. app3 receives the requests and sends back the reply - this is working.

Here is the code where I am trying to send the data:

async fn list_modules(
        &self,
        request: Request<ListModulesRequest>,
    ) -> Result<Response<ListModulesResponse>, Status> {
        println!("Got a request: {:?}", request);
        let request_data = request.into_inner();
        let request = tonic::Request::new(DalListModulesRequest {
            request_id: Uuid::new_v4().to_string(),
            app_id: APP_ID.to_string(),
        });
        let mut dal_client = ModulesDalManager::connect("http://[::1]:50052").await?;
        let mut response = dal_client.list_modules(request).await?;
        let reply = ListModulesResponse {
            request_id: response.request_id,
            modules: response.modules,
        };
        Ok(Response::new(reply))
    }

Here I guess I shouldn't be creating the dal_client as below as it is giving me errors. But I don't understand where to create it. let mut dal_client = ModulesDalManager::connect("http://[::1]:50052").await?;

Can someone please give me any ideas on how to proceed?

Here is the error I get:

error[E0599]: no function or associated item named `connect` found for trait object `(dyn ModulesDalManager + 'static)` in the current scope
  --> src/bal/services/module_manager.rs:48:49
   |
48 |         let mut dal_client = ModulesDalManager::connect("http://[::1]:50052").await?;
   |                                                 ^^^^^^^ function or associated item not found in `(dyn ModulesDalManager + 'static)`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following traits define an item `connect`, perhaps you need to implement one of them:
           candidate #1: `hyper::client::connect::sealed::Connect`
           candidate #2: `hyper::client::connect::sealed::ConnectSvc`

Upvotes: 1

Views: 1083

Answers (1)

defiant
defiant

Reputation: 3341

ModulesDalManager didn't have connect. I should have used ModulesDalManagerClient instead. So the line becomes

let mut dal_client = ModulesDalManager::connect("http://[::1]:50052").await?;

PS: jbg from rust unofficial IRC channel helped me in figuring out the issue.

Upvotes: 1

Related Questions