1   /*
2    * Copyright 2017 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 com.google.common.base.Preconditions.checkArgument;
19  
20  import com.fasterxml.jackson.annotation.JsonProperty;
21  import com.google.common.base.MoreObjects;
22  
23  /**
24   * Graceful shutdown timeout.
25   */
26  public final class GracefulShutdownTimeout {
27      private final long quietPeriodMillis;
28      private final long timeoutMillis;
29  
30      /**
31       * Creates a new instance.
32       *
33       * @param quietPeriodMillis the number of milliseconds to wait for active requests to go end before
34       *                          shutting down. {@code 0} means the server will stop right away without waiting.
35       * @param timeoutMillis the number of milliseconds to wait before shutting down the server regardless of
36       *                      active requests. This should be set to a time greater than {@code quietPeriodMillis}
37       *                      to ensure the server shuts down even if there is a stuck request.
38       */
39      public GracefulShutdownTimeout(
40              @JsonProperty(value = "quietPeriodMillis", required = true) long quietPeriodMillis,
41              @JsonProperty(value = "timeoutMillis", required = true) long timeoutMillis) {
42          checkArgument(quietPeriodMillis >= 0,
43                        "quietPeriodMillis: %s (expected: >= 0)", quietPeriodMillis);
44          checkArgument(timeoutMillis >= 0,
45                        "timeoutMillis: %s (expected: >= 0)", timeoutMillis);
46          this.quietPeriodMillis = quietPeriodMillis;
47          this.timeoutMillis = timeoutMillis;
48      }
49  
50      /**
51       * Returns the quiet period of graceful shutdown process, in milliseconds.
52       */
53      @JsonProperty("quietPeriodMillis")
54      public long quietPeriodMillis() {
55          return quietPeriodMillis;
56      }
57  
58      /**
59       * Returns the timeout of graceful shutdown process, in milliseconds.
60       */
61      @JsonProperty("timeoutMillis")
62      public long timeoutMillis() {
63          return timeoutMillis;
64      }
65  
66      @Override
67      public String toString() {
68          return MoreObjects.toStringHelper(this)
69                            .add("quietPeriodMillis", quietPeriodMillis)
70                            .add("timeoutMillis", timeoutMillis).toString();
71      }
72  }