1   /*
2    * Copyright 2020 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.server;
17  
18  import static org.apache.curator.shaded.com.google.common.base.Preconditions.checkArgument;
19  
20  import com.fasterxml.jackson.annotation.JsonCreator;
21  import com.fasterxml.jackson.annotation.JsonProperty;
22  import com.google.common.base.MoreObjects;
23  
24  /**
25   * {@link CentralDogma} API quota configuration.
26   */
27  public final class QuotaConfig {
28  
29      private final int requestQuota;
30      private final int timeWindowSeconds;
31  
32      /**
33       * Creates a new instance.
34       */
35      @JsonCreator
36      public QuotaConfig(@JsonProperty("requestQuota") int requestQuota,
37                         @JsonProperty("timeWindowSeconds") int timeWindowSeconds) {
38          checkArgument(requestQuota > 0, "requestQuota: %s (expected: > 0)", requestQuota);
39          checkArgument(timeWindowSeconds > 0, "timeWindowSeconds: %s (expected: > 0)", timeWindowSeconds);
40  
41          this.requestQuota = requestQuota;
42          this.timeWindowSeconds = timeWindowSeconds;
43      }
44  
45      /**
46       * Returns a time windows in seconds which is created with the given {@code timeWindowSeconds}.
47       */
48      @JsonProperty
49      public int timeWindowSeconds() {
50          return timeWindowSeconds;
51      }
52  
53      /**
54       * Returns a maximum number of acceptable requests which is created with the given {@code requestQuota}.
55       */
56      @JsonProperty
57      public int requestQuota() {
58          return requestQuota;
59      }
60  
61      /**
62       * Returns a maximum number of acceptable requests per second.
63       */
64      public double permitsPerSecond() {
65          return requestQuota() * 1.0 / timeWindowSeconds();
66      }
67  
68      @Override
69      public boolean equals(Object o) {
70          if (this == o) {
71              return true;
72          }
73          if (!(o instanceof QuotaConfig)) {
74              return false;
75          }
76  
77          final QuotaConfig that = (QuotaConfig) o;
78          return requestQuota == that.requestQuota && timeWindowSeconds == that.timeWindowSeconds;
79      }
80  
81      @Override
82      public int hashCode() {
83          return requestQuota * 31 + timeWindowSeconds;
84      }
85  
86      @Override
87      public String toString() {
88          return MoreObjects.toStringHelper(this)
89                            .add("requestQuota", requestQuota)
90                            .add("timeWindowSeconds", timeWindowSeconds)
91                            .toString();
92      }
93  }