Skip to content
Draft
Changes from all 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
39 changes: 33 additions & 6 deletions src/main/java/io/jenkins/docker/client/DockerAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import jenkins.model.Jenkins;
import jenkins.util.SystemProperties;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerEndpoint;
import org.kohsuke.stapler.AncestorInPath;
Expand Down Expand Up @@ -64,6 +65,9 @@

private String hostname;

private static boolean DISABLE_CLIENT_CACHE =
SystemProperties.getBoolean("io.jenkins.docker.client.DockerAPI.DISABLE_CLIENT_CACHE", false);

/**
* Is this host actually a swarm?
*/
Expand Down Expand Up @@ -196,39 +200,62 @@
final String dockerUri, final String credentialsId, final int readTimeout, final int connectTimeout) {
final Integer readTimeoutInMillisecondsOrNull = readTimeout > 0 ? readTimeout * 1000 : null;
final Integer connectTimeoutInMillisecondsOrNull = connectTimeout > 0 ? connectTimeout * 1000 : null;

if (DISABLE_CLIENT_CACHE) {

Check warning on line 204 in src/main/java/io/jenkins/docker/client/DockerAPI.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 204 is only partially covered, one branch is missing
SharableDockerClient client = makeClient(
dockerUri, credentialsId, readTimeoutInMillisecondsOrNull, connectTimeoutInMillisecondsOrNull);
LOGGER.debug("Created non-cached connection {} to {}", client, dockerUri);
return client;

Check warning on line 208 in src/main/java/io/jenkins/docker/client/DockerAPI.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 205-208 are not covered by tests
}

final DockerClientParameters cacheKey = new DockerClientParameters(
dockerUri, credentialsId, readTimeoutInMillisecondsOrNull, connectTimeoutInMillisecondsOrNull);
synchronized (CLIENT_CACHE) {
SharableDockerClient client = CLIENT_CACHE.getAndIncrementUsage(cacheKey);
if (client == null) {
client = makeClient(
dockerUri, credentialsId, readTimeoutInMillisecondsOrNull, connectTimeoutInMillisecondsOrNull);
LOGGER.info("Cached connection {} to {}", client, cacheKey);
CLIENT_CACHE.cacheAndIncrementUsage(cacheKey, client);
client.setCached(true);
LOGGER.debug("Cached connection {} to {}", client, cacheKey);
}
return client;
}
}

/**
* A docker-client that, when {@link Closeable#close()} is called, merely
* decrements the usage count. It'll only get properly closed once it's
* purged from the cache.
* decrements the usage count - when {@code DISABLE_CLIENT_CACHE} is not enabled. It'll only get properly closed
* once it's purged from the cache.
*/
private static class SharableDockerClient extends DelegatingDockerClient {

private boolean cached = false;

public SharableDockerClient(DockerClient delegate) {
super(delegate);
}

/**
* Sets whether this client is managed by the cache.
*/
public void setCached(boolean cached) {
this.cached = cached;
}

/**
* Tell the cache we no longer need the {@link DockerClient} and it can
* be thrown away if it remains unused.
* If the client is not cached, close it immediately.
*/
@Override
public void close() {
synchronized (CLIENT_CACHE) {
CLIENT_CACHE.decrementUsage(this);
public void close() throws IOException {
if (cached) {

Check warning on line 253 in src/main/java/io/jenkins/docker/client/DockerAPI.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 253 is only partially covered, one branch is missing
synchronized (CLIENT_CACHE) {
CLIENT_CACHE.decrementUsage(this);
}
} else {
reallyClose();

Check warning on line 258 in src/main/java/io/jenkins/docker/client/DockerAPI.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 258 is not covered by tests
}
}

Expand Down
Loading