|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | +package software.amazon.awssdk.crt.http; |
| 6 | + |
| 7 | +import software.amazon.awssdk.crt.CrtRuntimeException; |
| 8 | + |
| 9 | +import java.util.concurrent.CompletableFuture; |
| 10 | + |
| 11 | +/** |
| 12 | + * Manages a Pool of HTTP/1.1 Streams. Creates and manages HTTP/1.1 connections |
| 13 | + * under the hood. Will grab a connection from HttpClientConnectionManager to |
| 14 | + * make request on it, and will return it back until the request finishes. |
| 15 | + */ |
| 16 | +public class Http1StreamManager implements AutoCloseable { |
| 17 | + |
| 18 | + private HttpClientConnectionManager connectionManager = null; |
| 19 | + |
| 20 | + /** |
| 21 | + * Factory function for Http1StreamManager instances |
| 22 | + * |
| 23 | + * @param options the connection manager options configure to connection manager under the hood |
| 24 | + * @return a new instance of an Http1StreamManager |
| 25 | + */ |
| 26 | + public static Http1StreamManager create(HttpClientConnectionManagerOptions options) { |
| 27 | + return new Http1StreamManager(options); |
| 28 | + } |
| 29 | + |
| 30 | + private Http1StreamManager(HttpClientConnectionManagerOptions options) { |
| 31 | + this.connectionManager = HttpClientConnectionManager.create(options); |
| 32 | + } |
| 33 | + |
| 34 | + public CompletableFuture<Void> getShutdownCompleteFuture() { |
| 35 | + return this.connectionManager.getShutdownCompleteFuture(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Request an HTTP/1.1 HttpStream from StreamManager. |
| 40 | + * |
| 41 | + * @param request HttpRequest. The Request to make to the Server. |
| 42 | + * @param streamHandler HttpStreamBaseResponseHandler. The Stream Handler to be called from the Native EventLoop |
| 43 | + * @return A future for a HttpStream that will be completed when the stream is |
| 44 | + * acquired. |
| 45 | + * @throws CrtRuntimeException Exception happens from acquiring stream. |
| 46 | + */ |
| 47 | + public CompletableFuture<HttpStream> acquireStream(HttpRequest request, |
| 48 | + HttpStreamBaseResponseHandler streamHandler) { |
| 49 | + return this.acquireStream((HttpRequestBase) request, streamHandler); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Request an HTTP/1.1 HttpStream from StreamManager. |
| 54 | + * |
| 55 | + * @param request HttpRequestBase. The Request to make to the Server. |
| 56 | + * @param streamHandler HttpStreamBaseResponseHandler. The Stream Handler to be called from the Native EventLoop |
| 57 | + * @return A future for a HttpStream that will be completed when the stream is |
| 58 | + * acquired. |
| 59 | + * @throws CrtRuntimeException Exception happens from acquiring stream. |
| 60 | + */ |
| 61 | + public CompletableFuture<HttpStream> acquireStream(HttpRequestBase request, |
| 62 | + HttpStreamBaseResponseHandler streamHandler) { |
| 63 | + CompletableFuture<HttpStream> completionFuture = new CompletableFuture<>(); |
| 64 | + HttpClientConnectionManager connManager = this.connectionManager; |
| 65 | + |
| 66 | + connManager.acquireConnection().whenComplete((conn, throwable) -> { |
| 67 | + if (throwable != null) { |
| 68 | + completionFuture.completeExceptionally(throwable); |
| 69 | + } else { |
| 70 | + try { |
| 71 | + HttpStreamBase stream = conn.makeRequest(request, new HttpStreamBaseResponseHandler() { |
| 72 | + @Override |
| 73 | + public void onResponseHeaders(HttpStreamBase stream, int responseStatusCode, int blockType, |
| 74 | + HttpHeader[] nextHeaders) { |
| 75 | + streamHandler.onResponseHeaders(stream, responseStatusCode, blockType, nextHeaders); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onResponseHeadersDone(HttpStreamBase stream, int blockType) { |
| 80 | + streamHandler.onResponseHeadersDone(stream, blockType); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public int onResponseBody(HttpStreamBase stream, byte[] bodyBytesIn) { |
| 85 | + return streamHandler.onResponseBody(stream, bodyBytesIn); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onResponseComplete(HttpStreamBase stream, int errorCode) { |
| 90 | + streamHandler.onResponseComplete(stream, errorCode); |
| 91 | + /* Release the connection back */ |
| 92 | + connManager.releaseConnection(conn); |
| 93 | + } |
| 94 | + }); |
| 95 | + completionFuture.complete((HttpStream) stream); |
| 96 | + /* Active the stream for user */ |
| 97 | + try { |
| 98 | + stream.activate(); |
| 99 | + } catch (CrtRuntimeException e) { |
| 100 | + /* If activate failed, complete callback will not be invoked */ |
| 101 | + streamHandler.onResponseComplete(stream, e.errorCode); |
| 102 | + /* Release the connection back */ |
| 103 | + connManager.releaseConnection(conn); |
| 104 | + } |
| 105 | + } catch (Exception ex) { |
| 106 | + connManager.releaseConnection(conn); |
| 107 | + completionFuture.completeExceptionally(ex); |
| 108 | + } |
| 109 | + } |
| 110 | + }); |
| 111 | + return completionFuture; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @return concurrency metrics for the current manager |
| 116 | + */ |
| 117 | + public HttpManagerMetrics getManagerMetrics() { |
| 118 | + return this.connectionManager.getManagerMetrics(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return maximum number of connections this manager will pool |
| 123 | + */ |
| 124 | + public int getMaxConnections() { |
| 125 | + return this.connectionManager.getMaxConnections(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void close() { |
| 130 | + this.connectionManager.close(); |
| 131 | + } |
| 132 | +} |
0 commit comments