pub struct GrpcServerBuilder { /* private fields */ }router-grpc only.Expand description
Builder for configuring gRPC servers
Provides a fluent API for setting up gRPC servers with various options including TLS, reflection, health checks, and graceful shutdown.
Implementations§
Source§impl GrpcServerBuilder
impl GrpcServerBuilder
Sourcepub fn tls_from_env(self) -> Self
pub fn tls_from_env(self) -> Self
Enable TLS from environment variables
Reads from GRPC_TLS_CERT, GRPC_TLS_KEY, and optionally GRPC_TLS_CLIENT_CA
Sourcepub fn reflection(self, file_descriptor_set: &'static [u8]) -> Self
pub fn reflection(self, file_descriptor_set: &'static [u8]) -> Self
Enable gRPC reflection with the given file descriptor set
The file descriptor set is typically generated by tonic-build and available as a const in your generated code.
§Example
// In your proto build output
pub const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!("proto/descriptor.bin");
// In your server setup
GrpcServer::builder()
.reflection(FILE_DESCRIPTOR_SET)Sourcepub fn health_check(self) -> Self
pub fn health_check(self) -> Self
Enable gRPC health checking service
This adds the standard gRPC health checking protocol endpoints.
Sourcepub fn graceful_shutdown(self, shutdown: GracefulShutdown) -> Self
pub fn graceful_shutdown(self, shutdown: GracefulShutdown) -> Self
Set the graceful shutdown handler
When a shutdown signal is received, the server will stop accepting new connections and wait for existing requests to complete.
Sourcepub fn shutdown_timeout(self, timeout: Duration) -> Self
pub fn shutdown_timeout(self, timeout: Duration) -> Self
Set the shutdown timeout
How long to wait for existing requests to complete during shutdown. Default is 30 seconds.
Sourcepub fn get_addr(&self) -> SocketAddr
pub fn get_addr(&self) -> SocketAddr
Get the configured address
Sourcepub fn has_reflection(&self) -> bool
pub fn has_reflection(&self) -> bool
Check if reflection is enabled
Sourcepub fn has_health_check(&self) -> bool
pub fn has_health_check(&self) -> bool
Check if health check is enabled
Sourcepub async fn serve_router(self, router: Router) -> Result<(), GrpcServerError>
pub async fn serve_router(self, router: Router) -> Result<(), GrpcServerError>
Serve a gRPC router
This method consumes the builder and starts the gRPC server. The server will run until shutdown is signaled or an error occurs.
§Example
use tonic::transport::Server;
// Build your router with services
let router = Server::builder()
.add_service(MyServiceServer::new(my_service))
.add_service(OtherServiceServer::new(other_service));
// Use the GrpcServer builder for configuration
GrpcServer::builder()
.addr("[::1]:50051")
.graceful_shutdown(shutdown)
.serve_router(router)
.await?;Sourcepub fn server_builder(&self) -> Server
pub fn server_builder(&self) -> Server
Build a tonic Server with reflection service
Returns a tonic::transport::Server builder. Call add_service on it to add your services, then pass the resulting router to serve_router.
Sourcepub fn reflection_service(
&self,
) -> Result<Option<ServerReflectionServer<impl ServerReflection>>, GrpcServerError>
pub fn reflection_service( &self, ) -> Result<Option<ServerReflectionServer<impl ServerReflection>>, GrpcServerError>
Create a reflection service from the configured file descriptor set
Returns None if no file descriptor set was configured.
Sourcepub fn create_health_reporter(
&self,
) -> Option<(HealthReporter, impl Service<Request<Incoming>, Response = Response<Body>, Error = Infallible> + Clone + Send + 'static)>
pub fn create_health_reporter( &self, ) -> Option<(HealthReporter, impl Service<Request<Incoming>, Response = Response<Body>, Error = Infallible> + Clone + Send + 'static)>
Check if health check is enabled and return a health reporter
Use the returned function to create the health service. The health_reporter() function returns (HealthReporter, HealthService).
§Example
let builder = GrpcServer::builder().health_check();
if builder.has_health_check() {
let (mut reporter, service) = tonic_health::server::health_reporter();
// Add service to router
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for GrpcServerBuilder
impl !RefUnwindSafe for GrpcServerBuilder
impl Send for GrpcServerBuilder
impl Sync for GrpcServerBuilder
impl Unpin for GrpcServerBuilder
impl !UnwindSafe for GrpcServerBuilder
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request