Skip to content
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

Migrate ON_CONNECTION_SELECTED_CONSUMER to request context #2239

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.servicetalk.buffer.api.Buffer;
import io.servicetalk.concurrent.PublisherSource;
import io.servicetalk.concurrent.api.AsyncContext;
import io.servicetalk.concurrent.api.Single;
import io.servicetalk.concurrent.api.TerminalSignalConsumer;
import io.servicetalk.context.api.ContextMap;
Expand Down Expand Up @@ -82,18 +81,17 @@ final Single<StreamingHttpResponse> trackLifecycle(@Nullable final ConnectionInf
return defer(() -> {
final HttpExchangeObserver onExchange = safeReport(observer::onNewExchange, observer, "onNewExchange",
NoopHttpExchangeObserver.INSTANCE);
final boolean clearAsyncContext;
final Runnable clearContext;
if (connInfo != null) {
safeReport(onExchange::onConnectionSelected, connInfo, onExchange, "onConnectionSelected");
clearAsyncContext = false;
clearContext = () -> { /* noop */ };
} else {
// Pass it down to LoadBalancedStreamingHttpClient
// FIXME: switch to RequestContext when it's available
AsyncContext.put(ON_CONNECTION_SELECTED_CONSUMER, selectedConnection -> safeReport(
// Pass it down to LoadBalancedStreamingHttpClient and clear inside `onExchangeFinally`:
request.context().put(ON_CONNECTION_SELECTED_CONSUMER, selectedConnection -> safeReport(
onExchange::onConnectionSelected, selectedConnection, onExchange, "onConnectionSelected"));
clearAsyncContext = true;
clearContext = () -> request.context().remove(ON_CONNECTION_SELECTED_CONSUMER);
}
final ExchangeContext exchangeContext = new ExchangeContext(onExchange, client, clearAsyncContext);
final ExchangeContext exchangeContext = new ExchangeContext(onExchange, client, clearContext);
final HttpRequestObserver onRequest = safeReport(onExchange::onRequest, request, onExchange,
"onRequest", NoopHttpRequestObserver.INSTANCE);
final StreamingHttpRequest transformed = request
Expand Down Expand Up @@ -178,17 +176,17 @@ private static final class ExchangeContext implements TerminalSignalConsumer {
AtomicIntegerFieldUpdater.newUpdater(ExchangeContext.class, "remaining");

private final HttpExchangeObserver onExchange;
private final boolean clearAsyncContext;
private final Runnable clearContext;
@Nullable
private HttpResponseObserver onResponse;
private volatile int remaining;

private ExchangeContext(final HttpExchangeObserver onExchange, final boolean client,
final boolean clearAsyncContext) {
final Runnable clearContext) {
this.onExchange = onExchange;
// server has to always drain request, but client may fail before request message body starts:
remaining = client ? 1 : 2;
this.clearAsyncContext = clearAsyncContext;
this.clearContext = clearContext;
}

void onResponse(HttpResponseMetaData responseMetaData) {
Expand Down Expand Up @@ -249,9 +247,7 @@ void decrementRemaining() {
if (remainingUpdater.decrementAndGet(this) == 0) {
// Exchange completes only if both request and response terminate
safeReport(onExchange::onExchangeFinally, onExchange, "onExchangeFinally");
if (clearAsyncContext) {
AsyncContext.remove(ON_CONNECTION_SELECTED_CONSUMER);
}
clearContext.run();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package io.servicetalk.http.netty;

import io.servicetalk.client.api.LoadBalancer;
import io.servicetalk.concurrent.api.AsyncContext;
import io.servicetalk.concurrent.api.Completable;
import io.servicetalk.concurrent.api.Single;
import io.servicetalk.concurrent.api.TerminalSignalConsumer;
Expand Down Expand Up @@ -73,7 +72,10 @@ public Single<StreamingHttpResponse> request(final StreamingHttpRequest request)
// following the LoadBalancer API which this Client depends upon to ensure the concurrent request count state is
// correct.
return loadBalancer.selectConnection(SELECTOR_FOR_REQUEST, request.context()).flatMap(c -> {
final Consumer<ConnectionInfo> onConnectionSelected = AsyncContext.get(ON_CONNECTION_SELECTED_CONSUMER);
// Do not remove ON_CONNECTION_SELECTED_CONSUMER from the context to track new connection selections for
// retries and redirects.
final Consumer<ConnectionInfo> onConnectionSelected = request.context()
.get(ON_CONNECTION_SELECTED_CONSUMER);
if (onConnectionSelected != null) {
onConnectionSelected.accept(c.connectionContext());
}
Expand Down