Skip to content

Commit 1a69298

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

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-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
@@ -242,6 +242,24 @@ public class Properties {
242242
"DAPR_HTTP_CLIENT_MAX_IDLE_CONNECTIONS",
243243
DEFAULT_HTTP_CLIENT_MAX_IDLE_CONNECTIONS);
244244

245+
246+
247+
/**
248+
* Dapr's default maximum inbound message size for GRPC in bytes.
249+
*/
250+
public static final Property<Integer> GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES = new IntegerProperty(
251+
"dapr.grpc.max.inbound.message.size.bytes",
252+
"DAPR_GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES",
253+
4194304);
254+
255+
/**
256+
* Dapr's default maximum inbound metadata size for GRPC in bytes.
257+
*/
258+
public static final Property<Integer> GRPC_MAX_INBOUND_METADATA_SIZE_BYTES = new IntegerProperty(
259+
"dapr.grpc.max.inbound.metadata.size.bytes",
260+
"DAPR_GRPC_MAX_INBOUND_METADATA_SIZE_BYTES",
261+
8192);
262+
245263
/**
246264
* Mechanism to override properties set in a static context.
247265
*/

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@
4141
import static io.dapr.config.Properties.GRPC_KEEP_ALIVE_TIMEOUT_SECONDS;
4242
import static io.dapr.config.Properties.GRPC_KEEP_ALIVE_TIME_SECONDS;
4343
import static io.dapr.config.Properties.GRPC_KEEP_ALIVE_WITHOUT_CALLS;
44+
import static io.dapr.config.Properties.GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES;
45+
import static io.dapr.config.Properties.GRPC_MAX_INBOUND_METADATA_SIZE_BYTES;
4446
import static io.dapr.config.Properties.GRPC_PORT;
4547
import static io.dapr.config.Properties.GRPC_TLS_CA_PATH;
4648
import static io.dapr.config.Properties.GRPC_TLS_CERT_PATH;
4749
import static io.dapr.config.Properties.GRPC_TLS_INSECURE;
4850
import static io.dapr.config.Properties.GRPC_TLS_KEY_PATH;
4951
import static io.dapr.config.Properties.SIDECAR_IP;
5052

53+
5154
/**
5255
* Utility methods for network, internal to Dapr SDK.
5356
*/
@@ -152,8 +155,11 @@ public static ManagedChannel buildGrpcManagedChannel(Properties properties, Clie
152155
.keepAliveTimeout(settings.keepAliveTimeoutSeconds.toSeconds(), TimeUnit.SECONDS)
153156
.keepAliveWithoutCalls(settings.keepAliveWithoutCalls);
154157
}
158+
159+
return builder.maxInboundMessageSize(settings.maxInboundMessageSize)
160+
.maxInboundMetadataSize(settings.maxInboundMetadataSize)
161+
.build();
155162

