From a8cd9d916a5a014a8f75dc93317dc113661f6791 Mon Sep 17 00:00:00 2001 From: wulang Date: Fri, 11 Nov 2022 20:51:09 +0800 Subject: [PATCH] feat:Long polling returns status code --- .../cn/hippo4j/common/toolkit/NumberUtil.java | 105 ------------------ .../config/service/LongPollingService.java | 12 +- .../springboot/starter/core/ClientWorker.java | 17 ++- 3 files changed, 17 insertions(+), 117 deletions(-) delete mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/NumberUtil.java diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/NumberUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/NumberUtil.java deleted file mode 100644 index 88e2b440..00000000 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/NumberUtil.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package cn.hippo4j.common.toolkit; - -/** - * number util - */ -public class NumberUtil { - public static boolean isNumber(CharSequence str) { - if (StringUtil.isBlank(str)) { - return false; - } - char[] chars = str.toString().toCharArray(); - int sz = chars.length; - boolean hasExp = false; - boolean hasDecPoint = false; - boolean allowSigns = false; - boolean foundDigit = false; - // deal with any possible sign up front - int start = (chars[0] == '-' || chars[0] == '+') ? 1 : 0; - if (sz > start + 1) { - if (chars[start] == '0' && (chars[start + 1] == 'x' || chars[start + 1] == 'X')) { - int i = start + 2; - if (i == sz) { - return false; - } - for (; i < chars.length; i++) { - if ((chars[i] < '0' || chars[i] > '9') && (chars[i] < 'a' || chars[i] > 'f') && (chars[i] < 'A' || chars[i] > 'F')) { - return false; - } - } - return true; - } - } - sz--; - int i = start; - while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) { - if (chars[i] >= '0' && chars[i] <= '9') { - foundDigit = true; - allowSigns = false; - - } else if (chars[i] == '.') { - if (hasDecPoint || hasExp) { - return false; - } - hasDecPoint = true; - } else if (chars[i] == 'e' || chars[i] == 'E') { - if (hasExp) { - return false; - } - if (!foundDigit) { - return false; - } - hasExp = true; - allowSigns = true; - } else if (chars[i] == '+' || chars[i] == '-') { - if (!allowSigns) { - return false; - } - allowSigns = false; - foundDigit = false; - } else { - return false; - } - i++; - } - if (i < chars.length) { - if (chars[i] >= '0' && chars[i] <= '9') { - return true; - } - if (chars[i] == 'e' || chars[i] == 'E') { - return false; - } - if (chars[i] == '.') { - if (hasDecPoint || hasExp) { - return false; - } - return foundDigit; - } - if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) { - return foundDigit; - } - if (chars[i] == 'l' || chars[i] == 'L') { - return foundDigit && !hasExp; - } - return false; - } - return !allowSigns && foundDigit; - } -} diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java index 56a37f13..9a5fe574 100644 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java +++ b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java @@ -62,8 +62,6 @@ public class LongPollingService { private final Map retainIps = new ConcurrentHashMap<>(); - private static final int LOW_VERSION = 143; - public LongPollingService() { allSubs = new ConcurrentLinkedQueue<>(); ConfigExecutor.scheduleLongPolling(new StatTask(), 0L, 30L, TimeUnit.SECONDS); @@ -195,7 +193,7 @@ public class LongPollingService { this.probeRequestSize = probeRequestSize; this.timeoutTime = timeout; this.appName = appInfo.getLeft(); - this.appVersion = StringUtil.isNotBlank(appInfo.getRight()) ? appInfo.getRight().replace(".", "") : ""; + this.appVersion = appInfo.getRight(); this.createTime = System.currentTimeMillis(); } @@ -243,14 +241,10 @@ public class LongPollingService { private void generateResponse(List changedGroups) { HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse(); if (CollectionUtil.isEmpty(changedGroups)) { - if (StringUtil.isBlank(appVersion) || !NumberUtil.isNumber(appVersion)) { + if (StringUtil.isBlank(appVersion)) { response.setStatus(HttpServletResponse.SC_OK); } else { - if (Integer.parseInt(appVersion) < LOW_VERSION) { - response.setStatus(HttpServletResponse.SC_OK); - } else { - response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - } + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } // Tell web container to send http response. asyncContext.complete(); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientWorker.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientWorker.java index 5aa89370..570077f7 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientWorker.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientWorker.java @@ -22,7 +22,6 @@ import cn.hippo4j.common.toolkit.ContentUtil; import cn.hippo4j.common.toolkit.GroupKey; import cn.hippo4j.common.toolkit.IdUtil; import cn.hippo4j.common.toolkit.JSONUtil; -import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; import cn.hippo4j.springboot.starter.remote.HttpAgent; @@ -39,7 +38,19 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import static cn.hippo4j.common.constant.Constants.*; +import static cn.hippo4j.common.constant.Constants.CLIENT_VERSION; +import static cn.hippo4j.common.constant.Constants.CONFIG_CONTROLLER_PATH; +import static cn.hippo4j.common.constant.Constants.CONFIG_LONG_POLL_TIMEOUT; +import static cn.hippo4j.common.constant.Constants.GROUP_KEY_DELIMITER_TRANSLATION; +import static cn.hippo4j.common.constant.Constants.LINE_SEPARATOR; +import static cn.hippo4j.common.constant.Constants.LISTENER_PATH; +import static cn.hippo4j.common.constant.Constants.LONG_PULLING_CLIENT_IDENTIFICATION; +import static cn.hippo4j.common.constant.Constants.LONG_PULLING_TIMEOUT; +import static cn.hippo4j.common.constant.Constants.LONG_PULLING_TIMEOUT_NO_HANGUP; +import static cn.hippo4j.common.constant.Constants.NULL; +import static cn.hippo4j.common.constant.Constants.PROBE_MODIFY_REQUEST; +import static cn.hippo4j.common.constant.Constants.WEIGHT_CONFIGS; +import static cn.hippo4j.common.constant.Constants.WORD_SEPARATOR; /** * Client worker. @@ -70,7 +81,7 @@ public class ClientWorker { this.agent = httpAgent; this.identify = identify; this.timeout = CONFIG_LONG_POLL_TIMEOUT; - this.version = StringUtil.isNotEmpty(version) ? version.replace("-SNAPSHOT", "") : ""; + this.version = version; this.serverHealthCheck = serverHealthCheck; ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, runnable -> { Thread thread = new Thread(runnable);