forked from restatedev/restate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let MetadataStore use the node grpc server
In order to make the MetadataStore use the node grpc server, the grpc server needs to be started first. Since a few grpc services depend on a node having joined the cluster (in order to obtain a NodeId), this commit disables these until they can be used. To disable Tonic services, this commit introduces the TonicServiceFilter which takes a Tonic service and a predicate. The predicate allows to check on a status to decide whether to forward the request or fail the request with tonic::Status. This fixes restatedev#2411.
- Loading branch information
1 parent
0cd6be3
commit d9d52a8
Showing
30 changed files
with
431 additions
and
303 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) 2023 - 2024 Restate Software, Inc., Restate GmbH. | ||
// All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
|
||
use futures::future::{Either, Ready}; | ||
use http::Request; | ||
use std::convert::Infallible; | ||
use std::task::{Context, Poll}; | ||
use tonic::body::BoxBody; | ||
use tonic::codegen::Service; | ||
use tonic::server::NamedService; | ||
|
||
/// A tonic service wrapper that filters requests based on a predicate. This can be used to | ||
/// dynamically disable a service based on some condition. | ||
#[derive(Clone)] | ||
pub struct TonicServiceFilter<T, U> { | ||
inner: T, | ||
predicate: U, | ||
} | ||
|
||
impl<T, U> TonicServiceFilter<T, U> { | ||
pub fn new(inner: T, predicate: U) -> Self { | ||
Self { inner, predicate } | ||
} | ||
} | ||
|
||
impl<T, U> Service<Request<BoxBody>> for TonicServiceFilter<T, U> | ||
where | ||
T: Service<Request<BoxBody>, Response = http::Response<BoxBody>, Error = Infallible>, | ||
U: Predicate, | ||
{ | ||
type Response = http::Response<BoxBody>; | ||
type Error = Infallible; | ||
type Future = Either<T::Future, Ready<Result<http::Response<BoxBody>, Infallible>>>; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx) | ||
} | ||
|
||
fn call(&mut self, req: Request<BoxBody>) -> Self::Future { | ||
match self.predicate.check(req) { | ||
Ok(req) => Either::Left(self.inner.call(req)), | ||
Err(status) => Either::Right(futures::future::ready(Ok(status.into_http()))), | ||
} | ||
} | ||
} | ||
|
||
/// Predicate for the [`TonicServiceFilter`]. | ||
pub trait Predicate { | ||
/// Checks whether the given request should be processed. Return the given `request` if it | ||
/// should be processed. Otherwise, return the [`tonic::Status`] with which the request should | ||
/// fail. | ||
fn check(&mut self, request: Request<BoxBody>) -> Result<Request<BoxBody>, tonic::Status>; | ||
} | ||
|
||
impl<T, U> NamedService for TonicServiceFilter<T, U> | ||
where | ||
T: NamedService, | ||
{ | ||
const NAME: &'static str = T::NAME; | ||
} | ||
|
||
impl<F> Predicate for F | ||
where | ||
F: FnMut(Request<BoxBody>) -> Result<Request<BoxBody>, tonic::Status>, | ||
{ | ||
fn check(&mut self, request: Request<BoxBody>) -> Result<Request<BoxBody>, tonic::Status> { | ||
self(request) | ||
} | ||
} |
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
Oops, something went wrong.