snazy commented on code in PR #278: URL: https://github.com/apache/polaris/pull/278#discussion_r1766460259
########## polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java: ########## @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.polaris.service.ratelimiter; + +/** + * Token bucket implementation of a Polaris RateLimiter. Acquires tokens at a fixed rate and has a + * maximum amount of tokens. Each "acquire" costs 1 token. + */ +public class TokenBucketRateLimiter implements RateLimiter { + private final double tokensPerNano; Review Comment: Those `double`s could all be `long`s. I wonder though whether `com.google.common.util.concurrent.RateLimiter` wouldn't satisfy the need as well. Using the Guava one could save some testing pain ;) OTOH it uses Guava's clock/watch stuff. ########## polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java: ########## @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.polaris.service.ratelimiter; + +import jakarta.annotation.Priority; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.ws.rs.Priorities; +import jakarta.ws.rs.core.Response; +import java.io.IOException; +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Request filter that returns a 429 Too Many Requests if the rate limiter says so */ +@Priority(Priorities.AUTHORIZATION + 1) +public class RateLimiterFilter implements Filter { + private static final Logger LOGGER = LoggerFactory.getLogger(RateLimiterFilter.class); + private static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter(); + private static final RateLimiter ALWAYS_REJECT_LIMITER = + new TokenBucketRateLimiter(0, 0, new ClockImpl()); + private static final Clock CLOCK = new ClockImpl(); + + private final RateLimiterConfig config; + private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>(); + + public RateLimiterFilter(RateLimiterConfig config) { + this.config = config; + } + + /** Returns a 429 if the rate limiter says so. Otherwise, forwards the request along. */ + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier(); + RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm); + if (!rateLimiter.tryAcquire()) { + ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode()); + return; + } + chain.doFilter(request, response); + } + + private RateLimiter maybeBlockToGetRateLimiter(String realm) { + try { + return perRealmLimiters + .computeIfAbsent(realm, (key) -> config.getRateLimiterFactory().createRateLimiter(key)) Review Comment: Much better! Thanks! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@polaris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org