Skip to content

Commit

Permalink
Use secure random
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ committed Jul 11, 2023
1 parent f882cbd commit c45ce19
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.utils.StringUtils;

import java.security.SecureRandom;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -28,7 +29,7 @@
*/
public class Request {

private static final AtomicLong INVOKE_ID = new AtomicLong(ThreadLocalRandom.current().nextLong());
private static final AtomicLong INVOKE_ID;

private final long mId;

Expand All @@ -50,6 +51,16 @@ public Request(long id) {
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();
Expand Down

0 comments on commit c45ce19

Please sign in to comment.