Skip to content

Commit 2a21adb

Browse files
committed
Add support for maxInboundMetadataSize client configuration
1 parent b66d4d6 commit 2a21adb

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/channelfactory/AbstractChannelFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ protected boolean isNonNullAndNonBlank(final String value) {
234234
}
235235

236236
/**
237-
* Configures limits such as max message sizes that should be used by the channel.
237+
* Configures limits such as max message or metadata sizes that should be used by the channel.
238238
*
239239
* @param builder The channel builder to configure.
240240
* @param name The name of the client to configure.
@@ -245,6 +245,10 @@ protected void configureLimits(final T builder, final String name) {
245245
if (maxInboundMessageSize != null) {
246246
builder.maxInboundMessageSize((int) maxInboundMessageSize.toBytes());
247247
}
248+
final DataSize maxInboundMetadataSize = properties.getMaxInboundMetadataSize();
249+
if (maxInboundMetadataSize != null) {
250+
builder.maxInboundMetadataSize((int) maxInboundMetadataSize.toBytes());
251+
}
248252
}
249253

250254
/**

grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,41 @@ public void setMaxInboundMessageSize(final DataSize maxInboundMessageSize) {
333333
}
334334
}
335335

336+
@DataSizeUnit(DataUnit.BYTES)
337+
private DataSize maxInboundMetadataSize = null;
338+
339+
/**
340+
* Sets the maximum size of metadata in bytes allowed to be received.
341+
* If not set ({@code null}) then it will default.The default is implementation-dependent, but is not generally less than 8 KiB and may be unlimited.
342+
* If set to {@code -1} then it will use the highest possible limit (not recommended). Integer.MAX_VALUE disables the enforcement.
343+
*
344+
* @return The maximum size of metadata in bytes allowed to be received or null if the default should be used.
345+
*
346+
* @see ManagedChannelBuilder#maxInboundMetadataSize(int) (int)
347+
*/
348+
public DataSize getMaxInboundMetadataSize() {
349+
return maxInboundMetadataSize;
350+
}
351+
352+
/**
353+
* Sets the maximum size of metadata in bytes allowed to be received.
354+
* If not set ({@code null}) then it will default.The default is implementation-dependent, but is not generally less than 8 KiB and may be unlimited.
355+
* If set to {@code -1} then it will use the highest possible limit (not recommended). Integer.MAX_VALUE disables the enforcement.
356+
*
357+
* @param maxInboundMetadataSize The new maximum size of metadata in bytes allowed to be received. {@code -1} for max
358+
* possible. Null to use the gRPC's default.
359+
*
360+
* @see ManagedChannelBuilder#maxInboundMetadataSize(int) (int)
361+
*/
362+
public void setMaxInboundMetadataSize(DataSize maxInboundMetadataSize) {
363+
if (maxInboundMetadataSize == null || maxInboundMetadataSize.toBytes() >= 0) {
364+
this.maxInboundMetadataSize = maxInboundMetadataSize;
365+
} else if (maxInboundMetadataSize.toBytes() == -1) {
366+
this.maxInboundMetadataSize = DataSize.ofBytes(Integer.MAX_VALUE);
367+
} else {
368+
throw new IllegalArgumentException("Unsupported maxInboundMetadataSize: " + maxInboundMetadataSize);
369+
}
370+
}
336371
// --------------------------------------------------
337372

338373
private Boolean fullStreamDecompression;

grpc-client-spring-boot-starter/src/test/java/net/devh/boot/grpc/client/config/GrpcChannelPropertiesGivenUnitTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
@ExtendWith(SpringExtension.class)
3434
@SpringBootTest(properties = {
3535
"grpc.client.test.keepAliveTime=42m",
36-
"grpc.client.test.maxInboundMessageSize=5MB"
36+
"grpc.client.test.maxInboundMessageSize=5MB",
37+
"grpc.client.test.maxInboundMetadataSize=3MB"
3738
})
3839
class GrpcChannelPropertiesGivenUnitTest {
3940

@@ -45,6 +46,7 @@ void test() {
4546
final GrpcChannelProperties properties = this.grpcChannelsProperties.getChannel("test");
4647
assertEquals(Duration.ofMinutes(42), properties.getKeepAliveTime());
4748
assertEquals(DataSize.ofMegabytes(5), properties.getMaxInboundMessageSize());
49+
assertEquals(DataSize.ofMegabytes(3), properties.getMaxInboundMetadataSize());
4850
}
4951

5052
}

0 commit comments

Comments
 (0)