fix(etcd): make gRPC max inbound message size configurable
This commit is contained in:
1 parent
5966aea43b
commit
f1c998c9cc
11 files changed
+113
-1
No files matched your search
@@ -87,6 +87,10 @@ public final class EtcdAgent {
|
||||
private static final Gson GSON = new Gson();
|
||||
private static final int DEFAULT_LIMIT = 100;
|
||||
private static final int RPC_TIMEOUT_SECONDS = 30;
|
||||
static final int DEFAULT_GRPC_MAX_INBOUND_MESSAGE_SIZE = 32 * 1024 * 1024;
|
||||
static final int MIN_GRPC_MAX_INBOUND_MESSAGE_SIZE = 1024 * 1024;
|
||||
static final int MAX_GRPC_MAX_INBOUND_MESSAGE_SIZE = 256 * 1024 * 1024;
|
||||
private static final String GRPC_MAX_INBOUND_MESSAGE_SIZE_KEY = "grpc_max_inbound_message_size";
|
||||
private static final int PRESERVE_LEASE_MAX_ATTEMPTS = 3;
|
||||
private static final long HISTORY_DEFAULT_REVISION_WINDOW = 10_000L;
|
||||
private static final List<String> CAPABILITIES = Collections.unmodifiableList(Arrays.asList(
|
||||
@@ -167,7 +171,8 @@ public final class EtcdAgent {
|
||||
List<String> endpoints = endpoints(connection);
|
||||
ClientBuilder builder = Client.builder()
|
||||
.endpoints(endpoints.toArray(String[]::new))
|
||||
.connectTimeout(Duration.ofSeconds(connectTimeoutSeconds(connection)));
|
||||
.connectTimeout(Duration.ofSeconds(connectTimeoutSeconds(connection)))
|
||||
.maxInboundMessageSize(grpcMaxInboundMessageSize(connection));
|
||||
String username = stringOrEmpty(connection, "username");
|
||||
String password = stringOrEmpty(connection, "password");
|
||||
if (!username.isBlank()) {
|
||||
@@ -184,6 +189,19 @@ public final class EtcdAgent {
|
||||
return Math.min(300, Math.max(1, intOrDefault(connection, "connect_timeout_secs", RPC_TIMEOUT_SECONDS)));
|
||||
}
|
||||
|
||||
static int grpcMaxInboundMessageSize(JsonObject connection) {
|
||||
int configured = intOrDefault(
|
||||
connection,
|
||||
GRPC_MAX_INBOUND_MESSAGE_SIZE_KEY,
|
||||
intUrlParamOrDefault(
|
||||
stringOrEmpty(connection, "url_params"),
|
||||
GRPC_MAX_INBOUND_MESSAGE_SIZE_KEY,
|
||||
DEFAULT_GRPC_MAX_INBOUND_MESSAGE_SIZE
|
||||
)
|
||||
);
|
||||
return Math.min(MAX_GRPC_MAX_INBOUND_MESSAGE_SIZE, Math.max(MIN_GRPC_MAX_INBOUND_MESSAGE_SIZE, configured));
|
||||
}
|
||||
|
||||
private static Map<String, Object> validateConnectedClient() throws Exception {
|
||||
EtcdSessionState state = sessionState();
|
||||
Client active = requireClient();
|
||||
@@ -1630,6 +1648,26 @@ public final class EtcdAgent {
|
||||
return element == null || element.isJsonNull() ? fallback : element.getAsInt();
|
||||
}
|
||||
|
||||
private static int intUrlParamOrDefault(String params, String key, int fallback) {
|
||||
if (params == null || params.isBlank()) {
|
||||
return fallback;
|
||||
}
|
||||
for (String entry : params.replaceFirst("^\\?", "").split("&")) {
|
||||
int separator = entry.indexOf('=');
|
||||
String entryKey = separator < 0 ? entry : entry.substring(0, separator);
|
||||
if (!key.equals(entryKey)) {
|
||||
continue;
|
||||
}
|
||||
String value = separator < 0 ? "" : entry.substring(separator + 1);
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException ignored) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private static boolean boolOrDefault(JsonObject object, String key, boolean fallback) {
|
||||
JsonElement element = object.get(key);
|
||||
return element == null || element.isJsonNull() ? fallback : element.getAsBoolean();
|
||||
|
||||
@@ -89,6 +89,41 @@ final class EtcdAgentTest {
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void grpcInboundLimitDefaultsTo32MiBAndUsesSafeBounds() {
|
||||
Assertions.assertEquals(
|
||||
32 * 1024 * 1024,
|
||||
EtcdAgent.grpcMaxInboundMessageSize(new JsonObject())
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
64 * 1024 * 1024,
|
||||
EtcdAgent.grpcMaxInboundMessageSize(
|
||||
JsonParser.parseString("{\"grpc_max_inbound_message_size\":67108864}").getAsJsonObject()
|
||||
)
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
1024 * 1024,
|
||||
EtcdAgent.grpcMaxInboundMessageSize(
|
||||
JsonParser.parseString("{\"grpc_max_inbound_message_size\":0}").getAsJsonObject()
|
||||
)
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
256 * 1024 * 1024,
|
||||
EtcdAgent.grpcMaxInboundMessageSize(
|
||||
JsonParser.parseString("{\"grpc_max_inbound_message_size\":536870912}").getAsJsonObject()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void grpcInboundLimitCanBeConfiguredThroughConnectionUrlParams() {
|
||||
JsonObject connection = JsonParser.parseString(
|
||||
"{\"url_params\":\"foo=bar&grpc_max_inbound_message_size=50331648\"}"
|
||||
).getAsJsonObject();
|
||||
|
||||
Assertions.assertEquals(48 * 1024 * 1024, EtcdAgent.grpcMaxInboundMessageSize(connection));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateConnectionRequiresAnActiveSession() {
|
||||
String response = EtcdAgent.handleRequest(
|
||||
|
||||
Reference in new issue
Block a user