Skip to content

Commit b56a9bf

Browse files
authored
Merge pull request #380 from jbf154/feat/add_http_header_size_config
feat: add metadata size config for server
2 parents ea7c20e + a7997cc commit b56a9bf

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/config/GrpcServerProperties.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ public class GrpcServerProperties {
160160
@DataSizeUnit(DataUnit.BYTES)
161161
private DataSize maxInboundMessageSize = null;
162162

163+
/**
164+
* The maximum size of metadata allowed to be received. If not set ({@code null}) then
165+
* {@link GrpcUtil#DEFAULT_MAX_HEADER_LIST_SIZE gRPC's default} should be used.
166+
*
167+
* @return The maximum metadata size allowed.
168+
*/
169+
@DataSizeUnit(DataUnit.BYTES)
170+
private DataSize maxInboundMetadataSize = null;
171+
163172
/**
164173
* Whether gRPC health service is enabled or not. Defaults to {@code true}.
165174
*
@@ -313,4 +322,24 @@ public void setMaxInboundMessageSize(final DataSize maxInboundMessageSize) {
313322
}
314323
}
315324

325+
/**
326+
* Sets the maximum metadata size allowed to be received by the server. If not set ({@code null}) then it will
327+
* default to {@link GrpcUtil#DEFAULT_MAX_HEADER_LIST_SIZE gRPC's default}. If set to {@code -1} then it will use
328+
* the highest possible limit (not recommended).
329+
*
330+
* @param maxInboundMetadataSize The new maximum size allowed for incoming metadata. {@code -1} for max possible.
331+
* Null to use the gRPC's default.
332+
*
333+
* @see ServerBuilder#maxInboundMetadataSize(int)
334+
*/
335+
public void setMaxInboundMetadataSize(final DataSize maxInboundMetadataSize) {
336+
if (maxInboundMetadataSize == null || maxInboundMetadataSize.toBytes() >= 0) {
337+
this.maxInboundMetadataSize = maxInboundMetadataSize;
338+
} else if (maxInboundMetadataSize.toBytes() == -1) {
339+
this.maxInboundMetadataSize = DataSize.ofBytes(Integer.MAX_VALUE);
340+
} else {
341+
throw new IllegalArgumentException("Unsupported maxInboundMetadataSize: " + maxInboundMetadataSize);
342+
}
343+
}
344+
316345
}

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/serverfactory/AbstractGrpcServerFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ protected void configureLimits(final T builder) {
151151
if (maxInboundMessageSize != null) {
152152
builder.maxInboundMessageSize((int) maxInboundMessageSize.toBytes());
153153
}
154+
final DataSize maxInboundMetadataSize = this.properties.getMaxInboundMetadataSize();
155+
if (maxInboundMetadataSize != null) {
156+
builder.maxInboundMetadataSize((int) maxInboundMetadataSize.toBytes());
157+
}
154158
}
155159

156160
@Override

grpc-server-spring-boot-autoconfigure/src/test/java/net/devh/boot/grpc/server/config/GrpcServerPropertiesGivenUnitTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
@ExtendWith(SpringExtension.class)
3535
@SpringBootTest(properties = {
3636
"grpc.server.keepAliveTime=42m",
37-
"grpc.server.maxInboundMessageSize=5MB"
37+
"grpc.server.maxInboundMessageSize=5MB",
38+
"grpc.server.maxInboundMetadataSize=10KB"
3839
})
3940
class GrpcServerPropertiesGivenUnitTest {
4041

@@ -45,6 +46,7 @@ class GrpcServerPropertiesGivenUnitTest {
4546
void test() {
4647
assertEquals(Duration.ofMinutes(42), this.grpcServerProperties.getKeepAliveTime());
4748
assertEquals(DataSize.ofMegabytes(5), this.grpcServerProperties.getMaxInboundMessageSize());
49+
assertEquals(DataSize.ofKilobytes(10), this.grpcServerProperties.getMaxInboundMetadataSize());
4850
}
4951

5052
}

0 commit comments

Comments
 (0)