mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-12-02 17:58:06 -05:00
perf(doctor): run checks concurrently and share docker lookups (#32)
This commit is contained in:
1 parent
3c4118ff1a
commit
dd38f6dc27
20 files changed
+623
-51
No files matched your search
@@ -0,0 +1,85 @@
|
||||
package io.floci.cli.docker;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* A {@link DockerClient} that answers each read-only query once per instance: the first caller
|
||||
* runs the docker subprocess and every later or concurrent caller gets the same answer (or the
|
||||
* same failure). Meant for one diagnostic run, where several checks ask the same questions; never
|
||||
* hold one across a command that changes container or image state.
|
||||
*/
|
||||
public class CachingDockerClient extends DockerClient {
|
||||
|
||||
private final DockerClient delegate;
|
||||
|
||||
private final ConcurrentHashMap<String, Memo<?>> memos = new ConcurrentHashMap<>();
|
||||
|
||||
public CachingDockerClient() {
|
||||
this(new DockerClient());
|
||||
}
|
||||
|
||||
/** {@code delegate} answers the first call for each query; a test seam for counting. */
|
||||
public CachingDockerClient(DockerClient delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String dockerVersion() throws DockerException {
|
||||
return memo("version", delegate::dockerVersion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDaemonReachable() {
|
||||
try {
|
||||
return memo("daemon", delegate::isDaemonReachable);
|
||||
} catch (DockerException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ContainerInfo> inspectContainer(String name) throws DockerException {
|
||||
return memo("container:" + name, () -> delegate.inspectContainer(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isImagePresent(String image) throws DockerException {
|
||||
return memo("image:" + image, () -> delegate.isImagePresent(image));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> imageLabel(String image, String key) throws DockerException {
|
||||
return memo("label:" + image + ":" + key, () -> delegate.imageLabel(image, key));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T memo(String key, DockerCall<T> call) throws DockerException {
|
||||
return ((Memo<T>) memos.computeIfAbsent(key, k -> new Memo<>())).get(call);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface DockerCall<T> {
|
||||
T call() throws DockerException;
|
||||
}
|
||||
|
||||
// One answer per key, computed by the first caller while concurrent callers wait on the lock.
|
||||
private static final class Memo<T> {
|
||||
private boolean done;
|
||||
private T value;
|
||||
private DockerException failure;
|
||||
|
||||
synchronized T get(DockerCall<T> call) throws DockerException {
|
||||
if (!done) {
|
||||
try {
|
||||
value = call.call();
|
||||
} catch (DockerException e) {
|
||||
failure = e;
|
||||
}
|
||||
done = true;
|
||||
}
|
||||
if (failure != null) throw failure;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,6 +85,18 @@ public class DockerClient {
|
||||
}
|
||||
}
|
||||
|
||||
/** The value of label {@code key} on a local image, empty when the label or image is missing. */
|
||||
public Optional<String> imageLabel(String image, String key) throws DockerException {
|
||||
try {
|
||||
String out = run("docker", "inspect", "--format",
|
||||
"{{index .Config.Labels \"" + key + "\"}}", image);
|
||||
String value = out.trim();
|
||||
return value.isEmpty() || "<no value>".equals(value) ? Optional.empty() : Optional.of(value);
|
||||
} catch (DockerException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public void pull(String image, String policy) throws DockerException {
|
||||
if ("never".equalsIgnoreCase(policy)) return;
|
||||
if ("missing".equalsIgnoreCase(policy) && isImagePresent(image)) return;
|
||||
|
||||
Reference in new issue
Block a user