-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix duration comparison bug in timeout filters and reduce code duplic…
…ation (#1491) Motivation: `Duration#compareTo` is hard to understand, the result of comparison depends on the order of arguments. This lead to a bug in `TimeoutHttpRequesterFilter` and `TimeoutHttpServiceFilter` which always fails payload body publisher with `TimeoutException`. Use a shared utility to make `isPositive` check for `Duration` consistent across all modules. Also, `TimeoutHttpRequesterFilter` and `TimeoutHttpServiceFilter` logic is identical, we can reduce duplication and share the logic. Modifications: - Introduce a shared `DurationUtils` class for common operations with `Duration`; - Use this utility to reduce mistakes when we check that the passed `Duration` must be positive; - Introduce `AbstractTimeoutHttpFilterTest` to share timeout logic between `TimeoutHttpRequesterFilter` and `TimeoutHttpServiceFilter`; - Add tests to verify behavior correctness for both timeout filters; - Adjust how the timeout filters are created in `ResponseTimeoutTest` to avoid NPE; - Minor improvements to make javadoc consistent between `TimeoutHttpRequesterFilter` and `TimeoutHttpServiceFilter`; Result: 1. `TimeoutHttpRequesterFilter` and `TimeoutHttpServiceFilter` do not fail payload body publisher before the actual timeout expires. 2. No code duplication between timeout filters. 3. Shared utility to consistently validate `Duration` in all modules.
- Loading branch information
1 parent
6735dea
commit 5b0100b
Showing
15 changed files
with
608 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...icetalk-http-utils/src/main/java/io/servicetalk/http/utils/AbstractTimeoutHttpFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Copyright © 2021 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.http.utils; | ||
|
||
import io.servicetalk.concurrent.api.Executor; | ||
import io.servicetalk.concurrent.api.Single; | ||
import io.servicetalk.http.api.HttpExecutionStrategy; | ||
import io.servicetalk.http.api.HttpExecutionStrategyInfluencer; | ||
import io.servicetalk.http.api.HttpRequestMetaData; | ||
import io.servicetalk.http.api.StreamingHttpRequest; | ||
import io.servicetalk.http.api.StreamingHttpResponse; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Function; | ||
import javax.annotation.Nullable; | ||
|
||
import static io.servicetalk.concurrent.api.Publisher.defer; | ||
import static io.servicetalk.concurrent.api.Publisher.failed; | ||
import static io.servicetalk.utils.internal.DurationUtils.ensurePositive; | ||
import static io.servicetalk.utils.internal.DurationUtils.isPositive; | ||
import static java.time.Duration.ofNanos; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
abstract class AbstractTimeoutHttpFilter implements HttpExecutionStrategyInfluencer { | ||
/** | ||
* Establishes the timeout for a given request. | ||
*/ | ||
private final TimeoutFromRequest timeoutForRequest; | ||
|
||
/** | ||
* If {@code true} then timeout is for full request/response transaction otherwise only the response metadata must | ||
* complete before the timeout. | ||
*/ | ||
private final boolean fullRequestResponse; | ||
|
||
@Nullable | ||
private final Executor timeoutExecutor; | ||
|
||
AbstractTimeoutHttpFilter(final TimeoutFromRequest timeoutForRequest, final boolean fullRequestResponse) { | ||
this.timeoutForRequest = requireNonNull(timeoutForRequest, "timeoutForRequest"); | ||
this.fullRequestResponse = fullRequestResponse; | ||
this.timeoutExecutor = null; | ||
} | ||
|
||
AbstractTimeoutHttpFilter(final TimeoutFromRequest timeoutForRequest, final boolean fullRequestResponse, | ||
final Executor timeoutExecutor) { | ||
this.timeoutForRequest = requireNonNull(timeoutForRequest, "timeoutForRequest"); | ||
this.fullRequestResponse = fullRequestResponse; | ||
this.timeoutExecutor = requireNonNull(timeoutExecutor, "timeoutExecutor"); | ||
} | ||
|
||
@Override | ||
public final HttpExecutionStrategy influenceStrategy(final HttpExecutionStrategy strategy) { | ||
return timeoutForRequest.influenceStrategy(strategy); | ||
} | ||
|
||
final Single<StreamingHttpResponse> withTimeout(final StreamingHttpRequest request, | ||
final Function<StreamingHttpRequest, Single<StreamingHttpResponse>> responseFunction) { | ||
|
||
return Single.defer(() -> { | ||
final Duration timeout = timeoutForRequest.apply(request); | ||
if (null != timeout && !isPositive(timeout)) { | ||
return Single.failed(new TimeoutException("non-positive timeout of " + timeout.toMillis() + "ms")); | ||
} | ||
|
||
Single<StreamingHttpResponse> response = responseFunction.apply(request); | ||
if (null != timeout) { | ||
final Single<StreamingHttpResponse> timeoutResponse = timeoutExecutor == null ? | ||
response.timeout(timeout) : response.timeout(timeout, timeoutExecutor); | ||
|
||
if (fullRequestResponse) { | ||
final long deadline = System.nanoTime() + timeout.toNanos(); | ||
response = timeoutResponse.map(resp -> resp.transformMessageBody(body -> defer(() -> { | ||
final Duration remaining = ofNanos(deadline - System.nanoTime()); | ||
if (isPositive(remaining)) { | ||
return (timeoutExecutor == null ? | ||
body.timeoutTerminal(remaining) : body.timeoutTerminal(remaining, timeoutExecutor)) | ||
.subscribeShareContext(); | ||
} | ||
return failed(new TimeoutException("timeout after " + timeout.toMillis() + "ms")); | ||
}))); | ||
} else { | ||
response = timeoutResponse; | ||
} | ||
} | ||
|
||
return response.subscribeShareContext(); | ||
}); | ||
} | ||
|
||
/** | ||
* {@link TimeoutFromRequest} implementation which returns the provided default duration as the timeout duration to | ||
* be used for any request. | ||
*/ | ||
static final class FixedDuration implements TimeoutFromRequest { | ||
|
||
private final Duration duration; | ||
|
||
FixedDuration(final Duration duration) { | ||
this.duration = ensurePositive(duration, "duration"); | ||
} | ||
|
||
@Override | ||
public Duration apply(final HttpRequestMetaData request) { | ||
return duration; | ||
} | ||
|
||
@Override | ||
public HttpExecutionStrategy influenceStrategy(final HttpExecutionStrategy strategy) { | ||
// No influence since we do not block. | ||
return strategy; | ||
} | ||
} | ||
} |
Oops, something went wrong.