1
0
mirror of https://github.com/apache/dubbo.git synced 2024-10-23 07:04:37 +08:00

Use secure random

This commit is contained in:
Albumen Kevin 2023-07-11 10:10:38 +08:00
parent f882cbdd80
commit c45ce1974d

View File

@ -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();