mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Randomise next polling interval after pollNow().
This commit is contained in:
@@ -1,11 +1,15 @@
|
|||||||
package org.briarproject.plugins;
|
package org.briarproject.plugins;
|
||||||
|
|
||||||
|
import org.briarproject.api.TransportId;
|
||||||
import org.briarproject.api.lifecycle.IoExecutor;
|
import org.briarproject.api.lifecycle.IoExecutor;
|
||||||
import org.briarproject.api.plugins.ConnectionRegistry;
|
import org.briarproject.api.plugins.ConnectionRegistry;
|
||||||
import org.briarproject.api.plugins.Plugin;
|
import org.briarproject.api.plugins.Plugin;
|
||||||
import org.briarproject.api.system.Timer;
|
import org.briarproject.api.system.Timer;
|
||||||
|
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -20,14 +24,19 @@ class PollerImpl implements Poller {
|
|||||||
|
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
private final ConnectionRegistry connectionRegistry;
|
private final ConnectionRegistry connectionRegistry;
|
||||||
|
private final SecureRandom random;
|
||||||
private final Timer timer;
|
private final Timer timer;
|
||||||
|
private final Map<TransportId, PollTask> tasks;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PollerImpl(@IoExecutor Executor ioExecutor,
|
PollerImpl(@IoExecutor Executor ioExecutor,
|
||||||
ConnectionRegistry connectionRegistry, Timer timer) {
|
ConnectionRegistry connectionRegistry, SecureRandom random,
|
||||||
|
Timer timer) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.connectionRegistry = connectionRegistry;
|
this.connectionRegistry = connectionRegistry;
|
||||||
|
this.random = random;
|
||||||
this.timer = timer;
|
this.timer = timer;
|
||||||
|
tasks = new ConcurrentHashMap<TransportId, PollTask>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
@@ -35,17 +44,28 @@ class PollerImpl implements Poller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addPlugin(Plugin p) {
|
public void addPlugin(Plugin p) {
|
||||||
schedule(p, true);
|
// Randomise first polling interval
|
||||||
|
schedule(p, randomise(p.getPollingInterval()), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void schedule(Plugin plugin, boolean randomise) {
|
public void pollNow(Plugin p) {
|
||||||
long interval = plugin.getPollingInterval();
|
// Randomise next polling interval
|
||||||
// Randomise intervals at startup to spread out connection attempts
|
schedule(p, 0, true);
|
||||||
if (randomise) interval = (long) (interval * Math.random());
|
|
||||||
timer.schedule(new PollTask(plugin), interval);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pollNow(final Plugin p) {
|
private int randomise(int interval) {
|
||||||
|
return (int) (interval * random.nextDouble());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void schedule(Plugin p, int interval, boolean randomiseNext) {
|
||||||
|
// Replace any previously scheduled task for this plugin
|
||||||
|
PollTask task = new PollTask(p, randomiseNext);
|
||||||
|
PollTask replaced = tasks.put(p.getId(), task);
|
||||||
|
if (replaced != null) replaced.cancel();
|
||||||
|
timer.schedule(task, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void poll(final Plugin p) {
|
||||||
ioExecutor.execute(new Runnable() {
|
ioExecutor.execute(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
@@ -58,15 +78,20 @@ class PollerImpl implements Poller {
|
|||||||
private class PollTask extends TimerTask {
|
private class PollTask extends TimerTask {
|
||||||
|
|
||||||
private final Plugin plugin;
|
private final Plugin plugin;
|
||||||
|
private final boolean randomiseNext;
|
||||||
|
|
||||||
private PollTask(Plugin plugin) {
|
private PollTask(Plugin plugin, boolean randomiseNext) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
this.randomiseNext = randomiseNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
pollNow(plugin);
|
tasks.remove(plugin.getId());
|
||||||
schedule(plugin, false);
|
int interval = plugin.getPollingInterval();
|
||||||
|
if (randomiseNext) interval = randomise(interval);
|
||||||
|
schedule(plugin, interval, false);
|
||||||
|
poll(plugin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user