Skip to content

Commit

Permalink
Change Map value to be a Future itself, use System.nanoTime
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew4699 committed Sep 16, 2024
1 parent 4e0259d commit 7512182
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@

/** An implementation of our Clock interface using opentelemetry's Clock implementation */
public class ClockImpl implements Clock {
private final io.opentelemetry.sdk.common.Clock openTelemetryClock;

public ClockImpl() {
openTelemetryClock = io.opentelemetry.sdk.common.Clock.getDefault();
}

@Override
public long nanoTime() {
return openTelemetryClock.nanoTime();
return System.nanoTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public class DefaultRateLimiterFactory implements RateLimiterFactory {
private double windowSeconds;

@Override
public Future<RateLimiter> createRateLimiter(String key, Clock clock) {
public Future<RateLimiter> createRateLimiter(String key) {
return CompletableFuture.supplyAsync(
() ->
new OpenTelemetryRateLimiter(
requestsPerSecond, requestsPerSecond * windowSeconds, clock));
requestsPerSecond, requestsPerSecond * windowSeconds, new ClockImpl()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@JsonTypeName("no-op")
public class NoOpRateLimiterFactory implements RateLimiterFactory {
@Override
public Future<RateLimiter> createRateLimiter(String key, Clock clock) {
public Future<RateLimiter> createRateLimiter(String key) {
return CompletableFuture.supplyAsync(NoOpRateLimiter::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public interface RateLimiterFactory extends Discoverable {
*
* @param key The rate limiting key. Rate limiters may optionally choose to discriminate their
* behavior by the key.
* @param clock The clock which tells you the current time
* @return a Future with the constructed RateLimiter
*/
Future<RateLimiter> createRateLimiter(String key, Clock clock);
Future<RateLimiter> createRateLimiter(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.polaris.core.context.CallContext;
Expand All @@ -47,7 +48,7 @@ public class RateLimiterFilter implements Filter {
private static final Clock CLOCK = new ClockImpl();

private final RateLimiterConfig config;
private final Map<String, RateLimiter> perRealmLimiters = new ConcurrentHashMap<>();
private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();

public RateLimiterFilter(RateLimiterConfig config) {
this.config = config;
Expand All @@ -67,16 +68,10 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}

private RateLimiter maybeBlockToGetRateLimiter(String realm) {
return perRealmLimiters.computeIfAbsent(realm, this::createRateLimiterBlocking);
}

/** Creates a rate limiter, enforcing a timeout on how long creation can take. */
private RateLimiter createRateLimiterBlocking(String key) {
try {
return config
.getRateLimiterFactory()
.createRateLimiter(key, CLOCK)
.get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);
return perRealmLimiters.computeIfAbsent(realm, (key) -> config
.getRateLimiterFactory()
.createRateLimiter(key)).get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
return getDefaultRateLimiterOnConstructionFailed(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
#

org.apache.polaris.service.ratelimiter.DefaultRateLimiterFactory
org.apache.polaris.service.ratelimiter.NoOpRateLimiterFactory
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class MockRateLimiterFactory implements RateLimiterFactory {
public boolean neverFinishConstruction;

@Override
public Future<RateLimiter> createRateLimiter(String key, Clock clock) {
public Future<RateLimiter> createRateLimiter(String key) {
if (neverFinishConstruction) {
// This future will never finish
return new CompletableFuture<>();
Expand Down

0 comments on commit 7512182

Please sign in to comment.