From 4b2d9d4d74d7cd76b090e3065e3148ffe92aec6c Mon Sep 17 00:00:00 2001 From: tjq Date: Mon, 2 Jan 2023 12:47:28 +0800 Subject: [PATCH] feat: optimize remote http impl --- .../tech/powerjob/common/PowerJobDKey.java | 11 +++++++ .../remote/http/vertx/VertxInitializer.java | 32 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/powerjob-common/src/main/java/tech/powerjob/common/PowerJobDKey.java b/powerjob-common/src/main/java/tech/powerjob/common/PowerJobDKey.java index a7dfdb9f..3118076f 100644 --- a/powerjob-common/src/main/java/tech/powerjob/common/PowerJobDKey.java +++ b/powerjob-common/src/main/java/tech/powerjob/common/PowerJobDKey.java @@ -23,6 +23,17 @@ public class PowerJobDKey { */ public static final String IGNORED_NETWORK_INTERFACE_REGEX = "powerjob.network.interface.ignored"; + /** + * Enables compression during data transfer, such as gzip under the HTTP protocol. default value is 'false' + * Note that enabling compression reduces network usage, but increases CPU consumption + */ + public static final String TRANSPORTER_USE_COMPRESSING = "powerjob.transporter.compression.enabled"; + + /** + * keep-alive connection timeout(in seconds), value <= 0 means disable keepalive. default value is 75 + */ + public static final String TRANSPORTER_KEEP_ALIVE_TIMEOUT = "powerjob.transporter.keepalive.timeout"; + public static final String WORKER_STATUS_CHECK_PERIOD = "powerjob.worker.status-check.normal.period"; /** * ms diff --git a/powerjob-remote/powerjob-remote-impl-http/src/main/java/tech/powerjob/remote/http/vertx/VertxInitializer.java b/powerjob-remote/powerjob-remote-impl-http/src/main/java/tech/powerjob/remote/http/vertx/VertxInitializer.java index 711d53ba..602f1df0 100644 --- a/powerjob-remote/powerjob-remote-impl-http/src/main/java/tech/powerjob/remote/http/vertx/VertxInitializer.java +++ b/powerjob-remote/powerjob-remote-impl-http/src/main/java/tech/powerjob/remote/http/vertx/VertxInitializer.java @@ -7,6 +7,9 @@ import io.vertx.core.http.HttpClientOptions; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerOptions; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import tech.powerjob.common.OmsConstant; +import tech.powerjob.common.PowerJobDKey; /** * VertxInitializer @@ -18,6 +21,13 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class VertxInitializer { + /** + * 默认开启长连接,且 75S 超时 + */ + private static final int DEFAULT_KEEP_ALIVE_TIMEOUT = 75; + + private static final int CONNECTION_TIMEOUT_MS = 2000; + public static Vertx buildVertx() { VertxOptions options = new VertxOptions(); log.info("[PowerJob-Vertx] use vertx options: {}", options); @@ -43,7 +53,27 @@ public class VertxInitializer { } public static HttpClient buildHttpClient(Vertx vertx) { - HttpClientOptions httpClientOptions = new HttpClientOptions(); + + HttpClientOptions httpClientOptions = new HttpClientOptions() + .setMetricsName(OmsConstant.PACKAGE) + .setConnectTimeout(CONNECTION_TIMEOUT_MS) + .setMaxPoolSize(Math.max(8, Runtime.getRuntime().availableProcessors()) * 2); + + // 长连接 + String keepaliveTimeout = System.getProperty(PowerJobDKey.TRANSPORTER_KEEP_ALIVE_TIMEOUT, String.valueOf(DEFAULT_KEEP_ALIVE_TIMEOUT)); + int keepaliveTimeoutInt = Integer.parseInt(keepaliveTimeout); + if (keepaliveTimeoutInt > 0) { + httpClientOptions.setKeepAlive(true).setKeepAliveTimeout(keepaliveTimeoutInt); + } else { + httpClientOptions.setKeepAlive(false); + } + + // 压缩判定 + String enableCompressing = System.getProperty(PowerJobDKey.TRANSPORTER_USE_COMPRESSING); + if (StringUtils.isNotEmpty(enableCompressing)) { + httpClientOptions.setTryUseCompression(StringUtils.equalsIgnoreCase(enableCompressing, Boolean.TRUE.toString())); + } + log.info("[PowerJob-Vertx] use HttpClientOptions: {}", httpClientOptions.toJson()); return vertx.createHttpClient(httpClientOptions); }