Skip to content

Commit

Permalink
Adds WeightCalculatorWebFilter.setRandomFunction()
Browse files Browse the repository at this point in the history
This allows users to set a custom Function that has the ServerWebExchange available.

Default behavior is unchanged

Fixes gh-3298
  • Loading branch information
spencergibb committed Mar 14, 2024
1 parent 35c6384 commit e844024
Showing 1 changed file with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand All @@ -42,6 +43,7 @@
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;
Expand All @@ -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;

Expand Down Expand Up @@ -101,11 +103,18 @@ public void setOrder(int order) {

@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
Expand Down Expand Up @@ -238,17 +247,9 @@ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
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;

Expand Down

0 comments on commit e844024

Please sign in to comment.