-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathHttpStreamManager.java
More file actions
118 lines (105 loc) · 4.59 KB
/
HttpStreamManager.java
File metadata and controls
118 lines (105 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
package software.amazon.awssdk.crt.http;
import java.util.concurrent.CompletableFuture;
/**
* Manages a Pool of HTTP Streams. Wraps either Http1StreamManager or Http2StreamManager
* depending on the expected protocol configured via HttpStreamManagerOptions.
*/
public class HttpStreamManager implements AutoCloseable {
private Http1StreamManager h1StreamManager = null;
private Http2StreamManager h2StreamManager = null;
private CompletableFuture<Void> shutdownComplete = null;
/**
* Factory function for HttpStreamManager instances
*
* @param options the stream manager options to configure the manager
* @return a new instance of an HttpStreamManager
*/
public static HttpStreamManager create(HttpStreamManagerOptions options) {
return new HttpStreamManager(options);
}
private HttpStreamManager(HttpStreamManagerOptions options) {
if (options.getExpectedProtocol() == HttpVersion.UNKNOWN) {
throw new IllegalArgumentException("UNKNOWN protocol is not supported. Please specify either HTTP_2 or HTTP_1_1/HTTP_1_0.");
}
if (options.getExpectedProtocol() == HttpVersion.HTTP_2) {
this.h2StreamManager = Http2StreamManager.create(options.getHTTP2StreamManagerOptions());
this.shutdownComplete = this.h2StreamManager.getShutdownCompleteFuture();
} else {
this.h1StreamManager = Http1StreamManager.create(options.getHTTP1ConnectionManagerOptions());
this.shutdownComplete = this.h1StreamManager.getShutdownCompleteFuture();
}
}
/**
* Request an HttpStream from StreamManager.
*
* @param request HttpRequestBase. The Request to make to the Server.
* @param streamHandler HttpStreamBaseResponseHandler. The Stream Handler to be called from the Native EventLoop
* @return A future for a HttpStreamBase that will be completed when the stream is
* acquired.
*/
public CompletableFuture<HttpStreamBase> acquireStream(HttpRequestBase request,
HttpStreamBaseResponseHandler streamHandler) {
if (this.h2StreamManager != null) {
return this.h2StreamManager.acquireStream(request, streamHandler)
.thenApply(stream -> (HttpStreamBase) stream);
} else {
return this.h1StreamManager.acquireStream(request, streamHandler, false)
.thenApply(stream -> (HttpStreamBase) stream);
}
}
/**
* Request an HttpStream from StreamManager.
*
* @param request HttpRequestBase. The Request to make to the Server.
* @param streamHandler HttpStreamBaseResponseHandler. The Stream Handler to be called from the Native EventLoop
* @param useManualDataWrites A boolean variable to signal that body will be streamed using async writes.
* @return A future for a HttpStreamBase that will be completed when the stream is
* acquired.
*/
public CompletableFuture<HttpStreamBase> acquireStream(HttpRequestBase request,
HttpStreamBaseResponseHandler streamHandler, boolean useManualDataWrites) {
if (this.h2StreamManager != null) {
return this.h2StreamManager.acquireStream(request, streamHandler, useManualDataWrites)
.thenApply(stream -> (HttpStreamBase) stream);
} else {
return this.h1StreamManager.acquireStream(request, streamHandler, useManualDataWrites)
.thenApply(stream -> (HttpStreamBase) stream);
}
}
public CompletableFuture<Void> getShutdownCompleteFuture() {
return shutdownComplete;
}
/**
* @return concurrency metrics for the current manager
*/
public HttpManagerMetrics getManagerMetrics() {
if (this.h2StreamManager != null) {
return this.h2StreamManager.getManagerMetrics();
} else {
return this.h1StreamManager.getManagerMetrics();
}
}
/**
* @return maximum number of connections this connection manager will pool
*/
public int getMaxConnections() {
if (this.h2StreamManager != null) {
return this.h2StreamManager.getMaxConnections();
} else {
return this.h1StreamManager.getMaxConnections();
}
}
@Override
public void close() {
if (this.h1StreamManager != null) {
this.h1StreamManager.close();
}
if (this.h2StreamManager != null) {
this.h2StreamManager.close();
}
}
}