andrew4699 commented on code in PR #278: URL: https://github.com/apache/polaris/pull/278#discussion_r1771817363
########## polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java: ########## @@ -0,0 +1,92 @@ +/* + * 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.time.Clock; +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.apache.polaris.core.context.RealmContext; +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, Clock.systemUTC()); + + 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 { + RealmContext realmContext = CallContext.getCurrentContext().getRealmContext(); + RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext); + if (!rateLimiter.tryAcquire()) { + ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode()); + return; + } + chain.doFilter(request, response); + } + + private RateLimiter maybeBlockToGetRateLimiter(RealmContext realmContext) { + try { + return perRealmLimiters + .computeIfAbsent( + realmContext.getRealmIdentifier(), + (key) -> config.getRateLimiterFactory().createRateLimiter(realmContext)) + .get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS); Review Comment: Async construction isn't used by our implementation but making the interfaces async lets people implement either version and adds a safeguard in the form of a timeout in case they choose to implement an async one. An example of where you'd use async construction would be if you need to fetch some account information during construction. -- 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