Skip to content

Commit 6009d3c

Browse files
committed
(#44) ISE: Connection is still allocated
* UnixHttpClient: swapped BasicHttpClientConnectionManager for PoolingHttpClientConnectionManager
1 parent 81773b8 commit 6009d3c

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

puzzles.xml

Whitespace-only changes.

src/main/java/com/amihaiemil/docker/UnixHttpClient.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@
3737
import org.apache.http.conn.ClientConnectionManager;
3838
import org.apache.http.conn.socket.ConnectionSocketFactory;
3939
import org.apache.http.impl.client.HttpClientBuilder;
40-
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
4140
import org.apache.http.params.HttpParams;
4241
import org.apache.http.protocol.HttpContext;
4342
import java.io.File;
4443
import java.io.IOException;
4544
import java.net.InetSocketAddress;
4645
import java.net.Socket;
46+
import java.util.function.Supplier;
47+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
4748

4849
/**
4950
* An HttpClient which works over a UnixSocket.
@@ -53,6 +54,8 @@
5354
* @since 0.0.1
5455
* @checkstyle ParameterNumber (150 lines)
5556
* @checkstyle AnonInnerLength (150 lines)
57+
* @todo #44:30min Connection pooling is currently hardcoded at 10 connections
58+
* max. Figure out how to make this configurable.
5659
*/
5760
final class UnixHttpClient implements HttpClient {
5861

@@ -66,9 +69,9 @@ final class UnixHttpClient implements HttpClient {
6669
* @param socketFile Unix socket on disk.
6770
*/
6871
UnixHttpClient(final File socketFile) {
69-
this(
70-
HttpClientBuilder.create().setConnectionManager(
71-
new BasicHttpClientConnectionManager(
72+
this(() -> {
73+
final PoolingHttpClientConnectionManager pool =
74+
new PoolingHttpClientConnectionManager(
7275
RegistryBuilder
7376
.<ConnectionSocketFactory>create()
7477
.register(
@@ -98,9 +101,21 @@ public Socket connectSocket(
98101
}
99102
})
100103
.build()
101-
)
102-
).build()
103-
);
104+
);
105+
pool.setDefaultMaxPerRoute(10);
106+
pool.setMaxTotal(10);
107+
return HttpClientBuilder.create()
108+
.setConnectionManager(pool)
109+
.build();
110+
});
111+
}
112+
113+
/**
114+
* Ctor.
115+
* @param client The HttpClient.
116+
*/
117+
UnixHttpClient(final Supplier<HttpClient> client) {
118+
this(client.get());
104119
}
105120

106121
/**
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (c) 2018, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker;
27+
28+
import java.io.File;
29+
import java.io.IOException;
30+
import org.apache.http.client.HttpClient;
31+
import org.apache.http.client.methods.HttpGet;
32+
import org.junit.Ignore;
33+
import org.junit.Test;
34+
35+
/**
36+
* Integration tests for {@link UnixHttpClient}.
37+
*
38+
* @author George Aristy (george.aristy@gmail.com)
39+
* @version $Id$
40+
* @since 0.0.1
41+
*/
42+
public final class UnixHttpClientITCase {
43+
/**
44+
* UnixHttpClient can be executed more than once without throwing a
45+
* ConnectionPoolTimeoutException, demonstrating its connection-pooling
46+
* capabilities.
47+
* @throws IOException unexpected
48+
* @see <a href="https://github.com/amihaiemil/docker-java-api/issues/44">bug</a>
49+
* @todo #44:30min This should be un-ignored and refactored after #41 is
50+
* done. The unix socket server needs to be spooled up, and the url
51+
* changed accordingly.
52+
*/
53+
@Ignore
54+
@Test
55+
public void canBeReused() throws IOException {
56+
final HttpClient client = new UnixHttpClient(
57+
new File("/var/run/docker.sock")
58+
);
59+
client.execute(new HttpGet("http://localhost/ping"));
60+
client.execute(new HttpGet("http://localhost/ping"));
61+
}
62+
}

0 commit comments

Comments
 (0)