Skip to content

Commit dfe6753

Browse files
committed
fixed random to be thread safe #2973
1 parent b6002a5 commit dfe6753

File tree

1 file changed

+5
-7
lines changed
  • rate-limiting-pattern/src/main/java/com/iluwatar/rate/limiting/pattern

1 file changed

+5
-7
lines changed

rate-limiting-pattern/src/main/java/com/iluwatar/rate/limiting/pattern/App.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.iluwatar.rate.limiting.pattern;
22

3+
import java.security.SecureRandom;
34
import java.util.concurrent.*;
45
import java.util.concurrent.atomic.AtomicBoolean;
56
import java.util.concurrent.atomic.AtomicInteger;
@@ -101,13 +102,13 @@ private static void shutdownExecutor(ExecutorService service, String name) {
101102

102103
static Runnable createClientTask(
103104
int clientId, RateLimiter s3Limiter, RateLimiter dynamoDbLimiter, RateLimiter lambdaLimiter) {
105+
104106
return () -> {
105107
String[] services = {"s3", "dynamodb", "lambda"};
106108
String[] operations = {
107-
"GetObject", "PutObject", "Query", "Scan", "PutItem", "Invoke", "ListFunctions"
109+
"GetObject", "PutObject", "Query", "Scan", "PutItem", "Invoke", "ListFunctions"
108110
};
109-
// Safe: ThreadLocalRandom is used per-thread for concurrent simulation
110-
ThreadLocalRandom random = ThreadLocalRandom.current();
111+
SecureRandom random = new SecureRandom(); // ✅ Safe & compliant for SonarCloud
111112

112113
while (running.get() && !Thread.currentThread().isInterrupted()) {
113114
try {
@@ -138,10 +139,7 @@ static void makeRequest(int clientId, RateLimiter limiter, String service, Strin
138139
throttledRequests.incrementAndGet();
139140
LOGGER.warn(
140141
"Client {}: {}.{} - THROTTLED (Retry in {}ms)",
141-
clientId,
142-
service,
143-
operation,
144-
e.getRetryAfterMillis());
142+
clientId, service, operation, e.getRetryAfterMillis());
145143
} catch (ServiceUnavailableException e) {
146144
failedRequests.incrementAndGet();
147145
LOGGER.warn("Client {}: {}.{} - SERVICE UNAVAILABLE", clientId, service, operation);

0 commit comments

Comments
 (0)