Skip to content

Commit 697c336

Browse files
committed
feat(gax-grpc): add cap of 25 to maxResizeDelta and test
1 parent f62aa0c commit 697c336

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

sdk-platform-java/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/ChannelPoolSettings.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public abstract class ChannelPoolSettings {
6161
/** The maximum number of channels that can be added or removed at a time. */
6262
static final int DEFAULT_MAX_RESIZE_DELTA = 2;
6363

64+
// Arbitrary limit to prevent unbounded growth and protect server/client resources.
65+
// Capping at 25 ensures we don't scale too aggressively in a single cycle.
66+
private static final int MAX_ALLOWED_RESIZE_DELTA = 25;
67+
6468
/**
6569
* Threshold to start scaling down the channel pool.
6670
*
@@ -100,6 +104,8 @@ public abstract class ChannelPoolSettings {
100104
* the pool better handle sudden bursts or spikes in requests by allowing it to scale up faster.
101105
* Regardless of this setting, the number of channels will never exceed {@link
102106
* #getMaxChannelCount()}.
107+
*
108+
* <p><b>Note:</b> This value cannot exceed {@value #MAX_ALLOWED_RESIZE_DELTA}.
103109
*/
104110
public abstract int getMaxResizeDelta();
105111

@@ -193,6 +199,9 @@ public ChannelPoolSettings build() {
193199
s.getInitialChannelCount() > 0, "Initial channel count must be greater than 0");
194200
Preconditions.checkState(
195201
s.getMaxResizeDelta() > 0, "Max resize delta must be greater than 0");
202+
Preconditions.checkState(
203+
s.getMaxResizeDelta() <= MAX_ALLOWED_RESIZE_DELTA,
204+
"Max resize delta cannot be greater than " + MAX_ALLOWED_RESIZE_DELTA);
196205
Preconditions.checkState(
197206
s.getMaxResizeDelta() <= s.getMaxChannelCount(),
198207
"Max resize delta cannot be greater than max channel count");

sdk-platform-java/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/ChannelPoolTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,4 +899,12 @@ void testDoubleRelease() throws Exception {
899899
ChannelPool.LOG.removeHandler(logHandler);
900900
}
901901
}
902+
903+
@Test
904+
void settingsValidationFailsWhenMaxResizeDeltaExceedsLimit() {
905+
ChannelPoolSettings.Builder builder =
906+
ChannelPoolSettings.builder().setMaxResizeDelta(26).setMaxChannelCount(30);
907+
org.junit.jupiter.api.Assertions.assertThrows(
908+
IllegalStateException.class, () -> builder.build());
909+
}
902910
}

0 commit comments

Comments
 (0)