-
Notifications
You must be signed in to change notification settings - Fork 925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation for preprocessors #6051
Open
jrhee17
wants to merge
2
commits into
line:main
Choose a base branch
from
jrhee17:docs/preprocessor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
# Client Preprocessors | ||
|
||
A 'preprocessor' is a [decorator] that intercepts an outgoing request and allows users to | ||
customize certain properties before entering the decorating chain. These properties most | ||
notably include <type://EndpointGroup>, <type://SessionProtocol> and <type://EventLoop>. | ||
|
||
A preprocessor may be added when building a client: | ||
|
||
```java | ||
HttpPreprocessor preprocessor = ... | ||
WebClient client = WebClient.of(preprocessor); | ||
``` | ||
|
||
or by adding a client option: | ||
|
||
```java | ||
HttpPreprocessor preprocessor = ... | ||
WebClient client = WebClient.builder() | ||
.preprocessor(preprocessor) | ||
.build(); | ||
``` | ||
|
||
or both: | ||
|
||
```java | ||
HttpPreprocessor preprocessor1 = ... | ||
HttpPreprocessor preprocessor2 = ... | ||
WebClient client = WebClient.builder(preprocessor1) | ||
.preprocessor(preprocessor2) | ||
.build(); | ||
``` | ||
|
||
## Implementing `HttpPreprocessor` and `RpcPreprocessor` | ||
|
||
<type://HttpPreprocessor> and <type://RpcPreprocessor> expose a <type://PartialClientRequestContext>. | ||
Users may influence the finalized <type://ClientRequestContext> by calling methods such as | ||
<type://PartialClientRequestContext#endpointGroup(EndpointGroup)> and | ||
<type://PartialClientRequestContext#sessionProtocol(SessionProtocol)>. | ||
|
||
In the following example, HTTP requests are routed to a different endpoint based on the request header. | ||
|
||
```java | ||
WebClient client = WebClient.of((delegate, ctx, req) -> { | ||
if (req.headers().contains("x-canary")) { | ||
ctx.endpointGroup(Endpoint.of("canary.dev")); | ||
} else { | ||
ctx.endpointGroup(Endpoint.of("non-canary.dev")); | ||
} | ||
return delegate.execute(ctx, req); | ||
}); | ||
``` | ||
|
||
Note that once the request is passed to the decorator chain, modifying the above properties are not | ||
allowed. | ||
|
||
```java | ||
WebClient client = WebClient.of((delegate, ctx, req) -> { | ||
HttpResponse res = delegate.execute(ctx, req); | ||
ctx.endpointGroup(Endpoint.of("some-endpoint")); // this is not allowed | ||
return res; | ||
}); | ||
``` | ||
|
||
Implementing <type://RpcPreprocessor> is not very different from <type://HttpPreprocessor> except that | ||
the parameters are <type://RpcRequest> and <type://RpcResponse>. As such, <type://RpcPreprocessor> can | ||
only be added to RPC clients such as those built by <type://ThriftClients>. | ||
|
||
```java | ||
RpcPreprocessor rpcPreprocessor1 = ...; | ||
RpcPreprocessor rpcPreprocessor2 = ...; | ||
HelloService.Iface helloService = ThriftClients | ||
.builder(rpcPreprocessor1) | ||
.rpcPreprocessor(rpcPreprocessor2) | ||
.build(HelloService.Iface.class); | ||
``` | ||
|
||
## The order of preprocessors | ||
|
||
Similar to decorators, preprocessors are also executed in reverse order of insertion. | ||
The following example shows which order preprocessors are executed by printing messages. | ||
|
||
```java | ||
WebClient client = WebClient | ||
.builder((delegate, ctx, req) -> { | ||
System.err.println("Preprocessor 3"); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.decorator((delegate, ctx, req) -> { | ||
System.err.println("Decorator 1"); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.preprocessor((delegate, ctx, req) -> { | ||
System.err.println("Preprocessor 2"); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.preprocessor((delegate, ctx, req) -> { | ||
System.err.println("Preprocessor 1"); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.build(); | ||
HttpRequest httpRequest = ...; | ||
client.execute(httpRequest) | ||
``` | ||
|
||
The following diagram describes how an HTTP request goes through preprocessors and decorators. | ||
Note that decorators are executed after all preprocessors are executed. | ||
|
||
```bob-svg | ||
+-----------+ req +--------------+ req +--------------+ req +--------------+ req +----------+ req +-----------+ req +------------------+ req +--------+ | ||
| |------>| |------>| |------>| |------>| |------>| |------>| |------>| | | ||
| WebClient | | #1 | | #2 | | #3 | | Finalize | | #1 | | Armeria | | Server | | ||
| | res | preprocessor | res | preprocessor | res | preprocessor | res | Context | res | decorator | res | Networking Layer | res | | | ||
| |<------| |<------| |<------| |<------| |<------| |<------| |<------| | | ||
+-----------+ +--------------+ +--------------+ +--------------+ +----------+ +-----------+ +------------------+ +--------+ | ||
``` | ||
|
||
<type://RpcPreprocessor>s are executed similarly to <typeplural://HttpPreprocessor>. | ||
|
||
```java | ||
HelloService.Iface helloService = ThriftClients | ||
.builder((delegate, ctx, req) -> { | ||
System.err.println("RpcPreprocessor 3"); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.rpcPreprocessor((delegate, ctx, req) -> { | ||
System.err.println("RpcPreprocessor 2"); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.rpcPreprocessor((delegate, ctx, req) -> { | ||
System.err.println("RpcPreprocessor 1"); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.build(HelloService.Iface.class); | ||
``` | ||
|
||
```bob-svg | ||
+-----------+ req +-----------------+ req +-----------------+ req +-----------------+ req +----------+ req +-----------+ | ||
| |------>| |------>| |------>| |------>| |------>| | | ||
| WebClient | | #1 | | #2 | | #3 | | Finalize | | Decorator | | ||
| | res | rpcPreprocessor | res | rpcPreprocessor | res | rpcPreprocessor | res | Context | res | Chain | | ||
| |<------| |<------| |<------| |<------| |<------| | | ||
+-----------+ +-----------------+ +-----------------+ +-----------------+ +----------+ +-----------+ | ||
``` | ||
|
||
Note that unlike decorators, <typeplural://HttpPreprocessor> are not executed for RPC-based clients. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
## Determining which property is finally chosen | ||
|
||
Preprocessors are an extension point which allow users to customize the request created by a client. | ||
As such, requests created by clients may be overwritten by preprocessors. | ||
|
||
In the following example, the <type://SessionProtocol> will be overwritten from `HTTP` to `HTTPS`. | ||
|
||
```java | ||
WebClient client = WebClient | ||
.builder() | ||
.preprocessor((delegate, ctx, req) -> { | ||
ctx.sessionProtocol(SessionProtocol.HTTPS); // overwrite to HTTPS | ||
return delegate.execute(ctx, req); | ||
}) | ||
.build(); | ||
client.get("http://some-endpoint"); | ||
``` | ||
|
||
If a client is built exclusively by preprocessors, then the preprocessors are responsible for setting the | ||
necessary properties. At the very least, <type://PartialClientRequestContext#endpointGroup(EndpointGroup)> and | ||
<type://PartialClientRequestContext#sessionProtocol(SessionProtocol)> must be set by the final preprocessor. If | ||
a <type://SessionProtocol> and <type://EndpointGroup> are not specified, the request will fail early | ||
before entering the decorator chain. | ||
|
||
In the example below, it is unclear which endpoint the client should make a request to and the request will fail. | ||
|
||
```java | ||
WebClient client = WebClient | ||
.of((delegate, ctx, req) -> { | ||
ctx.endpointGroup(Endpoint.of("127.0.0.1")); | ||
return delegate.execute(ctx, req); | ||
}); | ||
client.get("/"). | ||
``` | ||
|
||
If multiple preprocessors are specified, then preprocessors invoked later can override values | ||
set from previous preprocessors. | ||
|
||
In the example below, the <type://SessionProtocol> has been overwritten to use `HTTP`. | ||
|
||
```java | ||
WebClient client = WebClient | ||
.builder((delegate, ctx, req) -> { | ||
ctx.sessionProtocol(SessionProtocol.HTTP); // the protocol is overwritten to HTTP | ||
return delegate.execute(ctx, req); | ||
}) | ||
.preprocessor((delegate, ctx, req) -> { | ||
ctx.endpointGroup(Endpoint.of("127.0.0.1")); // the endpoint is not overwritten | ||
ctx.sessionProtocol(SessionProtocol.HTTPS); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.build(); | ||
``` | ||
|
||
If a client specifies an <type://EndpointGroup> or an absolute `URI`, the specified values will be used unless overridden | ||
by preprocessors. In the example below, all requests will be made with `HTTPS`. | ||
|
||
```java | ||
// Client built with an EndpointGroup | ||
WebClient client = WebClient | ||
.builder(SessionProtocol.HTTP, Endpoint.of("1.2.3.4")) | ||
.preprocessor((delegate, ctx, req) -> { | ||
ctx.sessionProtocol(SessionProtocol.HTTPS); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.build(); | ||
client.get("/"); | ||
|
||
// Client built with an absolute URI | ||
WebClient client = WebClient | ||
.builder("http://1.2.3.4") | ||
.preprocessor((delegate, ctx, req) -> { | ||
ctx.sessionProtocol(SessionProtocol.HTTPS); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.build(); | ||
client.get("/"); | ||
|
||
// Client request with an absolute URI | ||
WebClient client = WebClient | ||
.builder() | ||
.preprocessor((delegate, ctx, req) -> { | ||
ctx.sessionProtocol(SessionProtocol.HTTPS); | ||
return delegate.execute(ctx, req); | ||
}) | ||
.build(); | ||
client.get("http://1.2.3.4/"); | ||
``` | ||
|
||
## See also | ||
|
||
- [Decorating a service](/docs/server-decorator) | ||
|
||
[decorator]: https://en.wikipedia.org/wiki/Decorator_pattern |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can understand what
Partial
means, but it might be ambiguous for normal users.What do you think of prefixing
Pre
to imply it is used in*Preprocessor
?