提交 790a3071 编写于 作者: D David Hoover

Reduce the thread overhead in NodeMonitorUpdater.

Before, if N nodes connected quickly, this would spawn N threads all
sleeping for 5 seconds before realizing they don't need to do anything.
上级 17f5227b
......@@ -4,9 +4,11 @@ import hudson.Extension;
import hudson.model.Computer;
import hudson.model.TaskListener;
import hudson.slaves.ComputerListener;
import hudson.util.Futures;
import jenkins.model.Jenkins;
import java.io.IOException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
......@@ -20,7 +22,16 @@ import jenkins.util.Timer;
@Extension
public class NodeMonitorUpdater extends ComputerListener {
private final AtomicInteger id = new AtomicInteger();
private static final Runnable MONITOR_UPDATER = new Runnable() {
@Override
public void run() {
for (NodeMonitor nm : Jenkins.getInstance().getComputer().getMonitors()) {
nm.triggerUpdate();
}
}
};
private Future<?> future = Futures.precomputed(null);
/**
* Triggers the update with 5 seconds quiet period, to avoid triggering data check too often
......@@ -28,18 +39,9 @@ public class NodeMonitorUpdater extends ComputerListener {
*/
@Override
public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException {
Timer.get().schedule(new Runnable() {
final int _id = id.incrementAndGet();
public void run() {
if (id.get() != _id)
return;
for (NodeMonitor nm : Jenkins.getInstance().getComputer().getMonitors()) {
nm.triggerUpdate();
}
}
}, 5, TimeUnit.SECONDS);
synchronized(this) {
future.cancel(false);
future = Timer.get().schedule(MONITOR_UPDATER, 5, TimeUnit.SECONDS);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册