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();
});
}
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;
}
Aggregations