This is an automated email from the ASF dual-hosted git repository.
yui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 5801f9b [type: refactor] Minor code optimizations (#3086)
5801f9b is described below
commit 5801f9b24b6f82e1159659b1c5a2d2408d04a5b8
Author: xiaoyu <[email protected]>
AuthorDate: Tue Mar 22 16:32:25 2022 +0800
[type: refactor] Minor code optimizations (#3086)
---
shenyu-bootstrap/pom.xml | 7 +++----
.../plugin/httpclient/config/HttpClientProperties.java | 2 +-
.../apache/shenyu/plugin/logging/AbstractLogCollector.java | 14 +++++---------
.../org/apache/shenyu/plugin/logging/LogConsumeClient.java | 5 +----
.../org/apache/shenyu/plugin/logging/body/BodyWriter.java | 2 +-
.../plugin/logging/body/LoggingServerHttpRequest.java | 1 +
.../shenyu/plugin/logging/constant/LoggingConstant.java | 1 -
.../plugin/logging/rocketmq/RocketMQLogCollectClient.java | 3 +--
.../apache/shenyu/plugin/metrics/constant/LabelNames.java | 2 +-
.../metrics/prometheus/PrometheusMetricsService.java | 7 +++----
.../starter/plugin/metrics/boot/MetricsRunner.java | 6 ++----
11 files changed, 19 insertions(+), 31 deletions(-)
diff --git a/shenyu-bootstrap/pom.xml b/shenyu-bootstrap/pom.xml
index 9cad6c6..8b01001 100644
--- a/shenyu-bootstrap/pom.xml
+++ b/shenyu-bootstrap/pom.xml
@@ -185,15 +185,14 @@
<version>${project.version}</version>
</dependency>
<!--shenyu websocket plugin end-->
-
-
- <!--shenyu websocket plugin start-->
+
+ <!--shenyu metrics plugin start-->
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-spring-boot-starter-plugin-metrics</artifactId>
<version>${project.version}</version>
</dependency>
- <!--shenyu websocket plugin end-->
+ <!--shenyu metrics plugin end-->
<!--shenyu grpc plugin start-->
<dependency>
diff --git
a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
index 04160b9..588b4d0 100644
---
a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
+++
b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
@@ -52,7 +52,7 @@ public class HttpClientProperties {
private String strategy;
/**
- * The connect timeout in millis, the default is 45s.
+ * The connection timeout in millis, the default is 45s.
*/
private Integer connectTimeout = 45000;
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/AbstractLogCollector.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/AbstractLogCollector.java
index e70c76e..c377823 100644
---
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/AbstractLogCollector.java
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/AbstractLogCollector.java
@@ -38,15 +38,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
public abstract class AbstractLogCollector implements LogCollector {
private static final Logger LOG =
LoggerFactory.getLogger(AbstractLogCollector.class);
-
- private final ExecutorService threadExecutor =
Executors.newSingleThreadExecutor();
-
+
private final int bufferSize;
-
- private final int batchSize = 100;
-
- private final int diffTimeMSForPush = 100;
-
+
private final BlockingQueue<ShenyuRequestLog> bufferQueue;
private long lastPushTime;
@@ -60,6 +54,7 @@ public abstract class AbstractLogCollector implements
LogCollector {
bufferSize =
LogCollectConfigUtils.getGlobalLogConfig().getBufferQueueSize();
bufferQueue = new LinkedBlockingDeque<>(bufferSize);
if (logCollectClient != null) {
+ ExecutorService threadExecutor =
Executors.newSingleThreadExecutor();
threadExecutor.execute(this::consume);
}
}
@@ -79,11 +74,13 @@ public abstract class AbstractLogCollector implements
LogCollector {
*/
private void consume() {
while (started.get()) {
+ int diffTimeMSForPush = 100;
try {
List<ShenyuRequestLog> logs = new ArrayList<>();
int size = bufferQueue.size();
long time = System.currentTimeMillis();
long timeDiffMs = time - lastPushTime;
+ int batchSize = 100;
if (size >= batchSize || timeDiffMs > diffTimeMSForPush) {
bufferQueue.drainTo(logs, batchSize);
logCollectClient.consume(logs);
@@ -105,5 +102,4 @@ public abstract class AbstractLogCollector implements
LogCollector {
logCollectClient.close();
}
}
-
}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/LogConsumeClient.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/LogConsumeClient.java
index c009a51..51eeb0e 100644
---
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/LogConsumeClient.java
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/LogConsumeClient.java
@@ -24,10 +24,8 @@ import java.util.List;
/**
* Used to collect logs, which can be stored in remote or local files or
databases, or others.
*/
-
public interface LogConsumeClient extends AutoCloseable {
-
-
+
/**
* collect logs.
*
@@ -35,5 +33,4 @@ public interface LogConsumeClient extends AutoCloseable {
* @throws Exception produce exception
*/
void consume(List<ShenyuRequestLog> logs) throws Exception;
-
}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/body/BodyWriter.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/body/BodyWriter.java
index 20af2be..cba3ac7 100644
---
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/body/BodyWriter.java
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/body/BodyWriter.java
@@ -82,7 +82,7 @@ public class BodyWriter {
* @return string of stream
*/
public String output() {
- if (size() == 0) {
+ if (isEmpty()) {
return "";
}
try {
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/body/LoggingServerHttpRequest.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/body/LoggingServerHttpRequest.java
index 253c0c1..6ae212c 100644
---
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/body/LoggingServerHttpRequest.java
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/body/LoggingServerHttpRequest.java
@@ -30,6 +30,7 @@ import reactor.util.annotation.NonNull;
* decorate ServerHttpRequest for read body.
*/
public class LoggingServerHttpRequest extends ServerHttpRequestDecorator {
+
private final ShenyuRequestLog logInfo;
public LoggingServerHttpRequest(final ServerHttpRequest delegate, final
ShenyuRequestLog logInfo) {
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/constant/LoggingConstant.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/constant/LoggingConstant.java
index a312be3..93dc28c 100644
---
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/constant/LoggingConstant.java
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/constant/LoggingConstant.java
@@ -29,6 +29,5 @@ public final class LoggingConstant {
public static final String PRODUCER_GROUP = "producerGroup";
public static final String SHENYU_AGENT_TRACE_ID = "shenyu-agent-trace-id";
-
}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/rocketmq/RocketMQLogCollectClient.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/rocketmq/RocketMQLogCollectClient.java
index 783e478..86564dd 100644
---
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/rocketmq/RocketMQLogCollectClient.java
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/main/java/org/apache/shenyu/plugin/logging/rocketmq/RocketMQLogCollectClient.java
@@ -86,10 +86,9 @@ public class RocketMQLogCollectClient implements
LogConsumeClient {
* store logs.
*
* @param logs list of log
- * @throws Exception produce exception
*/
@Override
- public void consume(final List<ShenyuRequestLog> logs) throws Exception {
+ public void consume(final List<ShenyuRequestLog> logs) {
if (CollectionUtils.isEmpty(logs)) {
return;
}
diff --git
a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/constant/LabelNames.java
b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/constant/LabelNames.java
index 0cca428..56b857a 100644
---
a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/constant/LabelNames.java
+++
b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/constant/LabelNames.java
@@ -30,7 +30,7 @@ public final class LabelNames {
/**
* The constant REQUEST_TYPE_TOTAL.
*/
- public static final String REQUEST_TYPE_TOTAL = "request_type_total";
+ public static final String REQUEST_TYPE_TOTAL =
"shenyu_request_type_total";
/**
* The constant REQUEST_THROW_TOTAL.
diff --git
a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/prometheus/PrometheusMetricsService.java
b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/prometheus/PrometheusMetricsService.java
index d64eb46..5fb51d8 100644
---
a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/prometheus/PrometheusMetricsService.java
+++
b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/prometheus/PrometheusMetricsService.java
@@ -32,7 +32,7 @@ import org.slf4j.LoggerFactory;
import javax.management.MalformedObjectNameException;
import java.io.IOException;
import java.net.InetSocketAddress;
-import java.util.Objects;
+import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
/**
@@ -54,11 +54,11 @@ public final class PrometheusMetricsService implements
MetricsService {
@Override
public void stop() {
- if (Objects.nonNull(server)) {
+ Optional.ofNullable(server).ifPresent(server -> {
server.stop();
registered.set(false);
CollectorRegistry.defaultRegistry.clear();
- }
+ });
}
/**
@@ -101,7 +101,6 @@ public final class PrometheusMetricsService implements
MetricsService {
if (!registered.compareAndSet(false, true)) {
return;
}
-
String jvmEnabled =
String.valueOf(config.getProps().getProperty("jvm_enabled"));
if (StringUtils.isNotEmpty(jvmEnabled)) {
boolean enabled = Boolean.parseBoolean(jvmEnabled);
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-metrics/src/main/java/org/apache/shenyu/springboot/starter/plugin/metrics/boot/MetricsRunner.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-metrics/src/main/java/org/apache/shenyu/springboot/starter/plugin/metrics/boot/MetricsRunner.java
index ccbe28e..ed351af 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-metrics/src/main/java/org/apache/shenyu/springboot/starter/plugin/metrics/boot/MetricsRunner.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-metrics/src/main/java/org/apache/shenyu/springboot/starter/plugin/metrics/boot/MetricsRunner.java
@@ -28,7 +28,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.CommandLineRunner;
-import java.util.Objects;
+import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
/**
@@ -72,8 +72,6 @@ public class MetricsRunner implements CommandLineRunner,
DisposableBean {
@Override
public void destroy() {
this.isStarted.compareAndSet(true, false);
- if (Objects.nonNull(metricsService)) {
- metricsService.stop();
- }
+ Optional.ofNullable(metricsService).ifPresent(MetricsService::stop);
}
}