From 594a1290db0e08d2e1f368557ec8dda4f061687e Mon Sep 17 00:00:00 2001 From: Volodymyr Babak Date: Fri, 26 Feb 2021 09:24:10 +0200 Subject: [PATCH] Async timeout connector customizer. Refactoring --- .../service/queue/TbCoreConsumerStats.java | 39 +++++++------------ .../transport/http/HttpTransportContext.java | 14 +++++++ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/queue/TbCoreConsumerStats.java b/application/src/main/java/org/thingsboard/server/service/queue/TbCoreConsumerStats.java index d342b4b565..bca5a90fa8 100644 --- a/application/src/main/java/org/thingsboard/server/service/queue/TbCoreConsumerStats.java +++ b/application/src/main/java/org/thingsboard/server/service/queue/TbCoreConsumerStats.java @@ -55,31 +55,22 @@ public class TbCoreConsumerStats { public TbCoreConsumerStats(StatsFactory statsFactory) { String statsKey = StatsType.CORE.getName(); - this.totalCounter = statsFactory.createStatsCounter(statsKey, TOTAL_MSGS); - this.sessionEventCounter = statsFactory.createStatsCounter(statsKey, SESSION_EVENTS); - this.getAttributesCounter = statsFactory.createStatsCounter(statsKey, GET_ATTRIBUTE); - this.subscribeToAttributesCounter = statsFactory.createStatsCounter(statsKey, ATTRIBUTE_SUBSCRIBES); - this.subscribeToRPCCounter = statsFactory.createStatsCounter(statsKey, RPC_SUBSCRIBES); - this.toDeviceRPCCallResponseCounter = statsFactory.createStatsCounter(statsKey, TO_DEVICE_RPC_CALL_RESPONSES); - this.subscriptionInfoCounter = statsFactory.createStatsCounter(statsKey, SUBSCRIPTION_INFO); - this.claimDeviceCounter = statsFactory.createStatsCounter(statsKey, DEVICE_CLAIMS); - this.deviceStateCounter = statsFactory.createStatsCounter(statsKey, DEVICE_STATES); - this.subscriptionMsgCounter = statsFactory.createStatsCounter(statsKey, SUBSCRIPTION_MSGS); - this.toCoreNotificationsCounter = statsFactory.createStatsCounter(statsKey, TO_CORE_NOTIFICATIONS); - - - counters.add(totalCounter); - counters.add(sessionEventCounter); - counters.add(getAttributesCounter); - counters.add(subscribeToAttributesCounter); - counters.add(subscribeToRPCCounter); - counters.add(toDeviceRPCCallResponseCounter); - counters.add(subscriptionInfoCounter); - counters.add(claimDeviceCounter); + this.totalCounter = register(statsFactory.createStatsCounter(statsKey, TOTAL_MSGS)); + this.sessionEventCounter = register(statsFactory.createStatsCounter(statsKey, SESSION_EVENTS)); + this.getAttributesCounter = register(statsFactory.createStatsCounter(statsKey, GET_ATTRIBUTE)); + this.subscribeToAttributesCounter = register(statsFactory.createStatsCounter(statsKey, ATTRIBUTE_SUBSCRIBES)); + this.subscribeToRPCCounter = register(statsFactory.createStatsCounter(statsKey, RPC_SUBSCRIBES)); + this.toDeviceRPCCallResponseCounter = register(statsFactory.createStatsCounter(statsKey, TO_DEVICE_RPC_CALL_RESPONSES)); + this.subscriptionInfoCounter = register(statsFactory.createStatsCounter(statsKey, SUBSCRIPTION_INFO)); + this.claimDeviceCounter = register(statsFactory.createStatsCounter(statsKey, DEVICE_CLAIMS)); + this.deviceStateCounter = register(statsFactory.createStatsCounter(statsKey, DEVICE_STATES)); + this.subscriptionMsgCounter = register(statsFactory.createStatsCounter(statsKey, SUBSCRIPTION_MSGS)); + this.toCoreNotificationsCounter = register(statsFactory.createStatsCounter(statsKey, TO_CORE_NOTIFICATIONS)); + } - counters.add(deviceStateCounter); - counters.add(subscriptionMsgCounter); - counters.add(toCoreNotificationsCounter); + private StatsCounter register(StatsCounter counter){ + counters.add(counter); + return counter; } public void log(TransportProtos.TransportToDeviceActorMsg msg) { diff --git a/common/transport/http/src/main/java/org/thingsboard/server/transport/http/HttpTransportContext.java b/common/transport/http/src/main/java/org/thingsboard/server/transport/http/HttpTransportContext.java index aad01c7c2f..fea07daffc 100644 --- a/common/transport/http/src/main/java/org/thingsboard/server/transport/http/HttpTransportContext.java +++ b/common/transport/http/src/main/java/org/thingsboard/server/transport/http/HttpTransportContext.java @@ -17,9 +17,13 @@ package org.thingsboard.server.transport.http; import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import org.apache.coyote.ProtocolHandler; +import org.apache.coyote.http11.Http11NioProtocol; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; +import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import org.thingsboard.server.common.transport.TransportContext; @@ -37,4 +41,14 @@ public class HttpTransportContext extends TransportContext { @Value("${transport.http.request_timeout}") private long defaultTimeout; + @Bean + public TomcatConnectorCustomizer tomcatAsyncTimeoutConnectorCustomizer() { + return connector -> { + ProtocolHandler handler = connector.getProtocolHandler(); + if (handler instanceof Http11NioProtocol) { + log.trace("Setting async timeout {}", defaultTimeout); + connector.setAsyncTimeout(defaultTimeout); + } + }; + } }