|
| 1 | +/* |
| 2 | + * Copyright 2025 LY Corporation |
| 3 | + * |
| 4 | + * LY 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 | + |
| 17 | +package com.linecorp.armeria.xds; |
| 18 | + |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +import com.linecorp.armeria.client.ClientDecoration; |
| 22 | +import com.linecorp.armeria.client.ClientRequestContext; |
| 23 | +import com.linecorp.armeria.client.ClientTlsSpec; |
| 24 | +import com.linecorp.armeria.client.Endpoint; |
| 25 | +import com.linecorp.armeria.client.HttpClient; |
| 26 | +import com.linecorp.armeria.client.HttpPreprocessor; |
| 27 | +import com.linecorp.armeria.client.PreClient; |
| 28 | +import com.linecorp.armeria.client.PreClientRequestContext; |
| 29 | +import com.linecorp.armeria.client.RpcClient; |
| 30 | +import com.linecorp.armeria.client.RpcPreprocessor; |
| 31 | +import com.linecorp.armeria.common.HttpRequest; |
| 32 | +import com.linecorp.armeria.common.HttpResponse; |
| 33 | +import com.linecorp.armeria.common.Request; |
| 34 | +import com.linecorp.armeria.common.Response; |
| 35 | +import com.linecorp.armeria.common.RpcRequest; |
| 36 | +import com.linecorp.armeria.common.RpcResponse; |
| 37 | +import com.linecorp.armeria.common.SessionProtocol; |
| 38 | +import com.linecorp.armeria.common.annotation.Nullable; |
| 39 | +import com.linecorp.armeria.internal.client.ClientRequestContextExtension; |
| 40 | +import com.linecorp.armeria.xds.client.endpoint.XdsEndpointGroup; |
| 41 | +import com.linecorp.armeria.xds.client.endpoint.XdsLoadBalancer; |
| 42 | +import com.linecorp.armeria.xds.internal.DelegatingHttpClient; |
| 43 | +import com.linecorp.armeria.xds.internal.DelegatingRpcClient; |
| 44 | +import com.linecorp.armeria.xds.internal.XdsCommonUtil; |
| 45 | + |
| 46 | +/** |
| 47 | + * A factory which injects cluster-related filters. |
| 48 | + * <pre>{@code |
| 49 | + * [downstream preprocessors] |
| 50 | + * -> [router preprocessor] |
| 51 | + * -> [cluster preprocessor] |
| 52 | + * -> [endpoint selection] |
| 53 | + * -> [retry decorator] |
| 54 | + * -> [cluster decorator] |
| 55 | + * -> [upstream decorators] |
| 56 | + * }</pre> |
| 57 | + */ |
| 58 | +final class ClusterFilterFactory { |
| 59 | + |
| 60 | + static final ClientDecoration DECORATION = |
| 61 | + ClientDecoration.builder() |
| 62 | + .add(ClusterFilterFactory::applyHttpClusterSettings) |
| 63 | + .addRpc(ClusterFilterFactory::applyRpcClusterSettings) |
| 64 | + .build(); |
| 65 | + |
| 66 | + private static final HttpClient CLUSTER_ONLY_HTTP_CLIENT = |
| 67 | + DECORATION.decorate(DelegatingHttpClient.of()); |
| 68 | + private static final RpcClient CLUSTER_ONLY_RPC_CLIENT = |
| 69 | + DECORATION.rpcDecorate(DelegatingRpcClient.of()); |
| 70 | + |
| 71 | + private final XdsEndpointGroup endpointGroup; |
| 72 | + private final SessionProtocol sessionProtocol; |
| 73 | + |
| 74 | + ClusterFilterFactory(XdsLoadBalancer loadBalancer, |
| 75 | + TransportSocketSnapshot transportSocket) { |
| 76 | + endpointGroup = XdsEndpointGroup.of(loadBalancer); |
| 77 | + sessionProtocol = transportSocket.clientTlsSpec() != null ? |
| 78 | + SessionProtocol.HTTPS : SessionProtocol.HTTP; |
| 79 | + } |
| 80 | + |
| 81 | + HttpPreprocessor httpPreprocessor() { |
| 82 | + return this::execute; |
| 83 | + } |
| 84 | + |
| 85 | + RpcPreprocessor rpcPreprocessor() { |
| 86 | + return this::execute; |
| 87 | + } |
| 88 | + |
| 89 | + private <I extends Request, O extends Response> O execute( |
| 90 | + PreClient<I, O> delegate, PreClientRequestContext ctx, I req) throws Exception { |
| 91 | + ctx.setEndpointGroup(endpointGroup); |
| 92 | + ctx.setSessionProtocol(sessionProtocol); |
| 93 | + |
| 94 | + final RouteEntry route = ctx.attr(XdsCommonUtil.SELECTED_ROUTE); |
| 95 | + final ClientRequestContextExtension ctxExt = ctx.as(ClientRequestContextExtension.class); |
| 96 | + if (ctxExt != null) { |
| 97 | + final HttpClient httpClient = route != null ? |
| 98 | + route.httpClient() : CLUSTER_ONLY_HTTP_CLIENT; |
| 99 | + final RpcClient rpcClient = route != null ? |
| 100 | + route.rpcClient() : CLUSTER_ONLY_RPC_CLIENT; |
| 101 | + ctxExt.httpClientCustomizer(actualClient -> { |
| 102 | + DelegatingHttpClient.setDelegate(ctx, actualClient); |
| 103 | + return httpClient; |
| 104 | + }); |
| 105 | + ctxExt.rpcClientCustomizer(actualClient -> { |
| 106 | + DelegatingRpcClient.setDelegate(ctx, actualClient); |
| 107 | + return rpcClient; |
| 108 | + }); |
| 109 | + } |
| 110 | + return delegate.execute(ctx, req); |
| 111 | + } |
| 112 | + |
| 113 | + // Decorator logic — applies per-endpoint cluster settings (TLS) |
| 114 | + |
| 115 | + private static HttpResponse applyHttpClusterSettings( |
| 116 | + HttpClient delegate, ClientRequestContext ctx, HttpRequest req) throws Exception { |
| 117 | + applyClusterSettings(ctx); |
| 118 | + return delegate.execute(ctx, req); |
| 119 | + } |
| 120 | + |
| 121 | + private static RpcResponse applyRpcClusterSettings( |
| 122 | + RpcClient delegate, ClientRequestContext ctx, RpcRequest req) throws Exception { |
| 123 | + applyClusterSettings(ctx); |
| 124 | + return delegate.execute(ctx, req); |
| 125 | + } |
| 126 | + |
| 127 | + private static void applyClusterSettings(ClientRequestContext ctx) { |
| 128 | + final Endpoint endpoint = ctx.endpoint(); |
| 129 | + if (endpoint == null) { |
| 130 | + return; |
| 131 | + } |
| 132 | + final TransportSocketSnapshot transportSocket = |
| 133 | + endpoint.attr(XdsCommonUtil.TRANSPORT_SOCKET_SNAPSHOT_KEY); |
| 134 | + if (transportSocket == null) { |
| 135 | + return; |
| 136 | + } |
| 137 | + @Nullable |
| 138 | + ClientTlsSpec clientTlsSpec = transportSocket.clientTlsSpec(); |
| 139 | + if (clientTlsSpec == null) { |
| 140 | + return; |
| 141 | + } |
| 142 | + final Set<String> alpnOverride = ctx.attr(XdsCommonUtil.ALPN_OVERRIDE_KEY); |
| 143 | + if (alpnOverride != null && !alpnOverride.isEmpty()) { |
| 144 | + clientTlsSpec = clientTlsSpec.toBuilder().alpnProtocols(alpnOverride).build(); |
| 145 | + } |
| 146 | + ctx.setClientTlsSpec(clientTlsSpec); |
| 147 | + } |
| 148 | +} |
0 commit comments