Skip to content

Commit d75fba5

Browse files
authored
Add support for maxInboundMetadataSize client configuration (#1064)
1 parent 3432610 commit d75fba5

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,43 @@ 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. If not set ({@code null}) then it will default
341+
* to gRPC's default. The default is implementation-dependent, but is not generally less than 8 KiB and may be
342+
* unlimited. If set to {@code -1} then it will use the highest possible limit (not recommended). Integer.MAX_VALUE
343+
* disables the enforcement.
344+
*
345+
* @return The maximum size of metadata in bytes allowed to be received or null if the default should be used.
346+
*
347+
* @see ManagedChannelBuilder#maxInboundMetadataSize(int) (int)
348+
*/
349+
public DataSize getMaxInboundMetadataSize() {
350+
return maxInboundMetadataSize;
351+
}
352+
353+
/**
354+
* Sets the maximum size of metadata in bytes allowed to be received. If not set ({@code null}) then it will
355+
* default.The default is implementation-dependent, but is not generally less than 8 KiB and may be unlimited. If
356+
* set to {@code -1} then it will use the highest possible limit (not recommended). Integer.MAX_VALUE disables the
357+
* enforcement.
358+
*
359+
* @param maxInboundMetadataSize The new maximum size of metadata in bytes allowed to be received. {@code -1} for
360+
* max possible. Null to use the gRPC's default.
361+
*
362+
* @see ManagedChannelBuilder#maxInboundMetadataSize(int) (int)
363+
*/
364+
public void setMaxInboundMetadataSize(DataSize maxInboundMetadataSize) {
365+
if (maxInboundMetadataSize == null || maxInboundMetadataSize.toBytes() >= 0) {
366+
this.maxInboundMetadataSize = maxInboundMetadataSize;
367+
} else if (maxInboundMetadataSize.toBytes() == -1) {
368+
this.maxInboundMetadataSize = DataSize.ofBytes(Integer.MAX_VALUE);
369+
} else {
370+
throw new IllegalArgumentException("Unsupported maxInboundMetadataSize: " + maxInboundMetadataSize);
371+
}
372+
}
336373
// --------------------------------------------------
337374

338375
private Boolean fullStreamDecompression;

grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@
9494
"sourceType": "net.devh.boot.grpc.client.config.GrpcChannelProperties",
9595
"description": "The maximum message size allowed to be received by the channel.\nIf not set (null) then it will default to gRPC's default.\nIf set to -1 then it will use the highest possible limit (not recommended)."
9696
},
97+
{
98+
"name": "grpc.client.GLOBAL.max-inbound-metadata-size",
99+
"type": "org.springframework.util.unit.DataSize",
100+
"sourceType": "net.devh.boot.grpc.client.config.GrpcChannelProperties",
101+
"description": "the maximum size of metadata in bytes allowed to be received. \nIf not set (null) then it will default to gRPC's default. \nIf set to {@code -1} then it will use the highest possible limit (not recommended)."
102+
},
97103
{
98104
"name": "grpc.client.GLOBAL.negotiation-type",
99105
"type": "net.devh.boot.grpc.client.config.NegotiationType",
@@ -158,4 +164,4 @@
158164
"description": "The path to the trusted certificate collection.\nIf not set (null) it will use the system's default collection (Default).\nThis collection will be used to verify server certificates."
159165
}
160166
]
161-
}
167+
}

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
}

grpc-client-spring-boot-starter/src/test/java/net/devh/boot/grpc/client/config/GrpcChannelPropertiesNoUnitTest.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=42",
36-
"grpc.client.test.maxInboundMessageSize=5242880"
36+
"grpc.client.test.maxInboundMessageSize=5242880",
37+
"grpc.client.test.maxInboundMetadataSize=3145728"
3738
})
3839
class GrpcChannelPropertiesNoUnitTest {
3940

@@ -45,6 +46,7 @@ void test() {
4546
final GrpcChannelProperties properties = this.grpcChannelsProperties.getChannel("test");
4647
assertEquals(Duration.ofSeconds(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)