Skip to content

Commit

Permalink
fabric8io#51 : Add an alternative UnixSocket implementation
Browse files Browse the repository at this point in the history
Adding jnr-unixsocket as alternative unix socket implementation which can be selected with 'withUseJnrUnixSocket()' config option. With this alternative there seems to be no performance issues anymore. Jnr-unixsockets are enabled by default now but can be switched off with this config option.
  • Loading branch information
rhuss committed May 24, 2016
1 parent 5ae8ac6 commit 3fb8a16
Show file tree
Hide file tree
Showing 7 changed files with 481 additions and 26 deletions.
15 changes: 15 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<version>${jackson.version}</version>
</dependency>

<!-- junixsocket-common -->
<dependency>
<groupId>com.kohlschutter.junixsocket</groupId>
<artifactId>junixsocket-common</artifactId>
Expand All @@ -102,6 +103,13 @@
<artifactId>junixsocket-native-common</artifactId>
</dependency>

<!-- jnr-unixsocket -->
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<version>0.12</version>
</dependency>

<!-- Compile Only Dependencies -->
<dependency>
<groupId>io.sundr</groupId>
Expand Down Expand Up @@ -133,6 +141,13 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
14 changes: 13 additions & 1 deletion client/src/main/java/io/fabric8/docker/client/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public class Config {
public static String HTTP_PROTOCOL_PREFIX = "http://";
public static String HTTPS_PROTOCOL_PREFIX = "https://";

public static final String USE_JNR_UNIX_SOCKET = "docker.useJnrUnixSocket";

private boolean trustCerts;
private String dockerUrl;
private String caCertFile;
Expand All @@ -111,14 +113,15 @@ public class Config {
private String httpsProxy;
private String[] noProxy;
private Map<String, AuthConfig> authConfigs = new HashMap<>();
private boolean useJnrUnixSocket = true;

public Config() {
tryDockerConfig(this);
//In case of Kubernetes / Openshift let's try to read the authconfig from service account token
tryServiceAccount(this);
}

public Config(boolean trustCerts, String dockerUrl, String caCertFile, String caCertData, String clientCertFile, String clientCertData, String clientKeyFile, String clientKeyData, String clientKeyAlgo, String clientKeyPassphrase, String username, String password, String oauthToken, int imageBuildTimeout, int imagePushTimeout, int imagePullTimeout, int imageSearchTimeout, int connectionTimeout, int requestTimeout, String httpProxy, String httpsProxy, String[] noProxy, Map<String, AuthConfig> authConfigs) {
public Config(boolean trustCerts, String dockerUrl, String caCertFile, String caCertData, String clientCertFile, String clientCertData, String clientKeyFile, String clientKeyData, String clientKeyAlgo, String clientKeyPassphrase, String username, String password, String oauthToken, int imageBuildTimeout, int imagePushTimeout, int imagePullTimeout, int imageSearchTimeout, int connectionTimeout, int requestTimeout, String httpProxy, String httpsProxy, String[] noProxy, Map<String, AuthConfig> authConfigs, boolean useJnrUnixSocket) {
this();
this.trustCerts = trustCerts;
this.dockerUrl = dockerUrl;
Expand All @@ -142,6 +145,7 @@ public Config(boolean trustCerts, String dockerUrl, String caCertFile, String ca
this.httpProxy = httpProxy;
this.httpsProxy = httpsProxy;
this.noProxy = noProxy;
this.useJnrUnixSocket = useJnrUnixSocket;

if (authConfigs != null && !authConfigs.isEmpty()) {
this.authConfigs = authConfigs;
Expand Down Expand Up @@ -448,4 +452,12 @@ public Map<String, AuthConfig> getAuthConfigs() {
public void setAuthConfigs(Map<String, AuthConfig> authConfigs) {
this.authConfigs = authConfigs;
}

public boolean getUseJnrUnixSocket() {
return this.useJnrUnixSocket;
}

public void setUseJnrUnixSocket(boolean useJnrUnixSocket) {
this.useJnrUnixSocket = useJnrUnixSocket;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,22 @@

package io.fabric8.docker.client.unix;

import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketImplFactory;
import java.io.*;
import java.net.*;
import java.nio.channels.SocketChannel;
import java.util.ArrayDeque;
import java.util.Queue;

public class UnixSocket extends Socket {
import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;

public class AfUnixSocket extends Socket {

private AFUNIXSocket delegate;
private AFUNIXSocketAddress fixed;
private final Queue<Runnable> optionsToSet = new ArrayDeque<Runnable>();

public UnixSocket(AFUNIXSocket delegate, AFUNIXSocketAddress fixed) {
public AfUnixSocket(AFUNIXSocket delegate, AFUNIXSocketAddress fixed) {
this.fixed = fixed;
this.delegate = delegate;
}
Expand Down Expand Up @@ -107,12 +101,27 @@ public SocketChannel getChannel() {

@Override
public InputStream getInputStream() throws IOException {
return delegate.getInputStream();
return new FilterInputStream(delegate.getInputStream()) {
@Override
public void close() throws IOException {
shutdownInput();
}
};
}

@Override
public OutputStream getOutputStream() throws IOException {
return delegate.getOutputStream();
return new FilterOutputStream(delegate.getOutputStream()) {
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
}

@Override
public void close() throws IOException {
shutdownOutput();
}
};
}

@Override
Expand Down
Loading

0 comments on commit 3fb8a16

Please sign in to comment.