Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#52) Configuration of Unix connection pooling #60

Merged
merged 3 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/com/amihaiemil/docker/LocalDocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
* <pre>
* final Docker docker = new LocalDocker("unix:///var/run/dicker.sock");
* </pre>
*
* This implementation manages an internal pool of 10 http connections. Users
* who wish to alter this behaviour may provide their own {@link HttpClient}
* via {@link #LocalDocker(HttpClient, String)}.
*
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
Expand Down Expand Up @@ -64,10 +68,14 @@ public LocalDocker(final File unixSocket, final String version){

/**
* Local Docker engine.
* <p>
* Users may supply their own {@link HttpClient} that must register a unix
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@llorllale ```... that must register a {@link UnixSocketFactory}, would be better and then we can remove the @see ...``

* socket factory.
* @param client The http client to use.
* @param version API version (e.g. v1.30).
* @see UnixSocketFactory
*/
LocalDocker(final HttpClient client, final String version) {
public LocalDocker(final HttpClient client, final String version) {
super(client, URI.create("unix://localhost:80/" + version));
}

Expand Down
33 changes: 1 addition & 32 deletions src/main/java/com/amihaiemil/docker/UnixHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*/
package com.amihaiemil.docker;

import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
Expand All @@ -41,8 +39,6 @@
import org.apache.http.protocol.HttpContext;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.function.Supplier;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

Expand All @@ -54,8 +50,6 @@
* @since 0.0.1
* @checkstyle ParameterNumber (150 lines)
* @checkstyle AnonInnerLength (150 lines)
* @todo #44:30min Connection pooling is currently hardcoded at 10 connections
* max. Figure out how to make this configurable.
*/
final class UnixHttpClient implements HttpClient {

Expand All @@ -74,32 +68,7 @@ final class UnixHttpClient implements HttpClient {
new PoolingHttpClientConnectionManager(
RegistryBuilder
.<ConnectionSocketFactory>create()
.register(
"unix",
new ConnectionSocketFactory() {
@Override
public Socket createSocket(
final HttpContext httpContext
) throws IOException {
return UnixSocketChannel.open().socket();
}

@Override
public Socket connectSocket(
final int connectionTimeout,
final Socket socket,
final HttpHost host,
final InetSocketAddress remoteAddress,
final InetSocketAddress localAddress,
final HttpContext context
) throws IOException {
socket.setSoTimeout(connectionTimeout);
socket.getChannel().connect(
new UnixSocketAddress(socketFile)
);
return socket;
}
})
.register("unix", new UnixSocketFactory(socketFile))
.build()
);
pool.setDefaultMaxPerRoute(10);
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/amihaiemil/docker/UnixSocketFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.amihaiemil.docker;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
import org.apache.http.HttpHost;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.protocol.HttpContext;

/**
* Provides unix sockets connecting to a given unix socket file.
*
* @author George Aristy (george.aristy@gmail.com)
* @version $Id$
* @since 0.0.1
* @checkstyle ParameterNumber (100 lines)
*/
public final class UnixSocketFactory implements ConnectionSocketFactory {
/**
* File pointing to the unix socket.
*/
private final File unixSocket;

/**
* Ctor.
* @param unixSocket File pointing to the unix socket.
*/
public UnixSocketFactory(final File unixSocket) {
this.unixSocket = unixSocket;
}

@Override
public Socket createSocket(final HttpContext context) throws IOException {
return UnixSocketChannel.open().socket();
}

@Override
public Socket connectSocket(final int connectTimeout, final Socket socket,
final HttpHost host, final InetSocketAddress remoteAddress,
final InetSocketAddress localAddress, final HttpContext context)
throws IOException {
socket.setSoTimeout(connectTimeout);
socket.getChannel().connect(
new UnixSocketAddress(this.unixSocket)
);
return socket;
}
}