Created
August 29, 2023 17:50
-
-
Save xd009642/016f93bf3df0992c075cff1040593e62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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