Skip to content

Commit 21d990e

Browse files
authored
Issue #1487: reuse Random instance (#1489)
1 parent 44918a3 commit 21d990e

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

changes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ Changes log
22
===========
33

44
- 2.7 Milestone 3 (??-??-2025)
5-
5+
- Bugs fixed
6+
- Reuse an instance of Random class in RandomUtils. Issue #1487.
67
- 2.7 Milestone 2 (29-06-2025)
78
- Misc
89
- Removed deprecated Servlet extension and related classes in Spring extension

org.restlet/src/main/java/org/restlet/routing/Router.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* <li>First match (default)</li>
3535
* <li>Last match</li>
3636
* <li>Random match</li>
37-
* <li>Round robin</li>
37+
* <li>Round-robin</li>
3838
* <li>Custom</li>
3939
* </ul>
4040
* <br>
@@ -53,7 +53,7 @@
5353
public class Router extends Restlet {
5454

5555
/**
56-
* Each call will be routed to the route with the best score, if the required
56+
* Each call will be routed to the route with the best score if the required
5757
* score is reached. See {@link RouteList#getBest(Request, Response, float)}
5858
* method for implementation details.
5959
*/
@@ -94,7 +94,7 @@ public class Router extends Restlet {
9494

9595
/**
9696
* Each call will be randomly routed to one of the routes that reached the
97-
* required score. If the random route selected is not a match then the
97+
* required score. If the random route selected is not a match, then the
9898
* immediate next route is evaluated until one matching route is found. If we
9999
* get back to the initial random route selected with no match, then we return
100100
* null. Unless all the routes score above the required score, this mode will

org.restlet/src/main/java/org/restlet/util/RouteList.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.restlet.Restlet;
1515
import org.restlet.routing.Route;
1616

17+
import java.security.SecureRandom;
1718
import java.util.Collections;
1819
import java.util.List;
1920
import java.util.Random;
@@ -34,14 +35,16 @@
3435
* @see java.util.List
3536
*/
3637
public final class RouteList extends WrapperList<Route> {
37-
/** The index of the last route used in the round robin mode. */
38+
/** The index of the last route used in the round-robin mode. */
3839
private volatile int lastIndex;
40+
/** Used when asked to return a random route. */
41+
private final SecureRandom random = new SecureRandom();
3942

4043
/**
4144
* Constructor.
4245
*/
4346
public RouteList() {
44-
super(new CopyOnWriteArrayList<Route>());
47+
super(new CopyOnWriteArrayList<>());
4548
this.lastIndex = -1;
4649
}
4750

@@ -51,7 +54,7 @@ public RouteList() {
5154
* @param delegate The delegate list.
5255
*/
5356
public RouteList(List<Route> delegate) {
54-
super(new CopyOnWriteArrayList<Route>(delegate));
57+
super(new CopyOnWriteArrayList<>(delegate));
5558
this.lastIndex = -1;
5659
}
5760

@@ -159,7 +162,7 @@ public synchronized Route getRandom(Request request, Response response, float re
159162
int length = size();
160163

161164
if (length > 0) {
162-
int j = new Random().nextInt(length);
165+
int j = random.nextInt(length);
163166
Route route = get(j);
164167

165168
if (route.score(request, response) >= requiredScore) {
@@ -169,7 +172,7 @@ public synchronized Route getRandom(Request request, Response response, float re
169172
boolean loopedAround = false;
170173

171174
do {
172-
if ((j == length) && (!loopedAround)) {
175+
if ((j == length) && !loopedAround) {
173176
j = 0;
174177
loopedAround = true;
175178
}
@@ -187,7 +190,7 @@ public synchronized Route getRandom(Request request, Response response, float re
187190
}
188191

189192
/**
190-
* Removes all routes routing to a given target.
193+
* Removes all routes to a given target.
191194
*
192195
* @param target The target Restlet to detach.
193196
*/

0 commit comments

Comments
 (0)