diff --git a/servicetalk-http-api/src/main/java/io/servicetalk/http/api/HttpApiConversions.java b/servicetalk-http-api/src/main/java/io/servicetalk/http/api/HttpApiConversions.java index 584944d32f..76ba9c242a 100644 --- a/servicetalk-http-api/src/main/java/io/servicetalk/http/api/HttpApiConversions.java +++ b/servicetalk-http-api/src/main/java/io/servicetalk/http/api/HttpApiConversions.java @@ -19,6 +19,8 @@ import io.servicetalk.http.api.StreamingHttpClientToBlockingStreamingHttpClient.ReservedStreamingHttpConnectionToBlockingStreaming; import io.servicetalk.http.api.StreamingHttpClientToHttpClient.ReservedStreamingHttpConnectionToReservedHttpConnection; +import static io.servicetalk.http.api.HttpContextKeys.HTTP_EXECUTION_STRATEGY_KEY; + /** * Conversion routines to {@link StreamingHttpService}. */ @@ -486,4 +488,20 @@ public static HttpService toHttpService(StreamingHttpService service) { public static BlockingStreamingHttpService toBlockingStreamingHttpService(StreamingHttpService service) { return new StreamingHttpServiceToBlockingStreamingHttpService(service); } + + /** + * Tries assigning a strategy for a request execution if none is already assigned. + *
+ * Top level user-facing API can optimize execution strategy (offloading) when it's known that users won't interact
+ * with some parts of request-response processing.
+ *
+ * @param metaData request meta-data to assign the strategy to
+ * @param strategy {@link HttpExecutionStrategy} to assign
+ * @see
+ * Programming models
+ */
+ static void assignStrategy(final HttpRequestMetaData metaData, final HttpExecutionStrategy strategy) {
+ metaData.context().putIfAbsent(HTTP_EXECUTION_STRATEGY_KEY, strategy);
+ }
}
diff --git a/servicetalk-http-api/src/main/java/io/servicetalk/http/api/StreamingHttpClientToBlockingHttpClient.java b/servicetalk-http-api/src/main/java/io/servicetalk/http/api/StreamingHttpClientToBlockingHttpClient.java
index 670ad24d7b..34ad25eeeb 100644
--- a/servicetalk-http-api/src/main/java/io/servicetalk/http/api/StreamingHttpClientToBlockingHttpClient.java
+++ b/servicetalk-http-api/src/main/java/io/servicetalk/http/api/StreamingHttpClientToBlockingHttpClient.java
@@ -18,7 +18,7 @@
import io.servicetalk.concurrent.BlockingIterable;
import static io.servicetalk.concurrent.api.internal.BlockingUtils.blockingInvocation;
-import static io.servicetalk.http.api.HttpContextKeys.HTTP_EXECUTION_STRATEGY_KEY;
+import static io.servicetalk.http.api.HttpApiConversions.assignStrategy;
import static io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy;
import static io.servicetalk.http.api.RequestResponseFactories.toAggregated;
import static io.servicetalk.http.api.StreamingHttpConnectionToBlockingHttpConnection.DEFAULT_BLOCKING_CONNECTION_STRATEGY;
@@ -44,7 +44,7 @@ public HttpExecutionStrategy executionStrategy() {
@Override
public ReservedBlockingHttpConnection reserveConnection(final HttpRequestMetaData metaData) throws Exception {
- metaData.context().putIfAbsent(HTTP_EXECUTION_STRATEGY_KEY, strategy);
+ assignStrategy(metaData, strategy);
return blockingInvocation(client.reserveConnection(metaData)
.map(c -> new ReservedStreamingHttpConnectionToReservedBlockingHttpConnection(c, this.strategy,
reqRespFactory)));
@@ -57,7 +57,7 @@ public StreamingHttpClient asStreamingClient() {
@Override
public HttpResponse request(final HttpRequest request) throws Exception {
- request.context().putIfAbsent(HTTP_EXECUTION_STRATEGY_KEY, strategy);
+ assignStrategy(request, strategy);
return BlockingRequestUtils.request(client, request);
}
@@ -89,6 +89,7 @@ public HttpRequest newRequest(final HttpRequestMethod method, final String reque
static final class ReservedStreamingHttpConnectionToReservedBlockingHttpConnection implements
ReservedBlockingHttpConnection {
private final ReservedStreamingHttpConnection connection;
+ private final HttpExecutionStrategy strategy;
private final HttpConnectionContext context;
private final HttpExecutionContext executionContext;
private final HttpRequestResponseFactory reqRespFactory;
@@ -104,13 +105,13 @@ static final class ReservedStreamingHttpConnectionToReservedBlockingHttpConnecti
final HttpExecutionStrategy strategy,
final HttpRequestResponseFactory reqRespFactory) {
- requireNonNull(strategy);
this.connection = requireNonNull(connection);
+ this.strategy = requireNonNull(strategy);
final HttpConnectionContext originalCtx = connection.connectionContext();
executionContext = new DelegatingHttpExecutionContext(connection.executionContext()) {
@Override
public HttpExecutionStrategy executionStrategy() {
- return strategy;
+ return ReservedStreamingHttpConnectionToReservedBlockingHttpConnection.this.strategy;
}
};
context = new DelegatingHttpConnectionContext(originalCtx) {
@@ -144,6 +145,7 @@ public