1   /*
2    * Copyright 2019 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.internal.storage.repository.git;
17  
18  import javax.annotation.Nullable;
19  
20  import org.slf4j.Logger;
21  
22  import com.linecorp.armeria.common.RequestContext;
23  import com.linecorp.armeria.common.util.Exceptions;
24  import com.linecorp.armeria.server.ServiceRequestContext;
25  import com.linecorp.centraldogma.server.internal.storage.RequestAlreadyTimedOutException;
26  
27  final class FailFastUtil {
28  
29      private static final RequestAlreadyTimedOutException REQUEST_ALREADY_TIMED_OUT =
30              Exceptions.clearTrace(new RequestAlreadyTimedOutException("Request already timed out."));
31  
32      static {
33          REQUEST_ALREADY_TIMED_OUT.initCause(null);
34      }
35  
36      @Nullable
37      static ServiceRequestContext context() {
38          return RequestContext.mapCurrent(ServiceRequestContext.class::cast, null);
39      }
40  
41      @SuppressWarnings("MethodParameterNamingConvention")
42      static void failFastIfTimedOut(GitRepository repo, Logger logger, @Nullable ServiceRequestContext ctx,
43                                     String methodName, Object arg1) {
44          if (ctx != null && ctx.isTimedOut()) {
45              logger.info("{} Rejecting a request timed out already: repo={}/{}, method={}, args={}",
46                          ctx, repo.parent().name(), repo.name(), methodName, arg1);
47              throw REQUEST_ALREADY_TIMED_OUT;
48          }
49      }
50  
51      @SuppressWarnings("MethodParameterNamingConvention")
52      static void failFastIfTimedOut(GitRepository repo, Logger logger, @Nullable ServiceRequestContext ctx,
53                                     String methodName, Object arg1, Object arg2) {
54          if (ctx != null && ctx.isTimedOut()) {
55              logger.info("{} Rejecting a request timed out already: repo={}/{}, method={}, args=[{}, {}]",
56                          ctx, repo.parent().name(), repo.name(), methodName, arg1, arg2);
57              throw REQUEST_ALREADY_TIMED_OUT;
58          }
59      }
60  
61      @SuppressWarnings("MethodParameterNamingConvention")
62      static void failFastIfTimedOut(GitRepository repo, Logger logger, @Nullable ServiceRequestContext ctx,
63                                     String methodName, Object arg1, Object arg2, Object arg3) {
64          if (ctx != null && ctx.isTimedOut()) {
65              logger.info("{} Rejecting a request timed out already: repo={}/{}, method={}, args=[{}, {}, {}]",
66                          ctx, repo.parent().name(), repo.name(), methodName, arg1, arg2, arg3);
67              throw REQUEST_ALREADY_TIMED_OUT;
68          }
69      }
70  
71      @SuppressWarnings("MethodParameterNamingConvention")
72      static void failFastIfTimedOut(GitRepository repo, Logger logger, @Nullable ServiceRequestContext ctx,
73                                     String methodName, Object arg1, Object arg2, Object arg3, int arg4) {
74          if (ctx != null && ctx.isTimedOut()) {
75              logger.info(
76                      "{} Rejecting a request timed out already: repo={}/{}, method={}, args=[{}, {}, {}, {}]",
77                      ctx, repo.parent().name(), repo.name(), methodName, arg1, arg2, arg3, arg4);
78              throw REQUEST_ALREADY_TIMED_OUT;
79          }
80      }
81  
82      private FailFastUtil() {}
83  }