grpc / grpc/grpc-rust

How to start another task along with tonic server

Open
#1,551 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
12.5k
Forks
1.3k
Avg merge
4d 7h
Merged PRs (30d)
24

Description

Hello, I'm a newbie of rust. I'm using tonic library to start a grpc server (the grpc server receives requests and then push the request data to a queue) and before starting the server, I want to start another task to consume the queue,it is an infinite loop.
```
use controller::controller_server::ControllerServer;
use std::sync::Arc;
use tokio::sync::Mutex;
use tonic::transport::Server;

pub mod codec;
pub mod controller;
pub mod error;
pub mod executor;
pub mod mq;
pub mod translate;
pub mod utils;

mod controller_proto {
include!("controller.rs");

pub(crate) const FILE_DESCRIPTOR_SET: &[u8] =
tonic::include_file_descriptor_set!("controller_descriptor");
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();

let cfg = executor::ExecutorConfig::from_env()?;

let addr = format!("0.0.0.0:{}", cfg.port);
let mut executor = executor::Executor::from_config(cfg)?;

let reflection_service = tonic_reflection::server::Builder::configure()
.register_encoded_file_descriptor_set(controller_proto::FILE_DESCRIPTOR_SET)
.build()
.unwrap();

tokio::spawn(executor.start());

log::info!("executor listen on: {}", addr);
Server::builder()
.add_service(ControllerServer::new(executor))
.add_service(reflection_service)
.serve(addr.parse()?)
.await?;

Ok(())
}

```
But the code did not compiled, the compiler says: "cannot move out of `executor` because it is borrowed"
what should I do ?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.