Adds WeightCalculatorWebFilter.setRandomFunction()
This allows users to set a custom Function that has the ServerWebExchange available. Default behavior is unchanged Fixes gh-3298
This commit is contained in:
@@ -25,6 +25,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -42,6 +43,7 @@ import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.event.SmartApplicationListener;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
import org.springframework.web.server.WebFilterChain;
|
||||
@@ -65,7 +67,7 @@ public class WeightCalculatorWebFilter implements WebFilter, Ordered, SmartAppli
|
||||
|
||||
private final ConfigurationService configurationService;
|
||||
|
||||
private Supplier<Double> randomSupplier = null;
|
||||
private Function<ServerWebExchange, Double> randomFunction = exchange -> ThreadLocalRandom.current().nextDouble();
|
||||
|
||||
private int order = WEIGHT_CALC_FILTER_ORDER;
|
||||
|
||||
@@ -101,11 +103,18 @@ public class WeightCalculatorWebFilter implements WebFilter, Ordered, SmartAppli
|
||||
|
||||
@Deprecated
|
||||
public void setRandom(Random random) {
|
||||
this.randomSupplier = random::nextDouble;
|
||||
Assert.notNull(random, "random may not be null");
|
||||
this.randomFunction = exchange -> random.nextDouble();
|
||||
}
|
||||
|
||||
public void setRandomSupplier(Supplier<Double> randomSupplier) {
|
||||
this.randomSupplier = randomSupplier;
|
||||
Assert.notNull(randomSupplier, "randomSupplier may not be null");
|
||||
this.randomFunction = exchange -> randomSupplier.get();
|
||||
}
|
||||
|
||||
public void setRandomFunction(Function<ServerWebExchange, Double> randomFunction) {
|
||||
Assert.notNull(randomFunction, "randomFunction may not be null");
|
||||
this.randomFunction = randomFunction;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -238,17 +247,9 @@ public class WeightCalculatorWebFilter implements WebFilter, Ordered, SmartAppli
|
||||
continue; // nothing we can do, but this is odd
|
||||
}
|
||||
|
||||
/*
|
||||
* Usually, multiple threads accessing the same random object will have some
|
||||
* performance problems, so we can use ThreadLocalRandom by default
|
||||
*/
|
||||
double r;
|
||||
if (this.randomSupplier != null) {
|
||||
r = randomSupplier.get();
|
||||
}
|
||||
else {
|
||||
r = ThreadLocalRandom.current().nextDouble();
|
||||
}
|
||||
// Usually, multiple threads accessing the same random object will have some
|
||||
// performance problems, so we can use ThreadLocalRandom by default
|
||||
double r = randomFunction.apply(exchange);
|
||||
|
||||
List<Double> ranges = config.ranges;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user