Search in sources :

Example 1 with Executor

use of io.servicetalk.concurrent.Executor in project servicetalk by apple.

the class AbstractTimeoutHttpFilter method withTimeout.

/**
 * Returns the response single for the provided request which will be completed within the specified timeout or
 * generate an error with a timeout exception. The timer begins when the {@link Single} is subscribed. The timeout
 * action, if it occurs, will execute on the filter's chosen timeout executor, or if none is specified, the executor
 * from the request or connection context or, if neither are specified, the global executor.
 *
 * @param request The request requiring a response.
 * @param responseFunction Function which generates the response.
 * @param contextExecutor Executor from the request/connection context to be used for the timeout terminal signal if
 * no specific timeout executor is defined for filter.
 * @return response single
 */
final Single<StreamingHttpResponse> withTimeout(final StreamingHttpRequest request, final Function<StreamingHttpRequest, Single<StreamingHttpResponse>> responseFunction, final Executor contextExecutor) {
    final Executor useForTimeout = null != this.timeoutExecutor ? this.timeoutExecutor : contextExecutor;
    return Single.defer(() -> {
        final Duration timeout = timeoutForRequest.apply(request, useForTimeout);
        Single<StreamingHttpResponse> response = responseFunction.apply(request);
        if (null != timeout) {
            final Single<StreamingHttpResponse> timeoutResponse = response.timeout(timeout, useForTimeout);
            if (fullRequestResponse) {
                final long deadline = useForTimeout.currentTime(NANOSECONDS) + timeout.toNanos();
                response = timeoutResponse.map(resp -> resp.transformMessageBody(body -> defer(() -> {
                    final Duration remaining = ofNanos(deadline - useForTimeout.currentTime(NANOSECONDS));
                    return (body.timeoutTerminal(remaining, useForTimeout)).onErrorMap(TimeoutException.class, t -> new MappedTimeoutException("message body timeout after " + timeout.toMillis() + "ms", t)).shareContextOnSubscribe();
                })));
            } else {
                response = timeoutResponse;
            }
        }
        return response.shareContextOnSubscribe();
    });
}
Also used : StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Single(io.servicetalk.concurrent.api.Single) BiFunction(java.util.function.BiFunction) HttpExecutionStrategyInfluencer(io.servicetalk.http.api.HttpExecutionStrategyInfluencer) TimeoutException(java.util.concurrent.TimeoutException) Function(java.util.function.Function) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Duration.ofNanos(java.time.Duration.ofNanos) Executor(io.servicetalk.concurrent.Executor) HttpExecutionStrategies(io.servicetalk.http.api.HttpExecutionStrategies) Duration(java.time.Duration) DurationUtils.ensurePositive(io.servicetalk.utils.internal.DurationUtils.ensurePositive) Objects.requireNonNull(java.util.Objects.requireNonNull) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) TimeSource(io.servicetalk.concurrent.TimeSource) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Publisher.defer(io.servicetalk.concurrent.api.Publisher.defer) Nullable(javax.annotation.Nullable) Executor(io.servicetalk.concurrent.Executor) Duration(java.time.Duration) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 2 with Executor

use of io.servicetalk.concurrent.Executor in project servicetalk by apple.

the class RetryingHttpRequesterFilterAutoRetryStrategiesTest method newFilter.

private ContextAwareRetryingHttpClientFilter newFilter(final RetryingHttpRequesterFilter filter) {
    final FilterableStreamingHttpClient client = mock(FilterableStreamingHttpClient.class);
    final HttpExecutionContext executionContext = mock(HttpExecutionContext.class);
    when(executionContext.executor()).then((Answer<Executor>) invocation -> immediate());
    when(client.executionContext()).then(__ -> executionContext);
    final ContextAwareRetryingHttpClientFilter f = (ContextAwareRetryingHttpClientFilter) filter.create(client);
    f.inject(lbEvents, sdStatus);
    return f;
}
Also used : FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) Answer(org.mockito.stubbing.Answer) BackOffPolicy.ofNoRetries(io.servicetalk.http.netty.RetryingHttpRequesterFilter.BackOffPolicy.ofNoRetries) Executor(io.servicetalk.concurrent.Executor) IsNull.nullValue(org.hamcrest.core.IsNull.nullValue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) Nonnull(javax.annotation.Nonnull) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) Executors.immediate(io.servicetalk.concurrent.api.Executors.immediate) RetryableConnectException(io.servicetalk.client.api.RetryableConnectException) Completable(io.servicetalk.concurrent.api.Completable) ContextAwareRetryingHttpClientFilter(io.servicetalk.http.netty.RetryingHttpRequesterFilter.ContextAwareRetryingHttpClientFilter) LOAD_BALANCER_READY_EVENT(io.servicetalk.client.api.LoadBalancerReadyEvent.LOAD_BALANCER_READY_EVENT) NoAvailableHostException(io.servicetalk.client.api.NoAvailableHostException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Mockito.when(org.mockito.Mockito.when) UnknownHostException(java.net.UnknownHostException) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) GET(io.servicetalk.http.api.HttpRequestMethod.GET) StreamingHttpRequests.newRequest(io.servicetalk.http.api.StreamingHttpRequests.newRequest) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) RetryingHttpRequesterFilter.disableAutoRetries(io.servicetalk.http.netty.RetryingHttpRequesterFilter.disableAutoRetries) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) Matchers.is(org.hamcrest.Matchers.is) Matchers.anyOf(org.hamcrest.Matchers.anyOf) INSTANCE(io.servicetalk.http.api.DefaultHttpHeadersFactory.INSTANCE) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) Mockito.mock(org.mockito.Mockito.mock) Executor(io.servicetalk.concurrent.Executor) ContextAwareRetryingHttpClientFilter(io.servicetalk.http.netty.RetryingHttpRequesterFilter.ContextAwareRetryingHttpClientFilter) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext)

Aggregations

Executor (io.servicetalk.concurrent.Executor)2 HttpRequestMetaData (io.servicetalk.http.api.HttpRequestMetaData)2 DEFAULT_ALLOCATOR (io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR)1 LOAD_BALANCER_READY_EVENT (io.servicetalk.client.api.LoadBalancerReadyEvent.LOAD_BALANCER_READY_EVENT)1 NoAvailableHostException (io.servicetalk.client.api.NoAvailableHostException)1 RetryableConnectException (io.servicetalk.client.api.RetryableConnectException)1 TimeSource (io.servicetalk.concurrent.TimeSource)1 Completable (io.servicetalk.concurrent.api.Completable)1 Executors.immediate (io.servicetalk.concurrent.api.Executors.immediate)1 Publisher.defer (io.servicetalk.concurrent.api.Publisher.defer)1 Single (io.servicetalk.concurrent.api.Single)1 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)1 TestCompletable (io.servicetalk.concurrent.api.TestCompletable)1 TestPublisher (io.servicetalk.concurrent.api.TestPublisher)1 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)1 TestCompletableSubscriber (io.servicetalk.concurrent.test.internal.TestCompletableSubscriber)1 INSTANCE (io.servicetalk.http.api.DefaultHttpHeadersFactory.INSTANCE)1 FilterableStreamingHttpClient (io.servicetalk.http.api.FilterableStreamingHttpClient)1 HttpExecutionContext (io.servicetalk.http.api.HttpExecutionContext)1 HttpExecutionStrategies (io.servicetalk.http.api.HttpExecutionStrategies)1