156-
return builder.build();
157163
} catch (Exception e) {
158164
throw new DaprException(
159165
new DaprError().setErrorCode("TLS_CREDENTIALS_ERROR")
@@ -217,7 +223,8 @@ public static ManagedChannel buildGrpcManagedChannel(Properties properties, Clie
217223
.keepAliveWithoutCalls(settings.keepAliveWithoutCalls);
218224
}
219225

220-
return builder.build();
226+
return builder.maxInboundMessageSize(settings.maxInboundMessageSize)
227+
.maxInboundMetadataSize(settings.maxInboundMetadataSize).build();
221228
}
222229

223230
// Not private to allow unit testing
@@ -233,10 +240,13 @@ static final class GrpcEndpointSettings {
233240
final Duration keepAliveTimeoutSeconds;
234241
final boolean keepAliveWithoutCalls;
235242

243+
final int maxInboundMessageSize;
244+
final int maxInboundMetadataSize;
245+
236246
private GrpcEndpointSettings(
237247
String endpoint, boolean secure, String tlsPrivateKeyPath, String tlsCertPath, String tlsCaPath,
238248
boolean enableKeepAlive, Duration keepAliveTimeSeconds, Duration keepAliveTimeoutSeconds,
239-
boolean keepAliveWithoutCalls) {
249+
boolean keepAliveWithoutCalls, int maxInboundMessageSize, int maxInboundMetadataSize) {
240250
this.endpoint = endpoint;
241251
this.secure = secure;
242252
this.tlsPrivateKeyPath = tlsPrivateKeyPath;
@@ -246,6 +256,8 @@ private GrpcEndpointSettings(
246256
this.keepAliveTimeSeconds = keepAliveTimeSeconds;
247257
this.keepAliveTimeoutSeconds = keepAliveTimeoutSeconds;
248258
this.keepAliveWithoutCalls = keepAliveWithoutCalls;
259+
this.maxInboundMessageSize = maxInboundMessageSize;
260+
this.maxInboundMetadataSize = maxInboundMetadataSize;
249261
}
250262

251263
static GrpcEndpointSettings parse(Properties properties) {
@@ -258,6 +270,8 @@ static GrpcEndpointSettings parse(Properties properties) {
258270
Duration keepAliveTimeSeconds = properties.getValue(GRPC_KEEP_ALIVE_TIME_SECONDS);
259271
Duration keepAliveTimeoutSeconds = properties.getValue(GRPC_KEEP_ALIVE_TIMEOUT_SECONDS);
260272
boolean keepAliveWithoutCalls = properties.getValue(GRPC_KEEP_ALIVE_WITHOUT_CALLS);
273+
int maxInboundMessageSizeBytes = properties.getValue(GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES);
274+
int maxInboundMetadataSizeBytes = properties.getValue(GRPC_MAX_INBOUND_METADATA_SIZE_BYTES);
261275

262276
boolean secure = false;
263277
String grpcEndpoint = properties.getValue(GRPC_ENDPOINT);
@@ -301,27 +315,30 @@ static GrpcEndpointSettings parse(Properties properties) {
301315
address,
302316
port),
303317
secure, clientKeyPath, clientCertPath, caCertPath, enablekeepAlive, keepAliveTimeSeconds,
304-
keepAliveTimeoutSeconds, keepAliveWithoutCalls);
318+
keepAliveTimeoutSeconds, keepAliveWithoutCalls, maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
305319
}
306320

307321
var socket = matcher.group("socket");
308322
if (socket != null) {
309323
return new GrpcEndpointSettings(socket, secure, clientKeyPath, clientCertPath, caCertPath, enablekeepAlive,
310-
keepAliveTimeSeconds, keepAliveTimeoutSeconds, keepAliveWithoutCalls);
324+
keepAliveTimeSeconds, keepAliveTimeoutSeconds, keepAliveWithoutCalls,
325+
maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
311326
}
312327

313328
var vsocket = matcher.group("vsocket");
314329
if (vsocket != null) {
315330
return new GrpcEndpointSettings(vsocket, secure, clientKeyPath, clientCertPath, caCertPath, enablekeepAlive,
316-
keepAliveTimeSeconds, keepAliveTimeoutSeconds, keepAliveWithoutCalls);
331+
keepAliveTimeSeconds, keepAliveTimeoutSeconds, keepAliveWithoutCalls,
332+
maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
317333
}
318334
}
319335

320336
return new GrpcEndpointSettings(String.format(
321337
"dns:///%s:%d",
322338
address,
323339
port), secure, clientKeyPath, clientCertPath, caCertPath, enablekeepAlive, keepAliveTimeSeconds,
324-
keepAliveTimeoutSeconds, keepAliveWithoutCalls);
340+
keepAliveTimeoutSeconds, keepAliveWithoutCalls,
341+
maxInboundMessageSizeBytes, maxInboundMetadataSizeBytes);
325342
}
326343

327344
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,4 +632,27 @@ public void testDefaultKeepAliveOverride() throws Exception {
632632
Assertions.assertEquals(50, settings.keepAliveTimeoutSeconds.getSeconds());
633633
Assertions.assertEquals(false, settings.keepAliveWithoutCalls);
634634
}
635+
636+
@Test
637+
public void testMaxDefaultInboundSize() throws Exception {
638+
Properties properties = new Properties();
639+
640+
GrpcEndpointSettings settings = NetworkUtils.GrpcEndpointSettings.parse(properties);
641+
Assertions.assertEquals(4194304, settings.maxInboundMessageSize);
642+
Assertions.assertEquals(8192, settings.maxInboundMetadataSize);
643+
644+
}
645+
646+
@Test
647+
public void testMaxInboundSize() throws Exception {
648+
Properties properties = new Properties(Map.of(
649+
Properties.GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES.getName(), "123456789",
650+
Properties.GRPC_MAX_INBOUND_METADATA_SIZE_BYTES.getName(), "123456"
651+
));
652+
653+
GrpcEndpointSettings settings = NetworkUtils.GrpcEndpointSettings.parse(properties);
654+
Assertions.assertEquals(123456789, settings.maxInboundMessageSize);
655+
Assertions.assertEquals(123456, settings.maxInboundMetadataSize);
656+
657+
}
635658
}

0 commit comments

Comments
 (0)