我正在尝试与多个gRPC服务共享我的SeaORM连接。
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = config::load();
let conn = connection::get(&config.database).await;
let addr = format!("{}:{}", config.server.host, config.server.port).parse()?;
Server::builder()
.add_service(GreeterServer::new(MyGreeter { connection: conn }))
.add_service(HealthServer::new(HealthCheck { connection: conn }))
.serve(addr)
.await?;
Ok(())
}
然而,在第二个服务(HealthCheck)之后,我得到了“移动后在此使用的值”。我如何解决这个问题?
这就是我如何将连接传递给每个服务:
use sea_orm::DatabaseConnection;
use tonic::{Request, Response, Status};
pub mod hello_world {
tonic::include_proto!("helloworld"); // The string specified here must match the proto package name
}
pub use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
#[derive(Debug, Default)]
pub struct MyGreeter {
pub connection: DatabaseConnection,
}
#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn unary(
&self,
request: Request<HelloRequest>, // Accept request of type HelloRequest
) -> Result<Response<HelloReply>, Status> {
// Return an instance of type HelloReply
println!("Got a request: {:?}", request);
let reply = hello_world::HelloReply {
message: format!("Hello {}!", request.into_inner().name).into(), // We must use .into_inner() as the fields of gRPC requests and responses are private
};
Ok(Response::new(reply)) // Send back our formatted greeting
}
}
1条答案
按热度按时间34gzjxbg1#
我认为这不是一个非常理想的使用,需要调查它如何影响连接数和线程安全。