-
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.
Improve timeout support for HTTP (#1456)
Motivation: Some uses of the HTTP API require progress or completion within specified time limits. Modifications: New or enhanced filters are proivded for client and server to manage timeout events. Existing client filter `TimeoutHttpRequesterFilter` is enhanced to support additional timeout options for the entire HTTP request/response transaction. A new server filter `TimeoutHttpServiceFilter` is provided for managing timeouts by services. Result: HTTP API provides request/response transaction timeout capabilities
- Loading branch information
Showing
14 changed files
with
916 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright © 2019, 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. | ||
*/ | ||
|
||
apply plugin: "java" | ||
apply from: "../../gradle/idea.gradle" | ||
|
||
dependencies { | ||
implementation project(":servicetalk-http-netty") | ||
implementation project(":servicetalk-http-utils") | ||
|
||
runtimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion" | ||
} |
77 changes: 77 additions & 0 deletions
77
...amples/http/timeout/src/main/java/io/servicetalk/examples/http/timeout/TimeoutClient.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,77 @@ | ||
/* | ||
* 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.examples.http.timeout; | ||
|
||
import io.servicetalk.http.api.HttpClient; | ||
import io.servicetalk.http.api.SingleAddressHttpClientBuilder; | ||
import io.servicetalk.http.netty.HttpClients; | ||
import io.servicetalk.http.utils.TimeoutHttpRequesterFilter; | ||
import io.servicetalk.transport.api.HostAndPort; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.time.Duration; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
import static io.servicetalk.http.api.HttpSerializationProviders.textDeserializer; | ||
|
||
/** | ||
* Extends the async 'Hello World!' example to demonstrate use of timeout filters and timeout operators. If a single | ||
* timeout can be applied to all transactions then the timeout should be applied using the | ||
* {@link TimeoutHttpRequesterFilter}. If only some transactions require a timeout then the timeout should be applied | ||
* using a {@link io.servicetalk.concurrent.api.Single#timeout(Duration)} Single.timeout()} or a | ||
* {@link io.servicetalk.concurrent.api.Publisher#timeoutTerminal(Duration)} (Duration)} Publisher.timeoutTerminal()} | ||
* operator. | ||
*/ | ||
public final class TimeoutClient { | ||
|
||
public static void main(String[] args) throws Exception { | ||
SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> builder = | ||
HttpClients.forSingleAddress("localhost", 8080) | ||
// Filter enforces that requests made with this client must fully complete | ||
// within 10 seconds or will be cancelled. | ||
.appendClientFilter(new TimeoutHttpRequesterFilter(Duration.ofSeconds(10), true)); | ||
|
||
try (HttpClient client = builder.build()) { | ||
// This example is demonstrating asynchronous execution, but needs to prevent the main thread from exiting | ||
// before the response has been processed. This isn't typical usage for a streaming API but is useful for | ||
// demonstration purposes. | ||
CountDownLatch responseProcessedLatch = new CountDownLatch(2); | ||
|
||
// first request, with default timeout from HttpClient (this will succeed) | ||
client.request(client.get("/sayHello")) | ||
.afterFinally(responseProcessedLatch::countDown) | ||
.afterOnError(System.err::println) | ||
.subscribe(resp -> { | ||
System.out.println(resp.toString((name, value) -> value)); | ||
System.out.println(resp.payloadBody(textDeserializer())); | ||
}); | ||
|
||
// second request, with custom timeout that is lower than the client default (this will timeout) | ||
client.request(client.get("/sayHello")) | ||
// This request and response must complete within 3 seconds or the request will be cancelled. | ||
.timeout(Duration.ofSeconds(3)) | ||
.afterFinally(responseProcessedLatch::countDown) | ||
.afterOnError(System.err::println) | ||
.subscribe(resp -> { | ||
System.out.println(resp.toString((name, value) -> value)); | ||
System.out.println(resp.payloadBody(textDeserializer())); | ||
}); | ||
|
||
// block until requests are complete and afterFinally() has been called | ||
responseProcessedLatch.await(); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...amples/http/timeout/src/main/java/io/servicetalk/examples/http/timeout/TimeoutServer.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,52 @@ | ||
/* | ||
* 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.examples.http.timeout; | ||
|
||
import io.servicetalk.concurrent.api.Single; | ||
import io.servicetalk.http.netty.HttpServers; | ||
import io.servicetalk.http.utils.TimeoutHttpServiceFilter; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static io.servicetalk.concurrent.api.Single.succeeded; | ||
import static io.servicetalk.http.api.HttpSerializationProviders.textSerializer; | ||
|
||
/** | ||
* Extends the async 'Hello World!' example to demonstrate use of timeout filter. | ||
*/ | ||
public final class TimeoutServer { | ||
public static void main(String[] args) throws Exception { | ||
HttpServers.forPort(8080) | ||
// Filter enforces that responses must complete within 30 seconds or will be cancelled. | ||
.appendServiceFilter(new TimeoutHttpServiceFilter(Duration.ofSeconds(30))) | ||
.listenAndAwait((ctx, request, responseFactory) -> | ||
Single.defer(() -> { | ||
// Force a 5 second delay in the response. | ||
try { | ||
TimeUnit.SECONDS.sleep(5); | ||
} catch (InterruptedException woken) { | ||
Thread.interrupted(); | ||
// just continue | ||
} | ||
|
||
return succeeded(responseFactory.ok() | ||
.payloadBody("Hello World!", textSerializer())) | ||
.subscribeShareContext(); | ||
})) | ||
.awaitShutdown(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
servicetalk-examples/http/timeout/src/main/resources/log4j2.xml
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,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright © 2018 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. | ||
--> | ||
<Configuration status="info"> | ||
<Appenders> | ||
<Console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%d %30t [%-5level] %-30logger{1} - %msg%n"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<!-- Prints server start and shutdown --> | ||
<Logger name="io.servicetalk.http.netty.NettyHttpServer" level="DEBUG"/> | ||
|
||
<!-- Prints default subscriber errors--> | ||
<Logger name="io.servicetalk.concurrent.api" level="DEBUG"/> | ||
|
||
<!-- Use `-Dservicetalk.logger.level=DEBUG` to change the root logger level via command line --> | ||
<Root level="${sys:servicetalk.logger.level:-INFO}"> | ||
<AppenderRef ref="Console"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
Oops, something went wrong.