From a44999b7eec09380e72677f5b8c2ed954174c3d4 Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Wed, 6 Nov 2024 17:50:57 +0800 Subject: [PATCH 01/11] Update MxController.java --- .../main/java/org/rx/spring/MxController.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/rxlib/src/main/java/org/rx/spring/MxController.java b/rxlib/src/main/java/org/rx/spring/MxController.java index fa53c4ef..a6b95f05 100644 --- a/rxlib/src/main/java/org/rx/spring/MxController.java +++ b/rxlib/src/main/java/org/rx/spring/MxController.java @@ -15,6 +15,7 @@ import org.rx.io.IOStream; import org.rx.net.NetEventWait; import org.rx.net.Sockets; +import org.rx.net.http.HttpClient; import org.rx.net.socks.SocksContext; import org.rx.util.BeanMapFlag; import org.rx.util.BeanMapper; @@ -28,6 +29,7 @@ import javax.annotation.PostConstruct; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.lang.management.ManagementFactory; import java.lang.reflect.Method; import java.net.InetAddress; @@ -138,15 +140,19 @@ public Object health(HttpServletRequest request) { } } - // @PostMapping("directOffer") -// public void directOffer(String appName, String socksId, String endpoint, MultipartFile binary) { -// SendPack pack = new SendPack(appName, socksId, Sockets.parseEndpoint(endpoint)); -// pack.setBinary(binary); -// server.frontendOffer(pack); -// } -// + @RequestMapping("health/p") + public void healthP(HttpServletRequest request, HttpServletResponse response) { + String fu = request.getParameter("fu"); + Integer tm = Reflects.convertQuietly(request.getParameter("tm"), Integer.class); + HttpClient client = new HttpClient(); + if (tm != null) { + client.withTimeoutMillis(tm); + } + client.forward(request, response, fu); + } + // @SneakyThrows -// @PostMapping("directPoll") +// @PostMapping("directPoll/Offer") // public void directPoll(String appName, String socksId, String endpoint, HttpServletResponse response) { // ReceivePack pack = server.frontendPoll(new SendPack(appName, socksId, Sockets.parseEndpoint(endpoint))); // ServletOutputStream out = response.getOutputStream(); From 6f93a7d62c510be0ad35533fa1dff5b9398cbd8f Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Wed, 13 Nov 2024 16:24:01 +0800 Subject: [PATCH 02/11] Update Validator.java --- .../src/main/java/org/rx/util/Validator.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/rxlib/src/main/java/org/rx/util/Validator.java b/rxlib/src/main/java/org/rx/util/Validator.java index fa460295..7a7967e0 100644 --- a/rxlib/src/main/java/org/rx/util/Validator.java +++ b/rxlib/src/main/java/org/rx/util/Validator.java @@ -1,8 +1,14 @@ package org.rx.util; +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; import org.rx.annotation.ValidRegex; +import org.rx.core.Arrays; import org.rx.core.Linq; +import org.rx.core.Reflects; import org.rx.util.function.Func; +import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl; +import sun.reflect.generics.reflectiveObjects.TypeVariableImpl; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; @@ -10,11 +16,14 @@ import javax.validation.Validation; import javax.validation.executable.ExecutableValidator; import java.lang.reflect.Constructor; +import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.lang.reflect.Type; import java.util.Collections; import java.util.Set; import java.util.regex.Pattern; +import static org.rx.core.Extends.as; import static org.rx.core.Extends.ifNull; /** @@ -134,4 +143,74 @@ public static void validateMethod(Method member, Object instance, Object[] param doThrow(violation); } } + +// void append(JSONObject j, Type type, Type[] typeArguments) { +// ParameterizedTypeImpl pt = as(type, ParameterizedTypeImpl.class); +// if (pt != null) { +// Type[] ats = ifNull(typeArguments, pt.getActualTypeArguments()); +// int atsOffset = 0; +// for (Field field : Reflects.getFieldMap(pt.getRawType()).values()) { +// if (field.getName().equals("serialVersionUID")) { +// continue; +// } +// ParameterizedTypeImpl gpt = as(field.getGenericType(), ParameterizedTypeImpl.class); +// if (gpt != null) { +// JSONObject n = new JSONObject(); +// if (Iterable.class.isAssignableFrom(gpt.getRawType())) { +// JSONArray arr = new JSONArray(); +// arr.add(n); +// j.put(field.getName(), arr); +// } else { +// j.put(field.getName(), n); +// } +// +// Type[] atas = gpt.getActualTypeArguments(); +// if (atas[0] instanceof TypeVariableImpl) { +// append(n, ats[atsOffset], Arrays.subarray(atas, atsOffset, atsOffset += atas.length)); +// } else { +// append(n, ats[atsOffset++], null); +// } +// continue; +// } +// +// j.put(field.getName(), getDesc(field)); +// } +// return; +// } +// +// Class clz = (Class) type; +// Type[] ats = typeArguments; +// int atsOffset = 0; +// for (Field field : Reflects.getFieldMap(clz).values()) { +// if (field.getName().equals("serialVersionUID")) { +// continue; +// } +// ParameterizedTypeImpl gpt = as(field.getGenericType(), ParameterizedTypeImpl.class); +// if (gpt != null) { +// JSONObject n = new JSONObject(); +// if (Iterable.class.isAssignableFrom(gpt.getRawType())) { +// JSONArray arr = new JSONArray(); +// arr.add(n); +// j.put(field.getName(), arr); +// } else { +// j.put(field.getName(), n); +// } +// +// Type[] atas = gpt.getActualTypeArguments(); +// if (atas[0] instanceof TypeVariableImpl && ats != null) { +// append(n, ats[atsOffset], Arrays.subarray(atas, atsOffset, atsOffset += atas.length)); +// } else { +// append(n, atas[0], null); +// } +// continue; +// } +// +// j.put(field.getName(), getDesc(field)); +// } +// } +// +// String getDesc(Field field) { +// ApiModelProperty attr = field.getAnnotation(ApiModelProperty.class); +// return field.getType().getSimpleName() + (attr == null ? "" : " " + attr.value()); +// } } From ec1067f78649801f026ea541b637648690ee8802 Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:46:06 +0800 Subject: [PATCH 03/11] Update JdbcUtil.java --- rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java b/rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java index b40d912b..35e7a9bb 100644 --- a/rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java +++ b/rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java @@ -205,6 +205,16 @@ public static void print(ResultSet resultSet) { } } + @SneakyThrows + public static T executeScalar(ResultSet resultSet) { + try (ResultSet rs = resultSet) { + if (rs.next()) { + return (T) rs.getObject(1); + } + return null; + } + } + public static final BiFunc TO_CAMEL_COLUMN_MAPPING = p -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, p); public static List readAs(ResultSet resultSet, Type type) { From b17b381ba8dc2480937d04a3ca06875a193a04eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=84=9E?= Date: Sat, 23 Nov 2024 19:59:54 +0800 Subject: [PATCH 04/11] Update MxController.java --- .../src/main/java/org/rx/spring/MxController.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rxlib/src/main/java/org/rx/spring/MxController.java b/rxlib/src/main/java/org/rx/spring/MxController.java index a6b95f05..ebf3add9 100644 --- a/rxlib/src/main/java/org/rx/spring/MxController.java +++ b/rxlib/src/main/java/org/rx/spring/MxController.java @@ -141,7 +141,10 @@ public Object health(HttpServletRequest request) { } @RequestMapping("health/p") - public void healthP(HttpServletRequest request, HttpServletResponse response) { + public Object healthP(HttpServletRequest request, HttpServletResponse response) { + if ("1".equals(request.getParameter("mock"))) { + return 1; + } String fu = request.getParameter("fu"); Integer tm = Reflects.convertQuietly(request.getParameter("tm"), Integer.class); HttpClient client = new HttpClient(); @@ -149,6 +152,7 @@ public void healthP(HttpServletRequest request, HttpServletResponse response) { client.withTimeoutMillis(tm); } client.forward(request, response, fu); + return null; } // @SneakyThrows @@ -206,9 +210,7 @@ Map findTopUsage(Date begin, Date end) { return result; } - Map queryTraces(Date startTime, Date endTime, String level, String keyword, Boolean newest, - Boolean methodOccurMost, String methodNamePrefix, String metricsName, - Integer take) { + Map queryTraces(Date startTime, Date endTime, String level, String keyword, Boolean newest, Boolean methodOccurMost, String methodNamePrefix, String metricsName, Integer take) { Map result = new LinkedHashMap<>(3); ExceptionLevel el = null; if (!Strings.isBlank(level)) { @@ -281,9 +283,7 @@ Map svrState(HttpServletRequest request) { j.put("rxConfig", RxConfig.INSTANCE); j.put("requestHeaders", Linq.from(Collections.list(request.getHeaderNames())).select(p -> String.format("%s: %s", p, String.join("; ", Collections.list(request.getHeaders(p)))))); - j.putAll(queryTraces(null, null, null, null, null, - null, null, null, - take)); + j.putAll(queryTraces(null, null, null, null, null, null, null, null, take)); return j; } From d64cddf3a7cb62c7dd13650c31aefd6b58e9eed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=84=9E?= Date: Sat, 23 Nov 2024 20:59:36 +0800 Subject: [PATCH 05/11] Update HttpClient.java --- rxlib/src/main/java/org/rx/net/http/HttpClient.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rxlib/src/main/java/org/rx/net/http/HttpClient.java b/rxlib/src/main/java/org/rx/net/http/HttpClient.java index fa1862fe..db03acd4 100644 --- a/rxlib/src/main/java/org/rx/net/http/HttpClient.java +++ b/rxlib/src/main/java/org/rx/net/http/HttpClient.java @@ -638,9 +638,9 @@ public RequestBody toBody() { } //todo get request body -// boolean isGet = Strings.equalsIgnoreCase(servletRequest.getMethod(), HttpMethod.GET.name()); -// ResponseContent resContent = new ResponseContent(getClient().newCall(createRequest(forwardUrl).method(servletRequest.getMethod(), isGet ? null : reqContent.toBody()).build()).execute()); - ResponseContent resContent = new ResponseContent(getClient().newCall(createRequest(forwardUrl).method(servletRequest.getMethod(), reqContent.toBody()).build()).execute()); + boolean isGet = Strings.equalsIgnoreCase(servletRequest.getMethod(), HttpMethod.GET.name()); + ResponseContent resContent = new ResponseContent(getClient().newCall(createRequest(forwardUrl).method(servletRequest.getMethod(), isGet ? null : reqContent.toBody()).build()).execute()); +// ResponseContent resContent = new ResponseContent(getClient().newCall(createRequest(forwardUrl).method(servletRequest.getMethod(), reqContent.toBody()).build()).execute()); resContent.cachingStream = (featureFlags & CACHING_STREAM_FLAG) == CACHING_STREAM_FLAG; servletResponse.setStatus(resContent.response.code()); for (Pair header : resContent.responseHeaders()) { From dcc8fafc01f5468cc3b27291582134b4fdae1558 Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Thu, 28 Nov 2024 15:16:37 +0800 Subject: [PATCH 06/11] 1 --- rxlib/src/main/java/org/rx/bean/Tuple.java | 8 ++++---- rxlib/src/main/java/org/rx/io/CompositeLock.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/rxlib/src/main/java/org/rx/bean/Tuple.java b/rxlib/src/main/java/org/rx/bean/Tuple.java index 417e0bcc..0dda65c0 100644 --- a/rxlib/src/main/java/org/rx/bean/Tuple.java +++ b/rxlib/src/main/java/org/rx/bean/Tuple.java @@ -23,10 +23,10 @@ public static Tuple of(T1 t1, T2 t2) { return new Tuple<>(t1, t2); } + public static Map.Entry toMapEntry(T1 t1, T2 t2) { + return new AbstractMap.SimpleImmutableEntry<>(t1, t2); + } + public T1 left; public T2 right; - - public Map.Entry toMapEntry() { - return new AbstractMap.SimpleImmutableEntry<>(left, right); - } } diff --git a/rxlib/src/main/java/org/rx/io/CompositeLock.java b/rxlib/src/main/java/org/rx/io/CompositeLock.java index 66e79e59..e7e8606f 100644 --- a/rxlib/src/main/java/org/rx/io/CompositeLock.java +++ b/rxlib/src/main/java/org/rx/io/CompositeLock.java @@ -32,7 +32,7 @@ enum Flags implements NEnum { private final FileStream owner; private final FlagsEnum flags; - // private final ConcurrentHashMap> rwLocks = new ConcurrentHashMap<>(); + //同一时间有锁就行 final Map rwLocks = Collections.synchronizedMap(new WeakHashMap<>()); @SneakyThrows From 9fce94a921e5f83a5506096df67e080fc40e878a Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Thu, 28 Nov 2024 15:54:41 +0800 Subject: [PATCH 07/11] 2 --- .../main/java/org/rx/spring/BeanRegister.java | 2 +- ...dentityMap.java => ConcurrentWeakMap.java} | 54 +++++++++---------- .../src/main/java/org/rx/bean/DataTable.java | 1 - .../main/java/org/rx/core/CpuWatchman.java | 5 +- rxlib/src/main/java/org/rx/core/IOC.java | 38 ++++++++++--- rxlib/src/main/java/org/rx/core/Locker.java | 42 +++++++-------- rxlib/src/main/java/org/rx/core/Numbers.java | 1 - .../java/org/rx/core/ObjectChangeTracker.java | 4 +- .../src/main/java/org/rx/util/Validator.java | 9 ---- rxlib/src/test/java/org/rx/util/TestUtil.java | 5 +- 10 files changed, 90 insertions(+), 71 deletions(-) rename rxlib/src/main/java/org/rx/bean/{WeakIdentityMap.java => ConcurrentWeakMap.java} (65%) diff --git a/rxlib-x/src/main/java/org/rx/spring/BeanRegister.java b/rxlib-x/src/main/java/org/rx/spring/BeanRegister.java index 1725bf7e..760a89b0 100644 --- a/rxlib-x/src/main/java/org/rx/spring/BeanRegister.java +++ b/rxlib-x/src/main/java/org/rx/spring/BeanRegister.java @@ -45,7 +45,7 @@ public RateLimiterAdapter httpRateLimiterAdapter(RedisCache rCache, Middle } String rk = "RateLimiter:" + clientIp; - RateLimiterAdapter adapter = IOC.weakMap(false) + RateLimiterAdapter adapter = IOC.weakMap(true) .computeIfAbsent(rk, k -> RedisUtil.wrapRateLimiter(new RedisRateLimiter(rCache, k, conf.getLimiterPermits()))); return adapter.tryAcquire(); }; diff --git a/rxlib/src/main/java/org/rx/bean/WeakIdentityMap.java b/rxlib/src/main/java/org/rx/bean/ConcurrentWeakMap.java similarity index 65% rename from rxlib/src/main/java/org/rx/bean/WeakIdentityMap.java rename to rxlib/src/main/java/org/rx/bean/ConcurrentWeakMap.java index e6708787..3e14c389 100644 --- a/rxlib/src/main/java/org/rx/bean/WeakIdentityMap.java +++ b/rxlib/src/main/java/org/rx/bean/ConcurrentWeakMap.java @@ -1,5 +1,7 @@ package org.rx.bean; +import lombok.NonNull; + import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; @@ -9,18 +11,25 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; -//ReferenceIdentityMap -public class WeakIdentityMap implements AbstractMap { - final Map, V> map; +//不要放值类型 +//ReferenceMap, ReferenceIdentityMap +public class ConcurrentWeakMap implements AbstractMap { final ReferenceQueue refQueue = new ReferenceQueue<>(); - transient MapView.EntrySetView, K, V> entrySet; + final Map, V> map; + final boolean identityReference; + transient MapView.EntrySetView, K, V> entrySet; - public WeakIdentityMap() { - this(16); + public ConcurrentWeakMap() { + this(16, false); } - public WeakIdentityMap(int initialCapacity) { + public ConcurrentWeakMap(int initialCapacity, boolean identityReference) { map = new ConcurrentHashMap<>(initialCapacity); + this.identityReference = identityReference; + } + + Reference toKeyReference(K key) { + return identityReference ? new IdentityWeakReference<>(key, refQueue) : new WeakReference<>(key, refQueue); } @Override @@ -30,43 +39,35 @@ public int size() { } @Override - public V get(Object key) { + public V get(@NonNull Object key) { expunge(); - Objects.requireNonNull(key, "key"); - WeakReference keyref = new IdentityWeakReference<>((K) key); - return map.get(keyref); + return map.get(toKeyReference((K) key)); } @Override - public V put(K key, V value) { + public V put(@NonNull K key, V value) { expunge(); - Objects.requireNonNull(key, "key"); - WeakReference keyref = new IdentityWeakReference<>(key, refQueue); - return map.put(keyref, value); + return map.put(toKeyReference(key), value); } @Override - public V remove(Object key) { + public V remove(@NonNull Object key) { expunge(); - Objects.requireNonNull(key, "key"); - WeakReference keyref = new IdentityWeakReference<>((K) key); - return map.remove(keyref); + return map.remove(toKeyReference((K) key)); } @Override public Set> entrySet() { expunge(); - MapView.EntrySetView, K, V> es; + MapView.EntrySetView, K, V> es; return (es = entrySet) != null ? es : (entrySet = new MapView.EntrySetView<>(map, Reference::get)); } @Override - public V computeIfAbsent(K key, Function mappingFunction) { + public V computeIfAbsent(@NonNull K key, Function mappingFunction) { expunge(); - Objects.requireNonNull(key, "key"); Objects.requireNonNull(mappingFunction, "mappingFunction"); - WeakReference keyref = new IdentityWeakReference<>(key, refQueue); - return map.computeIfAbsent(keyref, p -> mappingFunction.apply(key)); + return map.computeIfAbsent(toKeyReference(key), p -> mappingFunction.apply(key)); } // public Stream keysForValue(V value) { @@ -115,11 +116,10 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof WeakIdentityMap.IdentityWeakReference)) { + if (!(o instanceof IdentityWeakReference)) { return false; } - Object got = get(); - return got != null && got == ((IdentityWeakReference) o).get(); + return get() == ((IdentityWeakReference) o).get(); } @Override diff --git a/rxlib/src/main/java/org/rx/bean/DataTable.java b/rxlib/src/main/java/org/rx/bean/DataTable.java index 95238ed4..311ac4f6 100644 --- a/rxlib/src/main/java/org/rx/bean/DataTable.java +++ b/rxlib/src/main/java/org/rx/bean/DataTable.java @@ -1,6 +1,5 @@ package org.rx.bean; -import com.alibaba.fastjson2.JSONObject; import lombok.*; import lombok.extern.slf4j.Slf4j; import org.h2.expression.Alias; diff --git a/rxlib/src/main/java/org/rx/core/CpuWatchman.java b/rxlib/src/main/java/org/rx/core/CpuWatchman.java index 2cf48eda..bdf227b1 100644 --- a/rxlib/src/main/java/org/rx/core/CpuWatchman.java +++ b/rxlib/src/main/java/org/rx/core/CpuWatchman.java @@ -9,7 +9,10 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.rx.bean.*; +import org.rx.bean.DateTime; +import org.rx.bean.Decimal; +import org.rx.bean.IntWaterMark; +import org.rx.bean.Tuple; import org.rx.exception.InvalidException; import org.rx.exception.TraceHandler; import org.rx.util.BeanMapper; diff --git a/rxlib/src/main/java/org/rx/core/IOC.java b/rxlib/src/main/java/org/rx/core/IOC.java index 9ec62308..553ba25c 100644 --- a/rxlib/src/main/java/org/rx/core/IOC.java +++ b/rxlib/src/main/java/org/rx/core/IOC.java @@ -2,6 +2,10 @@ import lombok.NonNull; import lombok.SneakyThrows; +import org.apache.commons.collections4.map.AbstractReferenceMap; +import org.apache.commons.collections4.map.ReferenceIdentityMap; +import org.apache.commons.collections4.map.ReferenceMap; +import org.rx.bean.ConcurrentWeakMap; import org.rx.bean.WeakIdentityMap; import org.rx.exception.InvalidException; @@ -13,9 +17,8 @@ @SuppressWarnings(NON_UNCHECKED) public final class IOC { static final Map, Object> container = new ConcurrentHashMap<>(8); - static final Map WEAK_MAP = Collections.synchronizedMap(new WeakHashMap<>()); - //不要放值类型 - static final Map WEAK_IDENTITY_MAP = new WeakIdentityMap<>(); + static final Map WEAK_KEY_MAP = Collections.synchronizedMap(new WeakHashMap<>()); + static Map weakValMap, wKeyIdentityMap, wValIdentityMap; public static boolean isInit(Class type) { return container.containsKey(type); @@ -67,11 +70,32 @@ public static void unregister(Class type) { container.remove(type); } - public static Map weakMap(boolean identity) { - return identity ? WEAK_IDENTITY_MAP : WEAK_MAP; + public static Map weakMap(boolean weakValue) { + if (!weakValue) { + return WEAK_KEY_MAP; + } + synchronized (WEAK_KEY_MAP) { + if (weakValMap == null) { + weakValMap = Collections.synchronizedMap(new ReferenceMap<>(AbstractReferenceMap.ReferenceStrength.HARD, AbstractReferenceMap.ReferenceStrength.WEAK)); + } + return weakValMap; + } + } + + static Map weakMap(Object ref, boolean weakValue) { + return (Map) weakMap(weakValue).computeIfAbsent(ref, k -> new ConcurrentHashMap<>(4)); } - static Map weakMap(Object ref, boolean identity) { - return (Map) (identity ? WEAK_IDENTITY_MAP : WEAK_MAP).computeIfAbsent(ref, k -> new ConcurrentHashMap<>(4)); + public synchronized static Map weakIdentityMap(boolean weakValue) { + if (weakValue) { + if (wValIdentityMap == null) { + wValIdentityMap = Collections.synchronizedMap(new ReferenceIdentityMap<>(AbstractReferenceMap.ReferenceStrength.HARD, AbstractReferenceMap.ReferenceStrength.WEAK)); + } + return wValIdentityMap; + } + if (wKeyIdentityMap == null) { + wKeyIdentityMap = new ConcurrentWeakMap<>(); + } + return wKeyIdentityMap; } } diff --git a/rxlib/src/main/java/org/rx/core/Locker.java b/rxlib/src/main/java/org/rx/core/Locker.java index 958a3e84..05eb076a 100644 --- a/rxlib/src/main/java/org/rx/core/Locker.java +++ b/rxlib/src/main/java/org/rx/core/Locker.java @@ -1,21 +1,21 @@ -package org.rx.core; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.rx.bean.WeakIdentityMap; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.locks.ReentrantLock; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class Locker { - public static final Locker INSTANCE = new Locker(); - //key1: ref, key2: key - final Map> holder = new WeakIdentityMap<>(); - - public ReentrantLock getLock(Object ref, Object key) { - return holder.computeIfAbsent(ref, k -> new ConcurrentHashMap<>()) - .computeIfAbsent(key, k -> new ReentrantLock()); - } -} +//package org.rx.core; +// +//import lombok.AccessLevel; +//import lombok.NoArgsConstructor; +//import org.rx.bean.WeakIdentityMap; +// +//import java.util.Map; +//import java.util.concurrent.ConcurrentHashMap; +//import java.util.concurrent.locks.ReentrantLock; +// +//@NoArgsConstructor(access = AccessLevel.PRIVATE) +//public class Locker { +// public static final Locker INSTANCE = new Locker(); +// //key1: ref, key2: key +// final Map> holder = new WeakIdentityMap<>(); +// +// public ReentrantLock getLock(Object ref, Object key) { +// return holder.computeIfAbsent(ref, k -> new ConcurrentHashMap<>()) +// .computeIfAbsent(key, k -> new ReentrantLock()); +// } +//} diff --git a/rxlib/src/main/java/org/rx/core/Numbers.java b/rxlib/src/main/java/org/rx/core/Numbers.java index c72e0d79..16e184b3 100644 --- a/rxlib/src/main/java/org/rx/core/Numbers.java +++ b/rxlib/src/main/java/org/rx/core/Numbers.java @@ -1,7 +1,6 @@ package org.rx.core; import org.apache.commons.lang3.math.NumberUtils; -import org.rx.exception.InvalidException; import java.math.BigDecimal; diff --git a/rxlib/src/main/java/org/rx/core/ObjectChangeTracker.java b/rxlib/src/main/java/org/rx/core/ObjectChangeTracker.java index dedcc113..0958991f 100644 --- a/rxlib/src/main/java/org/rx/core/ObjectChangeTracker.java +++ b/rxlib/src/main/java/org/rx/core/ObjectChangeTracker.java @@ -3,7 +3,7 @@ import lombok.*; import lombok.extern.slf4j.Slf4j; import org.rx.annotation.Metadata; -import org.rx.bean.WeakIdentityMap; +import org.rx.bean.ConcurrentWeakMap; import org.rx.exception.InvalidException; import org.rx.exception.TraceHandler; import org.springframework.cglib.proxy.Enhancer; @@ -173,7 +173,7 @@ static Object getTarget(Object sourceObj) { //endregion public static final ObjectChangeTracker DEFAULT = new ObjectChangeTracker(); - final Map> sources = new WeakIdentityMap<>(); + final Map> sources = new ConcurrentWeakMap<>(); final EventBus bus = EventBus.DEFAULT; public ObjectChangeTracker() { diff --git a/rxlib/src/main/java/org/rx/util/Validator.java b/rxlib/src/main/java/org/rx/util/Validator.java index 7a7967e0..7b92a4da 100644 --- a/rxlib/src/main/java/org/rx/util/Validator.java +++ b/rxlib/src/main/java/org/rx/util/Validator.java @@ -1,14 +1,8 @@ package org.rx.util; -import com.alibaba.fastjson2.JSONArray; -import com.alibaba.fastjson2.JSONObject; import org.rx.annotation.ValidRegex; -import org.rx.core.Arrays; import org.rx.core.Linq; -import org.rx.core.Reflects; import org.rx.util.function.Func; -import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl; -import sun.reflect.generics.reflectiveObjects.TypeVariableImpl; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; @@ -16,14 +10,11 @@ import javax.validation.Validation; import javax.validation.executable.ExecutableValidator; import java.lang.reflect.Constructor; -import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.lang.reflect.Type; import java.util.Collections; import java.util.Set; import java.util.regex.Pattern; -import static org.rx.core.Extends.as; import static org.rx.core.Extends.ifNull; /** diff --git a/rxlib/src/test/java/org/rx/util/TestUtil.java b/rxlib/src/test/java/org/rx/util/TestUtil.java index 16ca57d2..58351ed6 100644 --- a/rxlib/src/test/java/org/rx/util/TestUtil.java +++ b/rxlib/src/test/java/org/rx/util/TestUtil.java @@ -14,7 +14,10 @@ import org.rx.annotation.Mapping; import org.rx.annotation.Metadata; import org.rx.annotation.Subscribe; -import org.rx.bean.*; +import org.rx.bean.BiTuple; +import org.rx.bean.DateTime; +import org.rx.bean.FlagsEnum; +import org.rx.bean.ULID; import org.rx.core.*; import org.rx.exception.InvalidException; import org.rx.test.GirlBean; From fd5fb218739522571a41c64cdabf5b7df8ac5af0 Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Thu, 28 Nov 2024 15:58:58 +0800 Subject: [PATCH 08/11] 3 --- rxlib/src/main/java/org/rx/bean/ConcurrentWeakMap.java | 6 +++--- rxlib/src/main/java/org/rx/core/CpuWatchman.java | 7 ++----- rxlib/src/main/java/org/rx/core/IOC.java | 6 +++--- rxlib/src/main/java/org/rx/core/ObjectChangeTracker.java | 2 +- rxlib/src/main/java/org/rx/net/NetEventWait.java | 4 ++-- 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/rxlib/src/main/java/org/rx/bean/ConcurrentWeakMap.java b/rxlib/src/main/java/org/rx/bean/ConcurrentWeakMap.java index 3e14c389..84a44cb3 100644 --- a/rxlib/src/main/java/org/rx/bean/ConcurrentWeakMap.java +++ b/rxlib/src/main/java/org/rx/bean/ConcurrentWeakMap.java @@ -19,11 +19,11 @@ public class ConcurrentWeakMap implements AbstractMap { final boolean identityReference; transient MapView.EntrySetView, K, V> entrySet; - public ConcurrentWeakMap() { - this(16, false); + public ConcurrentWeakMap(boolean identityReference) { + this(identityReference, 16); } - public ConcurrentWeakMap(int initialCapacity, boolean identityReference) { + public ConcurrentWeakMap(boolean identityReference, int initialCapacity) { map = new ConcurrentHashMap<>(initialCapacity); this.identityReference = identityReference; } diff --git a/rxlib/src/main/java/org/rx/core/CpuWatchman.java b/rxlib/src/main/java/org/rx/core/CpuWatchman.java index bdf227b1..c3ce5ac7 100644 --- a/rxlib/src/main/java/org/rx/core/CpuWatchman.java +++ b/rxlib/src/main/java/org/rx/core/CpuWatchman.java @@ -9,10 +9,7 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.rx.bean.DateTime; -import org.rx.bean.Decimal; -import org.rx.bean.IntWaterMark; -import org.rx.bean.Tuple; +import org.rx.bean.*; import org.rx.exception.InvalidException; import org.rx.exception.TraceHandler; import org.rx.util.BeanMapper; @@ -162,7 +159,7 @@ static int decrSize(ThreadPoolExecutor pool) { return poolSize; } - final Map> holder = new WeakIdentityMap<>(8); + final Map> holder = new ConcurrentWeakMap<>(true, 8); private CpuWatchman() { timer.newTimeout(this, RxConfig.INSTANCE.threadPool.samplingPeriod, TimeUnit.MILLISECONDS); diff --git a/rxlib/src/main/java/org/rx/core/IOC.java b/rxlib/src/main/java/org/rx/core/IOC.java index 553ba25c..ca20393a 100644 --- a/rxlib/src/main/java/org/rx/core/IOC.java +++ b/rxlib/src/main/java/org/rx/core/IOC.java @@ -6,7 +6,6 @@ import org.apache.commons.collections4.map.ReferenceIdentityMap; import org.apache.commons.collections4.map.ReferenceMap; import org.rx.bean.ConcurrentWeakMap; -import org.rx.bean.WeakIdentityMap; import org.rx.exception.InvalidException; import java.util.*; @@ -17,7 +16,8 @@ @SuppressWarnings(NON_UNCHECKED) public final class IOC { static final Map, Object> container = new ConcurrentHashMap<>(8); - static final Map WEAK_KEY_MAP = Collections.synchronizedMap(new WeakHashMap<>()); + // static final Map WEAK_KEY_MAP = Collections.synchronizedMap(new WeakHashMap<>()); + static final Map WEAK_KEY_MAP = new ConcurrentWeakMap<>(false); static Map weakValMap, wKeyIdentityMap, wValIdentityMap; public static boolean isInit(Class type) { @@ -94,7 +94,7 @@ public synchronized static Map weakIdentityMap(boolean weakValue) { return wValIdentityMap; } if (wKeyIdentityMap == null) { - wKeyIdentityMap = new ConcurrentWeakMap<>(); + wKeyIdentityMap = new ConcurrentWeakMap<>(true); } return wKeyIdentityMap; } diff --git a/rxlib/src/main/java/org/rx/core/ObjectChangeTracker.java b/rxlib/src/main/java/org/rx/core/ObjectChangeTracker.java index 0958991f..e052e5d6 100644 --- a/rxlib/src/main/java/org/rx/core/ObjectChangeTracker.java +++ b/rxlib/src/main/java/org/rx/core/ObjectChangeTracker.java @@ -173,7 +173,7 @@ static Object getTarget(Object sourceObj) { //endregion public static final ObjectChangeTracker DEFAULT = new ObjectChangeTracker(); - final Map> sources = new ConcurrentWeakMap<>(); + final Map> sources = new ConcurrentWeakMap<>(true); final EventBus bus = EventBus.DEFAULT; public ObjectChangeTracker() { diff --git a/rxlib/src/main/java/org/rx/net/NetEventWait.java b/rxlib/src/main/java/org/rx/net/NetEventWait.java index 1fbfbad4..9cadf645 100644 --- a/rxlib/src/main/java/org/rx/net/NetEventWait.java +++ b/rxlib/src/main/java/org/rx/net/NetEventWait.java @@ -9,7 +9,7 @@ import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; -import org.rx.bean.WeakIdentityMap; +import org.rx.bean.ConcurrentWeakMap; import org.rx.core.*; import org.rx.exception.TraceHandler; import org.rx.io.Bytes; @@ -88,7 +88,7 @@ public NetEventWait(@NonNull String group, @NonNull InetSocketAddress multicastE this.multicastEndpoint = multicastEndpoint; idString = group + "@" + Integer.toHexString(hashCode()); channel = channels.computeIfAbsent(multicastEndpoint, k -> (NioDatagramChannel) Sockets.udpBootstrap(MemoryMode.LOW, true, c -> { - c.attr(REF).set(Collections.newSetFromMap(new WeakIdentityMap<>())); + c.attr(REF).set(Collections.newSetFromMap(new ConcurrentWeakMap<>(true))); c.pipeline().addLast(Handler.DEFAULT); }) .bind(multicastEndpoint.getPort()).addListener((ChannelFutureListener) f -> { From 984e580939532b0171be754c3e406e625c3ca0f8 Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Thu, 28 Nov 2024 15:59:15 +0800 Subject: [PATCH 09/11] Update TestUtil.java --- rxlib/src/test/java/org/rx/util/TestUtil.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/rxlib/src/test/java/org/rx/util/TestUtil.java b/rxlib/src/test/java/org/rx/util/TestUtil.java index 58351ed6..8a5dfdca 100644 --- a/rxlib/src/test/java/org/rx/util/TestUtil.java +++ b/rxlib/src/test/java/org/rx/util/TestUtil.java @@ -14,10 +14,7 @@ import org.rx.annotation.Mapping; import org.rx.annotation.Metadata; import org.rx.annotation.Subscribe; -import org.rx.bean.BiTuple; -import org.rx.bean.DateTime; -import org.rx.bean.FlagsEnum; -import org.rx.bean.ULID; +import org.rx.bean.*; import org.rx.core.*; import org.rx.exception.InvalidException; import org.rx.test.GirlBean; @@ -419,7 +416,7 @@ public static class UserDemo { @Test public void third() { - Map identityMap = new WeakIdentityMap<>(); + Map identityMap = new ConcurrentWeakMap<>(true); UserStruct k = new UserStruct(); identityMap.put(k, 1); k.age = 2; From b8ad77d8337366e3f0630e19ff17b797bd7ec872 Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Thu, 28 Nov 2024 17:02:00 +0800 Subject: [PATCH 10/11] Update Main.java --- rxlib/src/main/java/org/rx/Main.java | 62 +++++++++++++++++----------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/rxlib/src/main/java/org/rx/Main.java b/rxlib/src/main/java/org/rx/Main.java index 3ad6340b..5e260370 100644 --- a/rxlib/src/main/java/org/rx/Main.java +++ b/rxlib/src/main/java/org/rx/Main.java @@ -12,6 +12,7 @@ import org.rx.net.TransportFlags; import org.rx.net.dns.DnsClient; import org.rx.net.dns.DnsServer; +import org.rx.net.http.*; import org.rx.net.rpc.Remoting; import org.rx.net.rpc.RpcClientConfig; import org.rx.net.rpc.RpcServerConfig; @@ -31,6 +32,7 @@ import java.math.BigInteger; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.Proxy; import java.util.Collections; import java.util.List; import java.util.Map; @@ -88,8 +90,8 @@ public static class RSSConf { public String udp2rawEndpoint; } - static RSSConf conf; static boolean udp2raw = false; + static RSSConf conf; @SneakyThrows static void launchClient(Map options, Integer port, Integer connectTimeout) { @@ -302,11 +304,36 @@ public void addWhiteList(InetAddress endpoint) { }); } - app.ddns(); + clientInit(); log.info("Server started.."); app.await(); } + static void clientInit() { + Tasks.schedulePeriod(() -> { + if (conf == null) { + log.warn("conf is null"); + } + + InetAddress wanIp = InetAddress.getByName(IPSearcher.DEFAULT.currentIp()); + for (String ddns : conf.ddnsDomains) { + List currentIps = DnsClient.inlandClient().resolveAll(ddns); + if (currentIps.contains(wanIp)) { + continue; + } + int i = ddns.indexOf("."); + String domain = ddns.substring(i + 1), name = ddns.substring(0, i); + log.info("ddns-{}.{}: {}->{}", name, domain, currentIps, wanIp); + AuthenticProxy p = conf.godaddyProxy != null + ? new AuthenticProxy(Proxy.Type.SOCKS, Sockets.parseEndpoint(conf.godaddyProxy)) + : null; + IPSearcher.godaddyDns(conf.getGodaddyKey(), domain, name, wanIp.getHostAddress(), p); + } + }, conf.ddnsSeconds * 1000L); + } + + static HttpServer httpServer; + static void launchServer(Map options, Integer port, Integer connectTimeout) { AuthenticEndpoint shadowUser = Reflects.convertQuietly(options.get("shadowUser"), AuthenticEndpoint.class); if (shadowUser == null) { @@ -330,34 +357,19 @@ static void launchServer(Map options, Integer port, Integer conn rpcConf.getTcpConfig().setTransportFlags(TransportFlags.FRONTEND_AES_COMBO.flags()); Main app = new Main(backSvr); Remoting.register(app, rpcConf); + serverInit(); app.await(); } - final SocksProxyServer proxyServer; - - void ddns() { -// Tasks.schedulePeriod(() -> { -// if (conf == null) { -// log.warn("conf is null"); -// } -// -// InetAddress wanIp = InetAddress.getByName(IPSearcher.DEFAULT.currentIp()); -// for (String ddns : conf.ddnsDomains) { -// List currentIps = DnsClient.inlandClient().resolveAll(ddns); -// if (currentIps.contains(wanIp)) { -// continue; -// } -// int i = ddns.indexOf("."); -// String domain = ddns.substring(i + 1), name = ddns.substring(0, i); -// log.info("ddns-{}.{}: {}->{}", name, domain, currentIps, wanIp); -// AuthenticProxy p = conf.godaddyProxy != null -// ? new AuthenticProxy(Proxy.Type.SOCKS, Sockets.parseEndpoint(conf.godaddyProxy)) -// : null; -// IPSearcher.godaddyDns(conf.getGodaddyKey(), domain, name, wanIp.getHostAddress(), p); -// } -// }, conf.ddnsSeconds * 1000L); + static void serverInit() { + httpServer = new HttpServer(8082, true).requestMapping("/hf", (request, response) -> { + String url = request.getQueryString().getFirst("forwardUrl"); + response.jsonBody(new HttpClient().get(url).toJson()); + }); } + final SocksProxyServer proxyServer; + @Override public void fakeEndpoint(BigInteger hash, String endpoint) { SocksSupport.fakeDict().putIfAbsent(hash, UnresolvedEndpoint.valueOf(endpoint)); From b17d4caa1b6846d05d23234346d7553246fd7953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=84=9E?= Date: Thu, 28 Nov 2024 17:14:17 +0800 Subject: [PATCH 11/11] Update Main.java --- rxlib/src/main/java/org/rx/Main.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rxlib/src/main/java/org/rx/Main.java b/rxlib/src/main/java/org/rx/Main.java index 5e260370..6c6b2de3 100644 --- a/rxlib/src/main/java/org/rx/Main.java +++ b/rxlib/src/main/java/org/rx/Main.java @@ -363,8 +363,13 @@ static void launchServer(Map options, Integer port, Integer conn static void serverInit() { httpServer = new HttpServer(8082, true).requestMapping("/hf", (request, response) -> { - String url = request.getQueryString().getFirst("forwardUrl"); - response.jsonBody(new HttpClient().get(url).toJson()); + String url = request.getQueryString().getFirst("fu"); + Integer tm = Reflects.convertQuietly(request.getQueryString().getFirst("tm"), Integer.class); + HttpClient client = new HttpClient(); + if (tm != null) { + client.withTimeoutMillis(tm); + } + response.jsonBody(client.get(url).toJson()); }); }