-
Notifications
You must be signed in to change notification settings - Fork 926
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation: This PR introduces the notion of `Preprocessor`s and allows users to configure these to clients as options. The second part of this PR will introduce a way for users to solely create a client based on `Preprocessor`s. The eventual POC can be found here: https://github.com/jrhee17/armeria/pull/36/files Eventually this extension point will also make it easier/clearer for users to use xDS with existing Armeria APIs. The full capability/limitations/design of `Preprocessors` are better described in the following PR: #6051 Modifications: - Introduced `Preprocessor` and `PreClient` APIs - Added `ClientPreprocessors` and `ClientPreprocessorsBuilder` to allow users to easily add `Preprocessor`s to clients as options - Modified `DefaultWebClient`, `DefaultTHttpClient`, and `ArmeriaClientCall` to use `Preprocessor`s - In order to allow users a way to overwrite the chosen `EndpointGroup`, the `EndpointGroup` is now specified when creating a `ClientRequestContext` instead of at initialization time. - Modified `ClientUtil` methods to pass an additional `req` field which signifies the original request for type-safety. Result: - Users can specify `Preprocessor`s when creating a client. <!-- Visit this URL to learn more about how to write a pull request description: https://armeria.dev/community/developer-guide#how-to-write-pull-request-description -->
- Loading branch information
Showing
51 changed files
with
1,517 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
core/src/main/java/com/linecorp/armeria/client/ClientPreprocessors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* 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.List; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import com.linecorp.armeria.common.annotation.UnstableApi; | ||
|
||
/** | ||
* A set of {@link Function}s that transforms a {@link HttpPreprocessor} or | ||
* {@link RpcPreprocessor} into another. | ||
*/ | ||
@UnstableApi | ||
public final class ClientPreprocessors { | ||
|
||
private static final ClientPreprocessors NONE = | ||
new ClientPreprocessors(ImmutableList.of(), ImmutableList.of()); | ||
|
||
/** | ||
* Returns an empty {@link ClientDecoration} which does not decorate a {@link Client}. | ||
*/ | ||
public static ClientPreprocessors of() { | ||
return NONE; | ||
} | ||
|
||
/** | ||
* Creates a new instance from a single {@link HttpPreprocessor}. | ||
* | ||
* @param preprocessor the {@link HttpPreprocessor} that transforms an | ||
* {@link HttpPreClient} to another | ||
*/ | ||
public static ClientPreprocessors of(HttpPreprocessor preprocessor) { | ||
return builder().add(preprocessor).build(); | ||
} | ||
|
||
/** | ||
* Creates a new instance from a single {@link RpcPreprocessor}. | ||
* | ||
* @param preprocessor the {@link RpcPreprocessor} that transforms an {@link RpcPreClient} | ||
* to another | ||
*/ | ||
public static ClientPreprocessors ofRpc(RpcPreprocessor preprocessor) { | ||
return builder().addRpc(preprocessor).build(); | ||
} | ||
|
||
/** | ||
* Returns a newly created {@link ClientPreprocessorsBuilder}. | ||
*/ | ||
public static ClientPreprocessorsBuilder builder() { | ||
return new ClientPreprocessorsBuilder(); | ||
} | ||
|
||
private final List<HttpPreprocessor> preprocessors; | ||
private final List<RpcPreprocessor> rpcPreprocessors; | ||
|
||
ClientPreprocessors(List<HttpPreprocessor> preprocessors, List<RpcPreprocessor> rpcPreprocessors) { | ||
this.preprocessors = ImmutableList.copyOf(preprocessors); | ||
this.rpcPreprocessors = ImmutableList.copyOf(rpcPreprocessors); | ||
} | ||
|
||
/** | ||
* Returns the HTTP-level preprocessors. | ||
*/ | ||
public List<HttpPreprocessor> preprocessors() { | ||
return preprocessors; | ||
} | ||
|
||
/** | ||
* Returns the RPC-level preprocessors. | ||
*/ | ||
public List<RpcPreprocessor> rpcPreprocessors() { | ||
return rpcPreprocessors; | ||
} | ||
|
||
/** | ||
* Decorates the specified {@link HttpPreClient} using preprocessors. | ||
* | ||
* @param execution the {@link HttpPreClient} being decorated | ||
*/ | ||
public HttpPreClient decorate(HttpPreClient execution) { | ||
for (HttpPreprocessor preprocessor : preprocessors) { | ||
final HttpPreClient execution0 = execution; | ||
execution = (ctx, req) -> preprocessor.execute(execution0, ctx, req); | ||
} | ||
return execution; | ||
} | ||
|
||
/** | ||
* Decorates the specified {@link RpcPreClient} using preprocessors. | ||
* | ||
* @param execution the {@link RpcPreClient} being decorated | ||
*/ | ||
public RpcPreClient rpcDecorate(RpcPreClient execution) { | ||
for (RpcPreprocessor rpcPreprocessor : rpcPreprocessors) { | ||
final RpcPreClient execution0 = execution; | ||
execution = (ctx, req) -> rpcPreprocessor.execute(execution0, ctx, req); | ||
} | ||
return execution; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) { | ||
return true; | ||
} | ||
if (object == null || getClass() != object.getClass()) { | ||
return false; | ||
} | ||
final ClientPreprocessors that = (ClientPreprocessors) object; | ||
return Objects.equals(preprocessors, that.preprocessors) && | ||
Objects.equals(rpcPreprocessors, that.rpcPreprocessors); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(preprocessors, rpcPreprocessors); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("preprocessors", preprocessors) | ||
.add("rpcPreprocessors", rpcPreprocessors) | ||
.toString(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
core/src/main/java/com/linecorp/armeria/client/ClientPreprocessorsBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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 static java.util.Objects.requireNonNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.linecorp.armeria.common.annotation.UnstableApi; | ||
|
||
/** | ||
* Creates a new {@link ClientPreprocessors} using the builder pattern. | ||
*/ | ||
@UnstableApi | ||
public final class ClientPreprocessorsBuilder { | ||
|
||
private final List<HttpPreprocessor> preprocessors = new ArrayList<>(); | ||
private final List<RpcPreprocessor> rpcPreprocessors = new ArrayList<>(); | ||
|
||
ClientPreprocessorsBuilder() {} | ||
|
||
/** | ||
* Adds the specified {@link ClientPreprocessors}. | ||
*/ | ||
public ClientPreprocessorsBuilder add(ClientPreprocessors preprocessors) { | ||
requireNonNull(preprocessors, "preprocessors"); | ||
preprocessors.preprocessors().forEach(this::add); | ||
preprocessors.rpcPreprocessors().forEach(this::addRpc); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds the specified HTTP-level {@code preprocessor}. | ||
* | ||
* @param preprocessor the {@link HttpPreprocessor} that preprocesses an invocation | ||
*/ | ||
public ClientPreprocessorsBuilder add(HttpPreprocessor preprocessor) { | ||
preprocessors.add(requireNonNull(preprocessor, "preprocessor")); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds the specified RPC-level {@code preprocessor}. | ||
* | ||
* @param rpcPreprocessor the {@link HttpPreprocessor} that preprocesses an invocation | ||
*/ | ||
public ClientPreprocessorsBuilder addRpc(RpcPreprocessor rpcPreprocessor) { | ||
rpcPreprocessors.add(requireNonNull(rpcPreprocessor, "rpcPreprocessor")); | ||
return this; | ||
} | ||
|
||
/** | ||
* Returns a newly-created {@link ClientPreprocessors} based on the decorators added to this builder. | ||
*/ | ||
public ClientPreprocessors build() { | ||
return new ClientPreprocessors(preprocessors, rpcPreprocessors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.