From c45ce1974dfd7942d7479e60f9fb80258c79b851 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Tue, 11 Jul 2023 10:10:38 +0800 Subject: [PATCH] Use secure random --- .../org/apache/dubbo/remoting/exchange/Request.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Request.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Request.java index c243258a24..51ec2f2ec8 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Request.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Request.java @@ -18,6 +18,7 @@ package org.apache.dubbo.remoting.exchange; import org.apache.dubbo.common.utils.StringUtils; +import java.security.SecureRandom; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicLong; @@ -28,7 +29,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.HEARTBEAT_EVENT; */ public class Request { - private static final AtomicLong INVOKE_ID = new AtomicLong(ThreadLocalRandom.current().nextLong()); + private static final AtomicLong INVOKE_ID; private final long mId; @@ -50,6 +51,16 @@ public class Request { mId = id; } + static { + long startID = ThreadLocalRandom.current().nextLong(); + try { + SecureRandom rand = new SecureRandom(SecureRandom.getSeed(20)); + startID = rand.nextLong(); + } catch (Throwable ignore) { + } + INVOKE_ID = new AtomicLong(startID); + } + private static long newId() { // getAndIncrement() When it grows to MAX_VALUE, it will grow to MIN_VALUE, and the negative can be used as ID return INVOKE_ID.getAndIncrement();