Skip to content

Commit

Permalink
poc
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhee17 committed Dec 18, 2024
1 parent 40ded96 commit 05a7ca1
Show file tree
Hide file tree
Showing 20 changed files with 461 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ public ResponseTimeoutMode responseTimeoutMode() {
return get(RESPONSE_TIMEOUT_MODE);
}

public ClientPreprocessors clientPreprocessors() {
return ClientPreprocessors.of();
}

/**
* Returns a new {@link ClientOptionsBuilder} created from this {@link ClientOptions}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.client;

import com.linecorp.armeria.common.Request;
import com.linecorp.armeria.common.Response;

/**
* TBU.
*/
@FunctionalInterface
public interface ClientPreprocessor<I extends Request, O extends Response> {

/**
* TBU.
*/
O execute(ClientRequestContext ctx, I req, RequestOptions requestOptions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.client;

import java.util.function.Function;

import com.google.common.collect.ImmutableList;

/**
* A set of {@link Function}s that transforms a {@link Client} into another.
*/
public final class ClientPreprocessors {

private static final ClientPreprocessors NONE = new ClientPreprocessors(Function.identity(), Function.identity());

/**
* Returns an empty {@link ClientPreprocessors} which does not decorate a {@link Client}.
*/
public static ClientPreprocessors of() {
return NONE;
}

/**
* Creates a new instance from a single decorator {@link Function}.
*
* @param decorator the {@link Function} that transforms an {@link HttpClient} to another
*/
public static ClientPreprocessors of(
Function<? super HttpPreprocessor, ? extends HttpPreprocessor> decorator) {
return new ClientPreprocessors(decorator, Function.identity());
}

/**
* Creates a new instance from a single decorator {@link Function}.
*
* @param decorator the {@link Function} that transforms an {@link RpcClient} to another
*/
public static ClientPreprocessors ofRpc(Function<? super RpcPreprocessor, ? extends RpcPreprocessor> decorator) {
return new ClientPreprocessors(Function.identity(), decorator);
}

private final Function<? super HttpPreprocessor, ? extends HttpPreprocessor> preprocessorFn;
private final Function<? super RpcPreprocessor, ? extends RpcPreprocessor> rpcPreprocessorFn;

ClientPreprocessors(Function<? super HttpPreprocessor, ? extends HttpPreprocessor> preprocessorFn,
Function<? super RpcPreprocessor, ? extends RpcPreprocessor> rpcPreprocessorFn) {
this.preprocessorFn = preprocessorFn;
this.rpcPreprocessorFn = rpcPreprocessorFn;
}

/**
* Decorates the specified {@link HttpPreprocessor} using the decorator.
*
* @param preprocessor the {@link HttpPreprocessor} being decorated
*/
public HttpPreprocessor decorate(HttpPreprocessor preprocessor) {
return preprocessorFn.apply(preprocessor);
}

/**
* Decorates the specified {@link RpcPreprocessor} using the decorator.
*
* @param rpcPreprocessor the {@link RpcPreprocessor} being decorated
*/
public RpcPreprocessor rpcDecorate(RpcPreprocessor rpcPreprocessor) {
return rpcPreprocessorFn.apply(rpcPreprocessor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.client;

import com.linecorp.armeria.common.Request;
import com.linecorp.armeria.common.Response;
import com.linecorp.armeria.common.annotation.UnstableApi;

@UnstableApi
@FunctionalInterface
public interface DecoratingClientPreprocessor<I extends Request, O extends Response> {

O execute(ClientPreprocessor<I, O> delegate, ClientRequestContext ctx, I req, RequestOptions requestOptions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.linecorp.armeria.common.Scheme;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.client.DefaultClientRequestContext;
import com.linecorp.armeria.internal.client.PreprocessorAttributeKeys;

import io.micrometer.core.instrument.MeterRegistry;

Expand Down Expand Up @@ -113,12 +115,17 @@ public HttpResponse execute(HttpRequest req, RequestOptions requestOptions) {
newReq = req.withHeaders(req.headers().toBuilder().path(newPath));
}

return execute(protocol,
endpointGroup,
newReq.method(),
reqTarget,
newReq,
requestOptions);
final RequestOptions reqOptions = requestOptions
.toBuilder()
.attr(PreprocessorAttributeKeys.FUTURE_CONVERTER_KEY, futureConverter())
.attr(PreprocessorAttributeKeys.ERROR_RESPONSE_FACTORY_KEY, errorResponseFactory())
.attr(PreprocessorAttributeKeys.ENDPOINT_GROUP_KEY, endpointGroup)
.build();
final DefaultClientRequestContext ctx = new DefaultClientRequestContext(
protocol, newReq, null, reqTarget,
reqOptions, options());
return options().clientPreprocessors().decorate(TailClientPreprocessor.of(unwrap()))
.execute(ctx, newReq, reqOptions);
}

private static HttpResponse abortRequestAndReturnFailureResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.client;

import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpResponse;

@FunctionalInterface
public interface HttpPreprocessor extends ClientPreprocessor<HttpRequest, HttpResponse> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,11 @@ private void execute0(ClientRequestContext ctx, RedirectContext redirectCtx,
return;
}

final HttpRequest ctxReq = derivedCtx.request();
assert ctxReq != null;
final HttpResponse response = executeWithFallback(unwrap(), derivedCtx,
(context, cause) -> HttpResponse.ofFailure(cause));
(context, cause) -> HttpResponse.ofFailure(cause),
ctxReq);
derivedCtx.log().whenAvailable(RequestLogProperty.RESPONSE_HEADERS).thenAccept(log -> {
if (log.isAvailable(RequestLogProperty.RESPONSE_CAUSE)) {
final Throwable cause = log.responseCause();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.client;

import com.linecorp.armeria.common.RpcRequest;
import com.linecorp.armeria.common.RpcResponse;

@FunctionalInterface
public interface RpcPreprocessor extends ClientPreprocessor<RpcRequest, RpcResponse> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.client;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Function;

import com.linecorp.armeria.client.endpoint.EndpointGroup;
import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.Request;
import com.linecorp.armeria.common.Response;
import com.linecorp.armeria.common.RpcRequest;
import com.linecorp.armeria.common.RpcResponse;
import com.linecorp.armeria.internal.client.ClientRequestContextExtension;
import com.linecorp.armeria.internal.client.ClientUtil;
import com.linecorp.armeria.internal.client.PreprocessorAttributeKeys;

public final class TailClientPreprocessor<I extends Request, O extends Response> implements ClientPreprocessor<I, O> {

private final Client<I, O> delegate;

private TailClientPreprocessor(Client<I, O> delegate) {
this.delegate = delegate;
}

public static HttpPreprocessor of(HttpClient httpClient) {
final TailClientPreprocessor<HttpRequest, HttpResponse> tail = new TailClientPreprocessor<>(httpClient);
return tail::execute;
}

public static RpcPreprocessor ofRpc(RpcClient rpcClient) {
final TailClientPreprocessor<RpcRequest, RpcResponse> tail = new TailClientPreprocessor<>(rpcClient);
return tail::execute;
}

@Override
public O execute(ClientRequestContext ctx, I req, RequestOptions requestOptions) {
final Function<CompletableFuture<O>, O> futureConverter =
(Function<CompletableFuture<O>, O>) requestOptions.attrs().get(
PreprocessorAttributeKeys.FUTURE_CONVERTER_KEY);
final BiFunction<ClientRequestContext, Throwable, O> errorResponseFactory =
(BiFunction<ClientRequestContext, Throwable, O>) requestOptions.attrs().get(
PreprocessorAttributeKeys.ERROR_RESPONSE_FACTORY_KEY);
final EndpointGroup endpointGroup = (EndpointGroup) requestOptions.attrs().get(
PreprocessorAttributeKeys.ENDPOINT_GROUP_KEY);
assert futureConverter != null;
assert errorResponseFactory != null;
assert endpointGroup != null;
final ClientRequestContextExtension ctxExt = ctx.as(ClientRequestContextExtension.class);
assert ctxExt != null;
return ClientUtil.initContextAndExecuteWithFallback(delegate, ctxExt, endpointGroup,
futureConverter, errorResponseFactory, req);
}
}
41 changes: 6 additions & 35 deletions core/src/main/java/com/linecorp/armeria/client/UserClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.linecorp.armeria.client;

import static com.linecorp.armeria.internal.client.ClientUtil.initContextAndExecuteWithFallback;

import java.net.URI;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand All @@ -28,19 +26,15 @@

import com.linecorp.armeria.client.endpoint.EndpointGroup;
import com.linecorp.armeria.common.HttpMethod;
import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.Request;
import com.linecorp.armeria.common.RequestId;
import com.linecorp.armeria.common.RequestTarget;
import com.linecorp.armeria.common.Response;
import com.linecorp.armeria.common.RpcRequest;
import com.linecorp.armeria.common.RpcResponse;
import com.linecorp.armeria.common.Scheme;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.util.AbstractUnwrappable;
import com.linecorp.armeria.common.util.SystemInfo;
import com.linecorp.armeria.internal.client.DefaultClientRequestContext;

import io.micrometer.core.instrument.MeterRegistry;

Expand Down Expand Up @@ -170,37 +164,14 @@ protected final O execute(SessionProtocol protocol, EndpointGroup endpointGroup,
*/
protected final O execute(SessionProtocol protocol, EndpointGroup endpointGroup, HttpMethod method,
RequestTarget reqTarget, I req, RequestOptions requestOptions) {
throw new UnsupportedOperationException();
}

final HttpRequest httpReq;
final RpcRequest rpcReq;
final RequestId id = nextRequestId();

if (req instanceof HttpRequest) {
httpReq = (HttpRequest) req;
rpcReq = null;
} else {
httpReq = null;
rpcReq = (RpcRequest) req;
}

final DefaultClientRequestContext ctx = new DefaultClientRequestContext(
meterRegistry, protocol, id, method, reqTarget, options(), httpReq, rpcReq,
requestOptions, System.nanoTime(), SystemInfo.currentTimeMicros());

return initContextAndExecuteWithFallback(unwrap(), ctx, endpointGroup,
futureConverter, errorResponseFactory);
protected Function<CompletableFuture<O>, O> futureConverter() {
return futureConverter;
}

private RequestId nextRequestId() {
final RequestId id = options().requestIdGenerator().get();
if (id == null) {
if (!warnedNullRequestId) {
warnedNullRequestId = true;
logger.warn("requestIdGenerator.get() returned null; using RequestId.random()");
}
return RequestId.random();
} else {
return id;
}
protected BiFunction<ClientRequestContext, Throwable, O> errorResponseFactory() {
return errorResponseFactory;
}
}
Loading

0 comments on commit 05a7ca1

Please sign in to comment.