From e13f934efef107680d0dbeca8e739b83e05d35e2 Mon Sep 17 00:00:00 2001 From: Javier Aliaga Date: Tue, 10 Jun 2025 23:05:42 +0200 Subject: [PATCH] chore: Support configuration for max grpc inbound message (#1411) Signed-off-by: Javier Aliaga --- .../main/java/io/dapr/config/Properties.java | 18 +++++++++++ .../main/java/io/dapr/utils/NetworkUtils.java | 31 ++++++++++++++----- .../java/io/dapr/utils/NetworkUtilsTest.java | 23 ++++++++++++++ 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/sdk/src/main/java/io/dapr/config/Properties.java b/sdk/src/main/java/io/dapr/config/Properties.java index ffc2ed77b..7287dbcdf 100644 --- a/sdk/src/main/java/io/dapr/config/Properties.java +++ b/sdk/src/main/java/io/dapr/config/Properties.java @@ -254,6 +254,24 @@ public class Properties { "DAPR_HTTP_CLIENT_MAX_IDLE_CONNECTIONS", DEFAULT_HTTP_CLIENT_MAX_IDLE_CONNECTIONS); + + + /** + * Dapr's default maximum inbound message size for GRPC in bytes. + */ + public static final Property GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES = new IntegerProperty( + "dapr.grpc.max.inbound.message.size.bytes", + "DAPR_GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES", + 4194304); + + /** + * Dapr's default maximum inbound metadata size for GRPC in bytes. + */ + public static final Property GRPC_MAX_INBOUND_METADATA_SIZE_BYTES = new IntegerProperty( + "dapr.grpc.max.inbound.metadata.size.bytes", + "DAPR_GRPC_MAX_INBOUND_METADATA_SIZE_BYTES", + 8192); + /** * Mechanism to override properties set in a static context. */ diff --git a/sdk/src/main/java/io/dapr/utils/NetworkUtils.java b/sdk/src/main/java/io/dapr/utils/NetworkUtils.java index 431bed792..49b206aeb 100644 --- a/sdk/src/main/java/io/dapr/utils/NetworkUtils.java +++ b/sdk/src/main/java/io/dapr/utils/NetworkUtils.java @@ -41,6 +41,8 @@ import static io.dapr.config.Properties.GRPC_ENDPOINT; import static io.dapr.config.Properties.GRPC_KEEP_ALIVE_TIMEOUT_SECONDS; import static io.dapr.config.Properties.GRPC_KEEP_ALIVE_TIME_SECONDS; import static io.dapr.config.Properties.GRPC_KEEP_ALIVE_WITHOUT_CALLS; +import static io.dapr.config.Properties.GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES; +import static io.dapr.config.Properties.GRPC_MAX_INBOUND_METADATA_SIZE_BYTES; import static io.dapr.config.Properties.GRPC_PORT; import static io.dapr.config.Properties.GRPC_TLS_CA_PATH; import static io.dapr.config.Properties.GRPC_TLS_CERT_PATH; @@ -48,6 +50,7 @@ import static io.dapr.config.Properties.GRPC_TLS_INSECURE; import static io.dapr.config.Properties.GRPC_TLS_KEY_PATH; import static io.dapr.config.Properties.SIDECAR_IP; + /** * Utility methods for network, internal to Dapr SDK. */ @@ -152,8 +155,11 @@ public final class NetworkUtils { .keepAliveTimeout(settings.keepAliveTimeoutSeconds.toSeconds(), TimeUnit.SECONDS) .keepAliveWithoutCalls(settings.keepAliveWithoutCalls); } + + return builder.maxInboundMessageSize(settings.maxInboundMessageSize) + .maxInboundMetadataSize(settings.maxInboundMetadataSize) + .build(); - return builder.build(); } catch (Exception e) { throw new DaprException( new DaprError().setErrorCode("TLS_CREDENTIALS_ERROR") @@ -217,7 +223,8 @@ public final class NetworkUtils { .keepAliveWithoutCalls(settings.keepAliveWithoutCalls); } - return builder.build(); + return builder.maxInboundMessageSize(settings.maxInboundMessageSize) + .maxInboundMetadataSize(settings.maxInboundMetadataSize).build(); } // Not private to allow unit testing @@ -233,10 +240,13 @@ public final class NetworkUtils { final Duration keepAliveTimeoutSeconds; final boolean keepAliveWithoutCalls; + final int maxInboundMessageSize; + final int maxInboundMetadataSize; + private GrpcEndpointSettings( String endpoint, boolean secure, String tlsPrivateKeyPath, String tlsCertPath, String tlsCaPath, boolean enableKeepAlive, Duration keepAliveTimeSeconds, Duration keepAliveTimeoutSeconds, - boolean keepAliveWithoutCalls) { + boolean keepAliveWithoutCalls, int maxInboundMessageSize, int maxInboundMetadataSize) { this.endpoint = endpoint; this.secure = secure; this.tlsPrivateKeyPath = tlsPrivateKeyPath; @@ -246,6 +256,8 @@ public final class NetworkUtils { this.keepAliveTimeSeconds = keepAliveTimeSeconds; this.keepAliveTimeoutSeconds = keepAliveTimeoutSeconds; this.keepAliveWithoutCalls = keepAliveWithoutCalls; + this.maxInboundMessageSize = maxInboundMessageSize; + this.maxInboundMetadataSize = maxInboundMetadataSize; } static GrpcEndpointSettings parse(Properties properties) { @@ -258,6 +270,8 @@ public final class NetworkUtils { Duration keepAliveTimeSeconds = properties.getValue(GRPC_KEEP_ALIVE_TIME_SECONDS); Duration keepAliveTimeoutSeconds = properties.getValue(GRPC_KEEP_ALIVE_TIMEOUT_SECONDS); boolean keepAliveWithoutCalls = properties.getValue(GRPC_KEEP_ALIVE_WITHOUT_CALLS); + int maxInboundMessageSizeBytes = properties.getValue(GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES); + int maxInboundMetadataSizeBytes = properties.getValue(GRPC_MAX_INBOUND_METADATA_SIZE_BYTES); boolean secure = false; String grpcEndpoint = properties.getValue(GRPC_ENDPOINT); @@ -301,19 +315,21 @@ public final class NetworkUtils { address, port), secure, clientKeyPath, clientCertPath, caCertPath, enablekeepAlive, keepAliveTimeSeconds, - keepAliveTimeoutSeconds, keepAliveWithoutCalls); + keepAliveTimeoutSeconds, keepAliveWithoutCalls, maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes); } var socket = matcher.group("socket"); if (socket != null) { return new GrpcEndpointSettings(socket, secure, clientKeyPath, clientCertPath, caCertPath, enablekeepAlive, - keepAliveTimeSeconds, keepAliveTimeoutSeconds, keepAliveWithoutCalls); + keepAliveTimeSeconds, keepAliveTimeoutSeconds, keepAliveWithoutCalls, + maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes); } var vsocket = matcher.group("vsocket"); if (vsocket != null) { return new GrpcEndpointSettings(vsocket, secure, clientKeyPath, clientCertPath, caCertPath, enablekeepAlive, - keepAliveTimeSeconds, keepAliveTimeoutSeconds, keepAliveWithoutCalls); + keepAliveTimeSeconds, keepAliveTimeoutSeconds, keepAliveWithoutCalls, + maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes); } } @@ -321,7 +337,8 @@ public final class NetworkUtils { "dns:///%s:%d", address, port), secure, clientKeyPath, clientCertPath, caCertPath, enablekeepAlive, keepAliveTimeSeconds, - keepAliveTimeoutSeconds, keepAliveWithoutCalls); + keepAliveTimeoutSeconds, keepAliveWithoutCalls, + maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes); } } diff --git a/sdk/src/test/java/io/dapr/utils/NetworkUtilsTest.java b/sdk/src/test/java/io/dapr/utils/NetworkUtilsTest.java index 4ad80fe5b..3823bef3b 100644 --- a/sdk/src/test/java/io/dapr/utils/NetworkUtilsTest.java +++ b/sdk/src/test/java/io/dapr/utils/NetworkUtilsTest.java @@ -632,4 +632,27 @@ public class NetworkUtilsTest { Assertions.assertEquals(50, settings.keepAliveTimeoutSeconds.getSeconds()); Assertions.assertEquals(false, settings.keepAliveWithoutCalls); } + + @Test + public void testMaxDefaultInboundSize() throws Exception { + Properties properties = new Properties(); + + GrpcEndpointSettings settings = NetworkUtils.GrpcEndpointSettings.parse(properties); + Assertions.assertEquals(4194304, settings.maxInboundMessageSize); + Assertions.assertEquals(8192, settings.maxInboundMetadataSize); + + } + + @Test + public void testMaxInboundSize() throws Exception { + Properties properties = new Properties(Map.of( + Properties.GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES.getName(), "123456789", + Properties.GRPC_MAX_INBOUND_METADATA_SIZE_BYTES.getName(), "123456" + )); + + GrpcEndpointSettings settings = NetworkUtils.GrpcEndpointSettings.parse(properties); + Assertions.assertEquals(123456789, settings.maxInboundMessageSize); + Assertions.assertEquals(123456, settings.maxInboundMetadataSize); + + } } \ No newline at end of file