diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesClientProvider.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesClientProvider.java index 07993e1164..2c4fa802ab 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesClientProvider.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesClientProvider.java @@ -16,6 +16,7 @@ import java.util.logging.Level; import java.util.logging.Logger; +import hudson.model.PeriodicWork; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; @@ -212,6 +213,17 @@ public static boolean gracefulClose(KubernetesClient client, OkHttpClient httpCl } } + private static volatile int runningCallsCount; + private static volatile int queuedCallsCount; + + public static int getRunningCallsCount() { + return runningCallsCount; + } + + public static int getQueuedCallsCount() { + return queuedCallsCount; + } + @Restricted(NoExternalUse.class) // testing only public static void invalidate(String displayName) { clients.invalidate(displayName); @@ -243,4 +255,30 @@ public void onChange(Saveable o, XmlFile file) { super.onChange(o, file); } } + + @Extension + public static class UpdateConnectionCount extends PeriodicWork { + + @Override + public long getRecurrencePeriod() { + return TimeUnit.SECONDS.toMillis(5); + } + + @Override + protected void doRun() { + int runningCallsCount = 0; + int queuedCallsCount = 0; + for (Client client : KubernetesClientProvider.clients.asMap().values()) { + KubernetesClient kClient = client.getClient(); + if (kClient instanceof HttpClientAware) { + OkHttpClient httpClient = ((HttpClientAware) kClient).getHttpClient(); + Dispatcher dispatcher = httpClient.dispatcher(); + runningCallsCount += dispatcher.runningCallsCount(); + queuedCallsCount += dispatcher.queuedCallsCount(); + } + } + KubernetesClientProvider.runningCallsCount = runningCallsCount; + KubernetesClientProvider.queuedCallsCount = queuedCallsCount; + } + } }