Skip to content

Instantly share code, notes, and snippets.

@xd009642
Created August 29, 2023 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xd009642/016f93bf3df0992c075cff1040593e62 to your computer and use it in GitHub Desktop.
Save xd009642/016f93bf3df0992c075cff1040593e62 to your computer and use it in GitHub Desktop.
use core::task::{Context, Poll};
use http::{Request, Response};
use std::convert::Infallible;
use std::sync::{Arc, RwLock};
use tonic::body::BoxBody;
use tonic::transport::Body;
use tower_layer::Layer;
use tower_service::Service;
pub struct ReloadLayer {
}
impl<S> Layer<Arc<RwLock<S>>> for ReloadLayer {
type Service = ReloadService<S>;
fn layer(&self, service: Arc<RwLock<S>>) -> Self::Service {
ReloadService { service }
}
}
// This service implements the Log behavior
pub struct ReloadService<S> {
pub(crate) service: Arc<RwLock<S>>,
}
// S: Service<Request<Body>, Response = Response<BoxBody>, Error = Infallible> + NamedService + Clone + Send + 'static
impl<S> Service<Request<Body>> for ReloadService<S>
where
S: Service<Request<Body>>,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.write().unwrap().poll_ready(cx)
}
fn call(&mut self, request: Request<Body>) -> Self::Future {
self.service.write().unwrap().call(request)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment