Setting Message Size Limits in Tonic While Using Service Interceptor

345 views Asked by At

I'm building a gRPC server using the Tonic framework in Rust (still exploring rust). By default, Tonic has a maximum message size limit of 4MB, and I would like to increase this limit to 50MB for both sending and receiving messages while intercepting incoming calls. However, I couldn't find a way to configure this on the server-side.

Here's a simplified example of my server setup:

async fn setup_and_run(&self, mut server_builder: Server) {
        let intercepted_service = registry_service_server::RegistryServiceServer::with_interceptor(
            self.registry.clone(),
            auth_guard,
        );
        let server = server_builder
            .add_service(intercepted_service)
            .add_service(user_service_server::UserServiceServer::new(
                self.user.clone(),
            ))
            .serve(self.addr)
            .await;

        match server {
            Err(err) => panic!("registry server failed: {:?}", err),
            Ok(_) => println!("registry server exited"),
        }
    }

Optimally would want to do something like:

let svc = registry_service_server::RegistryServiceServer::new(self.registry.clone())
       .max_decoding_message_size(50 * 1024 * 1024)
       .max_encoding_message_size(50 * 1024 * 1024);
let intercepted_service = registry_service_server::RegistryServiceServer::with_interceptor(
       svc,
       auth_guard,
);

But ::with_interceptor() accepts only the struct which implement the generated trait for the service.

I've checked the documentation and GitHub issues but haven't found a straightforward way to do this.

  1. How can I set the max_decoding_message_size and max_encoding_message_size while still using ::with_interceptor() on the server-side?
  2. Is there a way to access the inner service of InterceptedService to modify these parameters?
  3. Alternatively, is there any other recommended approach to set the message size limits while using an interceptor?
  4. Why is it that on the client side, I can set these properties alongside using an interceptor, but this does not seem straightforward to do on the server-side? Is this by design or a limitation? Any help or direction would be much appreciated.

In my client side it seems to work well with intercepted calls and setting custom message size:

let reg = registry_service_client::RegistryServiceClient::with_interceptor(
    channel.clone(),
    AuthInterceptor { token },
)
.max_decoding_message_size(50 * 1024 * 1024) // 50 MB
.max_encoding_message_size(50 * 1024 * 1024);
0

There are 0 answers