You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2022/01/19 07:48:54 UTC
[GitHub] [james-project] vttranlina commented on a change in pull request #848: JAMES-3693 Redis implementation RateLimiter (with configuration)
vttranlina commented on a change in pull request #848:
URL: https://github.com/apache/james-project/pull/848#discussion_r787427507
##########
File path: server/mailet/rate-limiter-redis/src/main/java/org/apache/james/rate/limiter/redis/RedisRateLimiterConfiguration.java
##########
@@ -0,0 +1,106 @@
+/****************************************************************
+ * 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.james.rate.limiter.redis;
+
+import java.time.Duration;
+
+import org.apache.commons.configuration2.HierarchicalConfiguration;
+import org.apache.commons.configuration2.tree.ImmutableNode;
+
+import com.google.common.base.Preconditions;
+
+import io.lettuce.core.RedisURI;
+
+public class RedisRateLimiterConfiguration {
+ public static RedisRateLimiterConfiguration parse(HierarchicalConfiguration<ImmutableNode> configuration) {
+ return builder()
+ .redisURI(configuration.getString("redisURL", null))
+ .periodTime(configuration.getLong("period", null))
+ .redisKeyPrefix(configuration.getString("redisKeyPrefix", null))
+ .build();
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static class Builder {
+ private RedisURI redisURI;
+ private String redisKeyPrefix;
+ private Duration periodTime;
+
+ public Builder() {
+ }
+
+ public Builder redisURI(RedisURI redisURI) {
+ this.redisURI = redisURI;
+ return this;
+ }
+
+ public Builder redisURI(String redisURI) {
+ return redisURI(RedisURI.create(redisURI));
+ }
+
+ public Builder redisKeyPrefix(String redisKeyPrefix) {
+ this.redisKeyPrefix = redisKeyPrefix;
+ return this;
+ }
+
+ public Builder periodTime(Duration periodTime) {
+ this.periodTime = periodTime;
+ return this;
+ }
+
+ public Builder periodTime(long periodTime) {
+ return periodTime(Duration.ofSeconds(periodTime));
+ }
+
+ public RedisRateLimiterConfiguration build() {
+ return new RedisRateLimiterConfiguration(redisURI, redisKeyPrefix, periodTime);
+ }
+
+ }
+
+ public RedisRateLimiterConfiguration(RedisURI redisURI, String redisKeyPrefix, Duration periodTime) {
+ Preconditions.checkNotNull(redisURI);
+ Preconditions.checkNotNull(redisKeyPrefix);
+ Preconditions.checkNotNull(periodTime);
+ Preconditions.checkArgument(!periodTime.isNegative() && !periodTime.isZero(), "period must be a positive");
+ this.redisURI = redisURI;
+ this.redisKeyPrefix = redisKeyPrefix;
+ this.periodTime = periodTime;
+ }
+
+ private final RedisURI redisURI;
+ private final String redisKeyPrefix;
+ private final Duration periodTime;
+
+ public RedisURI getRedisURI() {
+ return redisURI;
+ }
+
+ public String getRedisKeyPrefix() {
+ return redisKeyPrefix;
+ }
+
+ public Duration getPeriodTime() {
+ return periodTime;
+ }
Review comment:
Maybe, we have `org.apache.james.rate.limiter.api.Rule(quantity: Quantity, duration: Duration)`
--
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: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org