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.client.armeria;
17  
18  import java.net.UnknownHostException;
19  import java.util.concurrent.ScheduledExecutorService;
20  
21  import com.linecorp.armeria.client.ClientBuilder;
22  import com.linecorp.armeria.client.ClientRequestContext;
23  import com.linecorp.armeria.client.WebClient;
24  import com.linecorp.armeria.client.encoding.DecodingClient;
25  import com.linecorp.armeria.client.endpoint.EndpointGroup;
26  import com.linecorp.centraldogma.client.CentralDogma;
27  import com.linecorp.centraldogma.internal.client.ReplicationLagTolerantCentralDogma;
28  
29  /**
30   * Builds a {@link CentralDogma} client based on an <a href="https://line.github.io/armeria/">Armeria</a>
31   * HTTP client.
32   */
33  public final class ArmeriaCentralDogmaBuilder
34          extends AbstractArmeriaCentralDogmaBuilder<ArmeriaCentralDogmaBuilder> {
35      /**
36       * Returns a newly-created {@link CentralDogma} instance.
37       *
38       * @throws UnknownHostException if failed to resolve the host names from the DNS servers
39       */
40      public CentralDogma build() throws UnknownHostException {
41          final EndpointGroup endpointGroup = endpointGroup();
42          final String scheme = "none+" + (isUseTls() ? "https" : "http");
43          final ClientBuilder builder =
44                  newClientBuilder(scheme, endpointGroup, cb -> cb.decorator(DecodingClient.newDecorator()), "/");
45          final int maxRetriesOnReplicationLag = maxNumRetriesOnReplicationLag();
46  
47          // TODO(ikhoon): Apply ExecutorServiceMetrics for the 'blockingTaskExecutor' once
48          //               https://github.com/line/centraldogma/pull/542 is merged.
49          final ScheduledExecutorService blockingTaskExecutor = blockingTaskExecutor();
50  
51          final CentralDogma dogma = new ArmeriaCentralDogma(blockingTaskExecutor,
52                                                             builder.build(WebClient.class),
53                                                             accessToken());
54          if (maxRetriesOnReplicationLag <= 0) {
55              return dogma;
56          } else {
57              return new ReplicationLagTolerantCentralDogma(
58                      blockingTaskExecutor, dogma, maxRetriesOnReplicationLag,
59                      retryIntervalOnReplicationLagMillis(),
60                      () -> {
61                          // FIXME(trustin): Note that this will always return `null` due to a known limitation
62                          //                 in Armeria: https://github.com/line/armeria/issues/760
63                          final ClientRequestContext ctx = ClientRequestContext.currentOrNull();
64                          return ctx != null ? ctx.remoteAddress() : null;
65                      });
66          }
67      }
68  }