use of io.servicetalk.context.api.ContextMap in project servicetalk by apple.
the class ContextPreservingSubscriber method onNextSlowPath.
private void onNextSlowPath(T t) {
ContextMap prev = CONTEXT_THREAD_LOCAL.get();
try {
CONTEXT_THREAD_LOCAL.set(saved);
subscriber.onNext(t);
} finally {
CONTEXT_THREAD_LOCAL.set(prev);
}
}
use of io.servicetalk.context.api.ContextMap in project servicetalk by apple.
the class ContextPreservingSubscription method request.
@Override
public void request(long l) {
final Thread currentThread = Thread.currentThread();
if (currentThread instanceof ContextMapHolder) {
final ContextMapHolder asyncContextMapHolder = (ContextMapHolder) currentThread;
ContextMap prev = asyncContextMapHolder.context();
try {
asyncContextMapHolder.context(saved);
subscription.request(l);
} finally {
asyncContextMapHolder.context(prev);
}
} else {
requestSlowPath(l);
}
}
use of io.servicetalk.context.api.ContextMap in project servicetalk by apple.
the class ContextPreservingSubscription method cancel.
@Override
public void cancel() {
final Thread currentThread = Thread.currentThread();
if (currentThread instanceof ContextMapHolder) {
final ContextMapHolder asyncContextMapHolder = (ContextMapHolder) currentThread;
ContextMap prev = asyncContextMapHolder.context();
try {
asyncContextMapHolder.context(saved);
subscription.cancel();
} finally {
asyncContextMapHolder.context(prev);
}
} else {
cancelSlowPath();
}
}
use of io.servicetalk.context.api.ContextMap in project servicetalk by apple.
the class ContextPreservingSubscription method cancelSlowPath.
private void cancelSlowPath() {
ContextMap prev = CONTEXT_THREAD_LOCAL.get();
try {
CONTEXT_THREAD_LOCAL.set(saved);
subscription.cancel();
} finally {
CONTEXT_THREAD_LOCAL.set(prev);
}
}
use of io.servicetalk.context.api.ContextMap in project servicetalk by apple.
the class ConnectionFactoryOffloadingTest method testFactoryOffloading.
@ParameterizedTest(name = "offload={0} httpStrategy={1}")
@MethodSource("testCases")
void testFactoryOffloading(boolean offload, HttpExecutionStrategy httpStrategy) throws Exception {
AtomicReference<Thread> factoryThread = new AtomicReference<>();
Thread appThread = Thread.currentThread();
try (ServerContext server = HttpServers.forPort(0).listenAndAwait(this::helloWorld)) {
SocketAddress serverAddress = server.listenAddress();
ConnectionFactoryFilter<SocketAddress, FilterableStreamingHttpConnection> factory = ConnectionFactoryFilter.withStrategy(original -> new ConnectionFactory<SocketAddress, FilterableStreamingHttpConnection>() {
private final ListenableAsyncCloseable close = emptyAsyncCloseable();
@Override
public Single<FilterableStreamingHttpConnection> newConnection(final SocketAddress socketAddress, @Nullable final ContextMap context, @Nullable final TransportObserver observer) {
factoryThread.set(Thread.currentThread());
return original.newConnection(socketAddress, context, observer);
}
@Override
public Completable onClose() {
return close.onClose();
}
@Override
public Completable closeAsync() {
return close.closeAsync();
}
@Override
public Completable closeAsyncGracefully() {
return close.closeAsyncGracefully();
}
}, new ConnectAndHttpExecutionStrategy(offload ? ConnectExecutionStrategy.offloadAll() : ConnectExecutionStrategy.offloadNone(), httpStrategy));
try (HttpClient client = HttpClients.forResolvedAddress(serverAddress).appendConnectionFactoryFilter(factory).build()) {
assertThat(client.executionContext().executionStrategy().missing(httpStrategy), is(HttpExecutionStrategies.offloadNone()));
Single<HttpResponse> single = client.request(client.get("/sayHello"));
HttpResponse response = single.toFuture().get();
assertThat("unexpected status", response.status(), is(HttpResponseStatus.OK));
}
}
assertTrue((offload && !IoThreadFactory.IoThread.isIoThread(factoryThread.get())) || (!offload && factoryThread.get() == appThread), "incorrect offloading, offload=" + offload + " thread=" + factoryThread.get());
}
Aggregations