Search in sources :

Example 26 with ContextMap

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);
    }
}
Also used : ContextMap(io.servicetalk.context.api.ContextMap)

Example 27 with ContextMap

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);
    }
}
Also used : ContextMapHolder(io.servicetalk.context.api.ContextMapHolder) ContextMap(io.servicetalk.context.api.ContextMap)

Example 28 with ContextMap

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();
    }
}
Also used : ContextMapHolder(io.servicetalk.context.api.ContextMapHolder) ContextMap(io.servicetalk.context.api.ContextMap)

Example 29 with ContextMap

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);
    }
}
Also used : ContextMap(io.servicetalk.context.api.ContextMap)

Example 30 with ContextMap

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());
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) TransportObserver(io.servicetalk.transport.api.TransportObserver) HttpResponse(io.servicetalk.http.api.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) ContextMap(io.servicetalk.context.api.ContextMap) ServerContext(io.servicetalk.transport.api.ServerContext) Single(io.servicetalk.concurrent.api.Single) ConnectAndHttpExecutionStrategy(io.servicetalk.http.api.ConnectAndHttpExecutionStrategy) HttpClient(io.servicetalk.http.api.HttpClient) ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) SocketAddress(java.net.SocketAddress) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ContextMap (io.servicetalk.context.api.ContextMap)49 ContextMapHolder (io.servicetalk.context.api.ContextMapHolder)20 Single (io.servicetalk.concurrent.api.Single)5 AsyncContext (io.servicetalk.concurrent.api.AsyncContext)4 Completable (io.servicetalk.concurrent.api.Completable)4 Executor (io.servicetalk.concurrent.api.Executor)4 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 ListenableAsyncCloseable (io.servicetalk.concurrent.api.ListenableAsyncCloseable)3 TerminalSignalConsumer (io.servicetalk.concurrent.api.TerminalSignalConsumer)3 AbstractOffloadingTest (io.servicetalk.concurrent.api.internal.AbstractOffloadingTest)3 Cancellable (io.servicetalk.concurrent.Cancellable)2 Publisher (io.servicetalk.concurrent.api.Publisher)2 FilterableStreamingHttpConnection (io.servicetalk.http.api.FilterableStreamingHttpConnection)2 TransportObserver (io.servicetalk.transport.api.TransportObserver)2 ArrayList (java.util.ArrayList)2 EnumSet (java.util.EnumSet)2 BiFunction (java.util.function.BiFunction)2 Nullable (javax.annotation.Nullable)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2