-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR is motivated by the following issues: * `RateLimiter` implementations are typically request-scoped, because they are sensitive to the realm context, but keeping a cache of token buckets per realm must be delegated to a separate, application-scoped bean: this is solved by introducing a new `TokenBucketFactory` bean. * `TokenBucketRateLimiter` should NOT implement `RateLimiter`, because that would result in two available beans for this interface: the one selected by configuration (`realm-token-bucket` or `no-op`), and this one which is always available, thus triggering an unresolvable bean error when using the Quarkus runtime. Instead, this class is now just a general-purpose Token Bucket implementation. This PR has one user-facing change: * The configuration options for the `realm-token-bucket` rate limiter were moved from the `rateLimiter` section to the `tokenBucketFactory` section.
- Loading branch information
Showing
16 changed files
with
170 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ommon/src/main/java/org/apache/polaris/service/ratelimiter/DefaultTokenBucketFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.smallrye.common.annotation.Identifier; | ||
import java.time.Clock; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import org.apache.polaris.core.context.RealmContext; | ||
|
||
@Identifier("default") | ||
public class DefaultTokenBucketFactory implements TokenBucketFactory { | ||
|
||
private final long requestsPerSecond; | ||
private final long windowSeconds; | ||
private final Clock clock; | ||
private final Map<String, TokenBucket> perRealmBuckets = new ConcurrentHashMap<>(); | ||
|
||
@JsonCreator | ||
public DefaultTokenBucketFactory( | ||
@JsonProperty("requestsPerSecond") long requestsPerSecond, | ||
@JsonProperty("windowSeconds") long windowSeconds) { | ||
this(requestsPerSecond, windowSeconds, Clock.systemUTC()); | ||
} | ||
|
||
public DefaultTokenBucketFactory(long requestsPerSecond, long windowSeconds, Clock clock) { | ||
this.requestsPerSecond = requestsPerSecond; | ||
this.windowSeconds = windowSeconds; | ||
this.clock = clock; | ||
} | ||
|
||
@Override | ||
public TokenBucket getOrCreateTokenBucket(RealmContext realmContext) { | ||
String realmId = realmContext.getRealmIdentifier(); | ||
return perRealmBuckets.computeIfAbsent( | ||
realmId, | ||
k -> | ||
new TokenBucket( | ||
requestsPerSecond, Math.multiplyExact(requestsPerSecond, windowSeconds), clock)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.