chore: Support configuration for max grpc inbound message (#1411)

Signed-off-by: Javier Aliaga <javier@diagrid.io>
This commit is contained in:
Javier Aliaga 2025-06-10 23:05:42 +02:00 committed by Cassandra Coyle
parent 6823344b4a
commit c6ab46ac7f
No known key found for this signature in database
3 changed files with 77 additions and 7 deletions

View File

@ -175,6 +175,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<Integer> 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<Integer> 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.
*/

View File

@ -25,6 +25,8 @@ import java.net.Socket;
import java.util.regex.Pattern;
import static io.dapr.config.Properties.GRPC_ENDPOINT;
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.SIDECAR_IP;
@ -121,7 +123,9 @@ public final class NetworkUtils {
if (interceptors != null && interceptors.length > 0) {
builder = builder.intercept(interceptors);
}
return builder.build();
return builder.maxInboundMessageSize(settings.maxInboundMessageSize)
.maxInboundMetadataSize(settings.maxInboundMetadataSize)
.build();
}
// Not private to allow unit testing
@ -129,14 +133,24 @@ public final class NetworkUtils {
final String endpoint;
final boolean secure;
private GrpcEndpointSettings(String endpoint, boolean secure) {
final int maxInboundMessageSize;
final int maxInboundMetadataSize;
private GrpcEndpointSettings(
String endpoint, boolean secure,
int maxInboundMessageSize, int maxInboundMetadataSize) {
this.endpoint = endpoint;
this.secure = secure;
this.maxInboundMessageSize = maxInboundMessageSize;
this.maxInboundMetadataSize = maxInboundMetadataSize;
}
static GrpcEndpointSettings parse(Properties properties) {
String address = properties.getValue(SIDECAR_IP);
int port = properties.getValue(GRPC_PORT);
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);
if ((grpcEndpoint != null) && !grpcEndpoint.isEmpty()) {
@ -172,21 +186,23 @@ public final class NetworkUtils {
var authorityEndpoint = matcher.group("authorityEndpoint");
if (authorityEndpoint != null) {
return new GrpcEndpointSettings(String.format("dns://%s/%s:%d", authorityEndpoint, address, port), secure);
return new GrpcEndpointSettings(String.format("dns://%s/%s:%d", authorityEndpoint, address, port),
secure, maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
}
var socket = matcher.group("socket");
if (socket != null) {
return new GrpcEndpointSettings(socket, secure);
return new GrpcEndpointSettings(socket, secure, maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
}
var vsocket = matcher.group("vsocket");
if (vsocket != null) {
return new GrpcEndpointSettings(vsocket, secure);
return new GrpcEndpointSettings(vsocket, secure, maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
}
}
return new GrpcEndpointSettings(String.format("dns:///%s:%d", address, port), secure);
return new GrpcEndpointSettings(String.format("dns:///%s:%d", address, port), secure,
maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
}
}

View File

@ -1,3 +1,16 @@
/*
* Copyright 2025 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/
package io.dapr.utils;
import io.dapr.config.Properties;
@ -146,4 +159,27 @@ public class NetworkUtilsTest {
// Expected
}
}
}
@Test
public void testMaxDefaultInboundSize() throws Exception {
Properties properties = new Properties();
NetworkUtils.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"
));
NetworkUtils.GrpcEndpointSettings settings = NetworkUtils.GrpcEndpointSettings.parse(properties);
Assertions.assertEquals(123456789, settings.maxInboundMessageSize);
Assertions.assertEquals(123456, settings.maxInboundMetadataSize);
}
}