Skip to content

Commit f540f4f

Browse files
chore: Support configuration for max grpc inbound message (#1411) (#1469)
Signed-off-by: Javier Aliaga <[email protected]> Co-authored-by: Javier Aliaga <[email protected]>
1 parent 6823344 commit f540f4f

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

sdk/src/main/java/io/dapr/config/Properties.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@ public class Properties {
175175
"DAPR_HTTP_CLIENT_MAX_IDLE_CONNECTIONS",
176176
DEFAULT_HTTP_CLIENT_MAX_IDLE_CONNECTIONS);
177177

178+
179+
180+
/**
181+
* Dapr's default maximum inbound message size for GRPC in bytes.
182+
*/
183+
public static final Property<Integer> GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES = new IntegerProperty(
184+
"dapr.grpc.max.inbound.message.size.bytes",
185+
"DAPR_GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES",
186+
4194304);
187+
188+
/**
189+
* Dapr's default maximum inbound metadata size for GRPC in bytes.
190+
*/
191+
public static final Property<Integer> GRPC_MAX_INBOUND_METADATA_SIZE_BYTES = new IntegerProperty(
192+
"dapr.grpc.max.inbound.metadata.size.bytes",
193+
"DAPR_GRPC_MAX_INBOUND_METADATA_SIZE_BYTES",
194+
8192);
195+
178196
/**
179197
* Mechanism to override properties set in a static context.
180198
*/

sdk/src/main/java/io/dapr/utils/NetworkUtils.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.regex.Pattern;
2626

2727
import static io.dapr.config.Properties.GRPC_ENDPOINT;
28+
import static io.dapr.config.Properties.GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES;
29+
import static io.dapr.config.Properties.GRPC_MAX_INBOUND_METADATA_SIZE_BYTES;
2830
import static io.dapr.config.Properties.GRPC_PORT;
2931
import static io.dapr.config.Properties.SIDECAR_IP;
3032

@@ -121,22 +123,34 @@ public static ManagedChannel buildGrpcManagedChannel(Properties properties, Clie
121123
if (interceptors != null && interceptors.length > 0) {
122124
builder = builder.intercept(interceptors);
123125
}
124-
return builder.build();
126+
return builder.maxInboundMessageSize(settings.maxInboundMessageSize)
127+
.maxInboundMetadataSize(settings.maxInboundMetadataSize)
128+
.build();
125129
}
126130

127131
// Not private to allow unit testing
128132
static final class GrpcEndpointSettings {
129133
final String endpoint;
130134
final boolean secure;
131135

132-
private GrpcEndpointSettings(String endpoint, boolean secure) {
136+
final int maxInboundMessageSize;
137+
final int maxInboundMetadataSize;
138+
139+
private GrpcEndpointSettings(
140+
String endpoint, boolean secure,
141+
int maxInboundMessageSize, int maxInboundMetadataSize) {
133142
this.endpoint = endpoint;
134143
this.secure = secure;
144+
this.maxInboundMessageSize = maxInboundMessageSize;
145+
this.maxInboundMetadataSize = maxInboundMetadataSize;
135146
}
136147

137148
static GrpcEndpointSettings parse(Properties properties) {
138149
String address = properties.getValue(SIDECAR_IP);
139150
int port = properties.getValue(GRPC_PORT);
151+
int maxInboundMessageSizeBytes = properties.getValue(GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES);
152+
int maxInboundMetadataSizeBytes = properties.getValue(GRPC_MAX_INBOUND_METADATA_SIZE_BYTES);
153+
140154
boolean secure = false;
141155
String grpcEndpoint = properties.getValue(GRPC_ENDPOINT);
142156
if ((grpcEndpoint != null) && !grpcEndpoint.isEmpty()) {
@@ -172,21 +186,23 @@ static GrpcEndpointSettings parse(Properties properties) {
172186

173187
var authorityEndpoint = matcher.group("authorityEndpoint");
174188
if (authorityEndpoint != null) {
175-
return new GrpcEndpointSettings(String.format("dns://%s/%s:%d", authorityEndpoint, address, port), secure);
189+
return new GrpcEndpointSettings(String.format("dns://%s/%s:%d", authorityEndpoint, address, port),
190+
secure, maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
176191
}
177192

178193
var socket = matcher.group("socket");
179194
if (socket != null) {
180-
return new GrpcEndpointSettings(socket, secure);
195+
return new GrpcEndpointSettings(socket, secure, maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
181196
}
182197

183198
var vsocket = matcher.group("vsocket");
184199
if (vsocket != null) {
185-
return new GrpcEndpointSettings(vsocket, secure);
200+
return new GrpcEndpointSettings(vsocket, secure, maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
186201
}
187202
}
188203

189-
return new GrpcEndpointSettings(String.format("dns:///%s:%d", address, port), secure);
204+
return new GrpcEndpointSettings(String.format("dns:///%s:%d", address, port), secure,
205+
maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
190206
}
191207

192208
}

sdk/src/test/java/io/dapr/utils/NetworkUtilsTest.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
114
package io.dapr.utils;
215

316
import io.dapr.config.Properties;
@@ -146,4 +159,27 @@ private static void testGrpcEndpointParsingErrorScenario(String grpcEndpointEnvV
146159
// Expected
147160
}
148161
}
149-
}
162+
163+
@Test
164+
public void testMaxDefaultInboundSize() throws Exception {
165+
Properties properties = new Properties();
166+
167+
NetworkUtils.GrpcEndpointSettings settings = NetworkUtils.GrpcEndpointSettings.parse(properties);
168+
Assertions.assertEquals(4194304, settings.maxInboundMessageSize);
169+
Assertions.assertEquals(8192, settings.maxInboundMetadataSize);
170+
171+
}
172+
173+
@Test
174+
public void testMaxInboundSize() throws Exception {
175+
Properties properties = new Properties(Map.of(
176+
Properties.GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES.getName(), "123456789",
177+
Properties.GRPC_MAX_INBOUND_METADATA_SIZE_BYTES.getName(), "123456"
178+
));
179+
180+
NetworkUtils.GrpcEndpointSettings settings = NetworkUtils.GrpcEndpointSettings.parse(properties);
181+
Assertions.assertEquals(123456789, settings.maxInboundMessageSize);
182+
Assertions.assertEquals(123456, settings.maxInboundMetadataSize);
183+
184+
}
185+
}

0 commit comments

Comments
 (0)