You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing middleware to load application state into a grpc handler, and I cannot get my app to serve with the middleware layer
I've double-checked the types and looked at many examples and I can't tell what I've gotten wrong in my implementation, nor can I really make sense of the "help" message in the error.
Any help you can give me will be deeply appreciated!
Full error on compilation:
error[E0277]: the trait bound `RegistryApplicationStateMiddleware<Routes>: Service<http::request::Request<tonic::transport::Body>>` is not satisfied
--> registry/src/main.rs:39:6
|
39 | .serve(addr)
| ^^^^^ the trait `Service<http::request::Request<tonic::transport::Body>>` is not implemented for`RegistryApplicationStateMiddleware<Routes>`|
= help: the trait `Service<http::Request<tonic::transport::Body>>` is implemented for`RegistryApplicationStateMiddleware<S>`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `registry` (bin "registry") due to 1 previous error
Note the help message indicates the trait is implemented for a generic version of the middleware?? but is somehow not matching Routes...
middleware.rs
use std::{convert::Infallible, task::{Context,Poll}};use data_access::db::ConnectionPool;use http::Response;use tonic::{body::BoxBody, server::NamedService};use tower_service::Service;#[derive(Debug,Clone,Default)]pubstructRegistryApplicationState{pubpool:ConnectionPool,}#[derive(Debug,Clone,Default)]pubstructRegistryApplicationStateLayer{pubpool:ConnectionPool,}impl<S> tower_layer::Layer<S>forRegistryApplicationStateLayer{typeService = RegistryApplicationStateMiddleware<S>;fnlayer(&self,service:S) -> Self::Service{RegistryApplicationStateMiddleware{inner: service,pool:self.pool.clone(),}}}#[derive(Debug,Clone)]pubstructRegistryApplicationStateMiddleware<S>{inner:S,pubpool:ConnectionPool,}impl<S>Service<http::request::Request<tonic::transport::Body>>forRegistryApplicationStateMiddleware<S>whereS:Service<http::request::Request<tonic::transport::Body>,Response = Response<BoxBody>,Error = Infallible>
+ NamedService
+ Clone
+ Send + 'static,S::Future:Send + 'static,S::Error:Into<Box<dyn std::error::Error + Send + Sync>> + Send,{typeResponse = S::Response;typeError = S::Error;typeFuture = futures::future::BoxFuture<'static,Result<Self::Response,Self::Error>>;fnpoll_ready(&mutself,cx:&mutContext<'_>) -> Poll<Result<(),Self::Error>>{self.inner.poll_ready(cx)}fncall(&mutself,mutreq: http::request::Request<tonic::transport::Body>) -> Self::Future{// This is necessary because tonic internally uses `tower::buffer::Buffer`.// See https://github.com/tower-rs/tower/issues/547#issuecomment-767629149// for details on why this is necessarylet clone = self.inner.clone();letmut inner = std::mem::replace(&mutself.inner, clone);
req.extensions_mut().insert(RegistryApplicationState{pool:self.pool.clone(),});Box::pin(asyncmove{// Do extra async work here...let response = inner.call(req).await?;Ok(response)})}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm writing middleware to load application state into a grpc handler, and I cannot get my app to serve with the middleware layer
I've double-checked the types and looked at many examples and I can't tell what I've gotten wrong in my implementation, nor can I really make sense of the "help" message in the error.
Any help you can give me will be deeply appreciated!
Full error on compilation:
Note the help message indicates the trait is implemented for a generic version of the middleware?? but is somehow not matching
Routes
...middleware.rs
main.rs
Beta Was this translation helpful? Give feedback.
All reactions