37
37
import reactor .ipc .netty .resources .LoopResources ;
38
38
import reactor .ipc .netty .resources .PoolResources ;
39
39
40
+ import javax .annotation .PreDestroy ;
40
41
import java .time .Duration ;
41
42
import java .util .List ;
42
43
import java .util .Map ;
@@ -67,6 +68,15 @@ abstract class _DefaultConnectionContext implements ConnectionContext {
67
68
68
69
private static final int UNDEFINED_PORT = -1 ;
69
70
71
+ /**
72
+ * Disposes resources created to service this connection context
73
+ */
74
+ @ PreDestroy
75
+ public final void dispose () {
76
+ getConnectionPool ().ifPresent (PoolResources ::dispose );
77
+ getThreadPool ().dispose ();
78
+ }
79
+
70
80
/**
71
81
* The number of connections to use when processing requests and responses. Setting this to `null` disables connection pooling.
72
82
*/
@@ -81,12 +91,12 @@ public Integer getConnectionPoolSize() {
81
91
public HttpClient getHttpClient () {
82
92
return HttpClient .create (options -> {
83
93
options
84
- .loopResources (LoopResources . create ( "cloudfoundry-client" , getThreadPoolSize (), true ))
94
+ .loopResources (getThreadPool ( ))
85
95
.option (SO_SNDBUF , SEND_BUFFER_SIZE )
86
96
.option (SO_RCVBUF , RECEIVE_BUFFER_SIZE )
87
97
.disablePool ();
88
98
89
- Optional . ofNullable ( getConnectionPoolSize ()) .ifPresent (connectionPoolSize -> options . poolResources ( PoolResources . fixed ( "cloudfoundry-client" , connectionPoolSize )) );
99
+ getConnectionPool () .ifPresent (options :: poolResources );
90
100
getKeepAlive ().ifPresent (keepAlive -> options .option (SO_KEEPALIVE , keepAlive ));
91
101
getProxyConfiguration ().ifPresent (c -> options .proxy (ClientOptions .Proxy .HTTP , c .getHost (), c .getPort ().orElse (null ), c .getUsername ().orElse (null ), u -> c .getPassword ().orElse (null )));
92
102
getConnectTimeout ().ifPresent (socketTimeout -> options .option (CONNECT_TIMEOUT_MILLIS , (int ) socketTimeout .toMillis ()));
@@ -115,15 +125,6 @@ public Integer getPort() {
115
125
return DEFAULT_PORT ;
116
126
}
117
127
118
- @ Override
119
- public Mono <String > getRoot (String key ) {
120
- return getInfo ()
121
- .map (info -> normalize (UriComponentsBuilder .fromUriString (info .get (key )), getScheme ()))
122
- .doOnNext (components -> trust (components , getSslCertificateTruster ()))
123
- .map (UriComponents ::toUriString )
124
- .cache ();
125
- }
126
-
127
128
@ Value .Derived
128
129
public Mono <String > getRoot () {
129
130
Integer port = getPort ();
@@ -138,6 +139,15 @@ public Mono<String> getRoot() {
138
139
return Mono .just (components .toUriString ());
139
140
}
140
141
142
+ @ Override
143
+ public Mono <String > getRoot (String key ) {
144
+ return getInfo ()
145
+ .map (info -> normalize (UriComponentsBuilder .fromUriString (info .get (key )), getScheme ()))
146
+ .doOnNext (components -> trust (components , getSslCertificateTruster ()))
147
+ .map (UriComponents ::toUriString )
148
+ .cache ();
149
+ }
150
+
141
151
/**
142
152
* The number of worker threads to use when processing requests and responses
143
153
*/
@@ -165,6 +175,12 @@ void checkForValidApiHost() {
165
175
*/
166
176
abstract Optional <Duration > getConnectTimeout ();
167
177
178
+ @ Value .Derived
179
+ Optional <PoolResources > getConnectionPool () {
180
+ return Optional .ofNullable (getConnectionPoolSize ())
181
+ .map (connectionPoolSize -> PoolResources .fixed ("cloudfoundry-client" , connectionPoolSize ));
182
+ }
183
+
168
184
@ SuppressWarnings ("unchecked" )
169
185
@ Value .Derived
170
186
Mono <Map <String , String >> getInfo () {
@@ -229,6 +245,11 @@ Optional<SslCertificateTruster> getSslCertificateTruster() {
229
245
*/
230
246
abstract Optional <Duration > getSslHandshakeTimeout ();
231
247
248
+ @ Value .Derived
249
+ LoopResources getThreadPool () {
250
+ return LoopResources .create ("cloudfoundry-client" , getThreadPoolSize (), true );
251
+ }
252
+
232
253
private static void trust (UriComponents components , Optional <SslCertificateTruster > sslCertificateTruster ) {
233
254
sslCertificateTruster .ifPresent (t -> t .trust (components .getHost (), components .getPort (), Duration .ofSeconds (30 )));
234
255
}
0 commit comments