Skip to content

Commit

Permalink
Use SecureRandom rather than ThreadLocalRandom (#16291)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanwenjun authored Jul 9, 2024
1 parent 255ec35 commit fb8f3ed
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

import org.apache.commons.collections4.CollectionUtils;

import java.security.SecureRandom;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;

import org.jetbrains.annotations.NotNull;

Expand All @@ -34,8 +34,11 @@ public class RandomWorkerLoadBalancer implements IWorkerLoadBalancer {

private final WorkerClusters workerClusters;

private final SecureRandom secureRandom;

public RandomWorkerLoadBalancer(WorkerClusters workerClusters) {
this.workerClusters = workerClusters;
this.secureRandom = new SecureRandom();
}

@Override
Expand All @@ -44,7 +47,7 @@ public Optional<String> select(@NotNull String workerGroup) {
if (CollectionUtils.isEmpty(workerServerAddresses)) {
return Optional.empty();
}
int index = ThreadLocalRandom.current().nextInt(workerServerAddresses.size());
int index = secureRandom.nextInt(workerServerAddresses.size());
return Optional.of(workerServerAddresses.get(index));
}

Expand Down

0 comments on commit fb8f3ed

Please sign in to comment.