Skip to content

Commit 2173bff

Browse files
Align HttpRequester/StreamingHttpRequester with blocking variants
Motivation: `BlockingHttpRequester` and `BlockingStreamingHttpRequester` implement `GracefulAutoCloseable` interface and in result can be used in try-with-resources. For some reason, our `HttpRequester` and `StreamingHttpRequester` don't but their client and connection interfaces do. Modifications: - Promote `GracefulAutoCloseable` interface from `HttpClient` and `HttpConnection` to `HttpRequester`. - Promote `GracefulAutoCloseable` interface from `StreamingHttpClient` and `StreamingHttpConnection` to `StreamingHttpRequester`. Result: All requester/client/connection interfaces are consistent across all 4 API variants.
1 parent 7a7341a commit 2173bff

File tree

7 files changed

+38
-64
lines changed

7 files changed

+38
-64
lines changed

servicetalk-http-api/src/main/java/io/servicetalk/http/api/HttpClient.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515
*/
1616
package io.servicetalk.http.api;
1717

18-
import io.servicetalk.concurrent.GracefulAutoCloseable;
1918
import io.servicetalk.concurrent.api.Single;
2019

21-
import static io.servicetalk.concurrent.internal.FutureUtils.awaitTermination;
22-
2320
/**
2421
* Provides a means to issue requests against HTTP service. The implementation is free to maintain a collection of
2522
* {@link HttpConnection} instances and distribute calls to {@link #request(HttpRequest)} amongst this collection.
2623
*/
27-
public interface HttpClient extends HttpRequester, GracefulAutoCloseable {
24+
public interface HttpClient extends HttpRequester {
2825
/**
2926
* Reserve an {@link HttpConnection} based on provided {@link HttpRequestMetaData}.
3027
* <p>
@@ -63,14 +60,4 @@ default BlockingStreamingHttpClient asBlockingStreamingClient() {
6360
default BlockingHttpClient asBlockingClient() {
6461
return asStreamingClient().asBlockingClient();
6562
}
66-
67-
@Override
68-
default void close() throws Exception {
69-
awaitTermination(closeAsync().toFuture());
70-
}
71-
72-
@Override
73-
default void closeGracefully() throws Exception {
74-
awaitTermination(closeAsyncGracefully().toFuture());
75-
}
7663
}

servicetalk-http-api/src/main/java/io/servicetalk/http/api/HttpConnection.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515
*/
1616
package io.servicetalk.http.api;
1717

18-
import io.servicetalk.concurrent.GracefulAutoCloseable;
1918
import io.servicetalk.concurrent.PublisherSource;
2019
import io.servicetalk.concurrent.api.Publisher;
2120

22-
import static io.servicetalk.concurrent.internal.FutureUtils.awaitTermination;
23-
2421
/**
2522
* Represents a single fixed connection to a HTTP server.
2623
*/
27-
public interface HttpConnection extends HttpRequester, GracefulAutoCloseable {
24+
public interface HttpConnection extends HttpRequester {
2825
/**
2926
* Get the {@link HttpConnectionContext}.
3027
*
@@ -69,14 +66,4 @@ default BlockingStreamingHttpConnection asBlockingStreamingConnection() {
6966
default BlockingHttpConnection asBlockingConnection() {
7067
return asStreamingConnection().asBlockingConnection();
7168
}
72-
73-
@Override
74-
default void close() throws Exception {
75-
awaitTermination(closeAsync().toFuture());
76-
}
77-
78-
@Override
79-
default void closeGracefully() throws Exception {
80-
awaitTermination(closeAsyncGracefully().toFuture());
81-
}
8269
}

servicetalk-http-api/src/main/java/io/servicetalk/http/api/HttpRequester.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
*/
1616
package io.servicetalk.http.api;
1717

18+
import io.servicetalk.concurrent.GracefulAutoCloseable;
1819
import io.servicetalk.concurrent.api.ListenableAsyncCloseable;
1920
import io.servicetalk.concurrent.api.Single;
2021

22+
import static io.servicetalk.concurrent.internal.FutureUtils.awaitTermination;
23+
2124
/**
2225
* Provides a means to make a HTTP request.
2326
*/
24-
public interface HttpRequester extends HttpRequestFactory, ListenableAsyncCloseable {
27+
public interface HttpRequester extends HttpRequestFactory, ListenableAsyncCloseable, GracefulAutoCloseable {
2528
/**
2629
* Send a {@code request}.
2730
*
@@ -46,4 +49,14 @@ public interface HttpRequester extends HttpRequestFactory, ListenableAsyncClosea
4649
* @return a {@link HttpResponseFactory}.
4750
*/
4851
HttpResponseFactory httpResponseFactory();
52+
53+
@Override
54+
default void close() throws Exception {
55+
awaitTermination(closeAsync().toFuture());
56+
}
57+
58+
@Override
59+
default void closeGracefully() throws Exception {
60+
awaitTermination(closeAsyncGracefully().toFuture());
61+
}
4962
}

servicetalk-http-api/src/main/java/io/servicetalk/http/api/StreamingHttpClient.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515
*/
1616
package io.servicetalk.http.api;
1717

18-
import io.servicetalk.concurrent.GracefulAutoCloseable;
1918
import io.servicetalk.concurrent.api.Single;
2019

21-
import static io.servicetalk.concurrent.internal.FutureUtils.awaitTermination;
22-
2320
/**
2421
* The equivalent of {@link HttpClient} but that accepts {@link StreamingHttpRequest} and returns
2522
* {@link StreamingHttpResponse}.
2623
*/
27-
public interface StreamingHttpClient extends FilterableStreamingHttpClient, GracefulAutoCloseable {
24+
public interface StreamingHttpClient extends FilterableStreamingHttpClient {
2825
/**
2926
* Reserve a {@link StreamingHttpConnection} based on provided {@link HttpRequestMetaData}.
3027
* <p>
@@ -66,14 +63,4 @@ public interface StreamingHttpClient extends FilterableStreamingHttpClient, Grac
6663
* @return a {@link BlockingHttpClient} representation of this {@link StreamingHttpClient}.
6764
*/
6865
BlockingHttpClient asBlockingClient();
69-
70-
@Override
71-
default void close() throws Exception {
72-
awaitTermination(closeAsync().toFuture());
73-
}
74-
75-
@Override
76-
default void closeGracefully() throws Exception {
77-
awaitTermination(closeAsyncGracefully().toFuture());
78-
}
7966
}

servicetalk-http-api/src/main/java/io/servicetalk/http/api/StreamingHttpConnection.java

+1-15
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@
1515
*/
1616
package io.servicetalk.http.api;
1717

18-
import io.servicetalk.concurrent.GracefulAutoCloseable;
19-
20-
import static io.servicetalk.concurrent.internal.FutureUtils.awaitTermination;
21-
2218
/**
2319
* The equivalent of {@link HttpConnection} but that accepts {@link StreamingHttpRequest} and returns
2420
* {@link StreamingHttpResponse}.
2521
*/
26-
public interface StreamingHttpConnection extends FilterableStreamingHttpConnection, GracefulAutoCloseable {
22+
public interface StreamingHttpConnection extends FilterableStreamingHttpConnection {
2723
/**
2824
* Convert this {@link StreamingHttpConnection} to the {@link HttpConnection} API.
2925
* <p>
@@ -50,14 +46,4 @@ public interface StreamingHttpConnection extends FilterableStreamingHttpConnecti
5046
* @return a {@link BlockingHttpConnection} representation of this {@link StreamingHttpConnection}.
5147
*/
5248
BlockingHttpConnection asBlockingConnection();
53-
54-
@Override
55-
default void close() throws Exception {
56-
awaitTermination(closeAsync().toFuture());
57-
}
58-
59-
@Override
60-
default void closeGracefully() throws Exception {
61-
awaitTermination(closeAsyncGracefully().toFuture());
62-
}
6349
}

servicetalk-http-api/src/main/java/io/servicetalk/http/api/StreamingHttpRequester.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
*/
1616
package io.servicetalk.http.api;
1717

18+
import io.servicetalk.concurrent.GracefulAutoCloseable;
1819
import io.servicetalk.concurrent.api.ListenableAsyncCloseable;
1920
import io.servicetalk.concurrent.api.Single;
2021

22+
import static io.servicetalk.concurrent.internal.FutureUtils.awaitTermination;
23+
2124
/**
2225
* The equivalent of {@link HttpRequester} but that accepts {@link StreamingHttpRequest} and returns
2326
* {@link StreamingHttpResponse}.
2427
*/
25-
public interface StreamingHttpRequester extends StreamingHttpRequestFactory, ListenableAsyncCloseable {
28+
public interface StreamingHttpRequester extends StreamingHttpRequestFactory, ListenableAsyncCloseable,
29+
GracefulAutoCloseable {
2630
/**
2731
* Send a {@code request}.
2832
*
@@ -47,4 +51,14 @@ public interface StreamingHttpRequester extends StreamingHttpRequestFactory, Lis
4751
* @return a {@link StreamingHttpResponseFactory}.
4852
*/
4953
StreamingHttpResponseFactory httpResponseFactory();
54+
55+
@Override
56+
default void close() throws Exception {
57+
awaitTermination(closeAsync().toFuture());
58+
}
59+
60+
@Override
61+
default void closeGracefully() throws Exception {
62+
awaitTermination(closeAsyncGracefully().toFuture());
63+
}
5064
}

servicetalk-http-netty/src/main/java/io/servicetalk/http/netty/H2ClientParentConnectionContext.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import io.servicetalk.transport.api.ConnectionObserver.StreamObserver;
4646
import io.servicetalk.transport.api.IoThreadFactory;
4747
import io.servicetalk.transport.api.SslConfig;
48+
import io.servicetalk.transport.netty.internal.ChannelCloseUtils;
4849
import io.servicetalk.transport.netty.internal.ChannelInitializer;
4950
import io.servicetalk.transport.netty.internal.CloseHandler;
5051
import io.servicetalk.transport.netty.internal.DefaultNettyConnection;
@@ -86,7 +87,6 @@
8687
import static io.servicetalk.http.netty.AbstractStreamingHttpConnection.ZERO_MAX_CONCURRENCY_EVENT;
8788
import static io.servicetalk.http.netty.HeaderUtils.OBJ_EXPECT_CONTINUE;
8889
import static io.servicetalk.http.netty.HttpDebugUtils.showPipeline;
89-
import static io.servicetalk.transport.netty.internal.ChannelCloseUtils.close;
9090
import static io.servicetalk.transport.netty.internal.ChannelSet.CHANNEL_CLOSEABLE_KEY;
9191
import static io.servicetalk.transport.netty.internal.CloseHandler.forNonPipelined;
9292
import static io.servicetalk.transport.netty.internal.NettyPipelineSslUtils.extractSslSession;
@@ -145,14 +145,14 @@ protected void handleSubscribe(final Subscriber<? super H2ClientParentConnection
145145
delayedCancellable, shouldWaitForSslHandshake(sslSession, sslConfig),
146146
allowDropTrailersReadFromTransport, config.headersFactory(), reqRespFactory, observer);
147147
} catch (Throwable cause) {
148-
close(channel, cause);
148+
ChannelCloseUtils.close(channel, cause);
149149
deliverErrorFromSource(subscriber, cause);
150150
return;
151151
}
152152
try {
153153
subscriber.onSubscribe(delayedCancellable);
154154
} catch (Throwable cause) {
155-
close(channel, cause);
155+
ChannelCloseUtils.close(channel, cause);
156156
handleExceptionFromOnSubscribe(subscriber, cause);
157157
return;
158158
}
@@ -215,7 +215,7 @@ void tryCompleteSubscriber() {
215215
@Override
216216
boolean tryFailSubscriber(Throwable cause) {
217217
if (subscriber != null) {
218-
close(parentContext.nettyChannel(), cause);
218+
ChannelCloseUtils.close(parentContext.nettyChannel(), cause);
219219
Subscriber<? super H2ClientParentConnection> subscriberCopy = subscriber;
220220
subscriber = null;
221221
subscriberCopy.onError(cause);
@@ -391,7 +391,7 @@ private void childChannelActive(Future<Http2StreamChannel> future,
391391
} catch (Throwable cause) {
392392
if (streamChannel != null) {
393393
try {
394-
close(streamChannel, cause);
394+
ChannelCloseUtils.close(streamChannel, cause);
395395
} catch (Throwable unexpected) {
396396
addSuppressed(unexpected, cause);
397397
LOGGER.warn("Unexpected exception while handling the original cause", unexpected);

0 commit comments

Comments
 (0)