add random delay to timer based tasks

This commit is contained in:
Manfred Karrer 2016-01-18 19:13:57 +01:00
parent a417768782
commit 6312ce31d4
3 changed files with 15 additions and 20 deletions

View File

@ -21,6 +21,7 @@ import org.slf4j.LoggerFactory;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@ -57,7 +58,8 @@ public class PeerExchangeManager implements MessageListener {
networkNode.addMessageListener(this);
executor = Utilities.getScheduledThreadPoolExecutor("PeerExchangeManager", 1, 10, 5);
executor.scheduleAtFixedRate(() -> UserThread.execute(() -> trySendGetPeersRequest()), 7, 7, TimeUnit.MINUTES);
long delay = new Random().nextInt(60) + 60 * 6; // 6-7 min.
executor.scheduleAtFixedRate(() -> UserThread.execute(() -> trySendGetPeersRequest()), delay, delay, TimeUnit.SECONDS);
}
public void shutDown() {

View File

@ -449,8 +449,9 @@ public class PeerManager implements MessageListener, ConnectionListener {
}
protected void startCheckSeedNodeConnectionTask() {
long delay = new Random().nextInt(60) + 60 * 2; // 2-3 min.
checkSeedNodeConnectionExecutor.scheduleAtFixedRate(() -> UserThread.execute(()
-> checkSeedNodeConnections()), 2, 2, TimeUnit.MINUTES);
-> checkSeedNodeConnections()), delay, delay, TimeUnit.SECONDS);
}
// We want to stay connected to at least one seed node to avoid to get isolated with a group of peers

View File

@ -1,6 +1,7 @@
package io.bitsquare.p2p.storage;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.MoreExecutors;
import io.bitsquare.app.Log;
import io.bitsquare.common.ByteArray;
import io.bitsquare.common.UserThread;
@ -30,21 +31,22 @@ import java.security.KeyPair;
import java.security.PublicKey;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
// Run in UserThread
public class P2PDataStorage implements MessageListener {
private static final Logger log = LoggerFactory.getLogger(P2PDataStorage.class);
@VisibleForTesting
public static int CHECK_TTL_INTERVAL = 10 * 60 * 1000;
public static int CHECK_TTL_INTERVAL = new Random().nextInt(1000) + 10 * 60 * 1000; // 10-11 min.
private final PeerManager peerManager;
private final Map<ByteArray, ProtectedData> map = new HashMap<>();
private final CopyOnWriteArraySet<HashMapChangedListener> hashMapChangedListeners = new CopyOnWriteArraySet<>();
private HashMap<ByteArray, Integer> sequenceNumberMap = new HashMap<>();
private final Storage<HashMap> storage;
private final Timer timer = new Timer();
protected final ScheduledThreadPoolExecutor removeExpiredEntriesExecutor;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
@ -57,7 +59,8 @@ public class P2PDataStorage implements MessageListener {
networkNode.addMessageListener(this);
storage = new Storage<>(storageDir);
removeExpiredEntriesExecutor = Utilities.getScheduledThreadPoolExecutor("removeExpiredEntries", 1, 10, 5);
init();
}
@ -67,19 +70,8 @@ public class P2PDataStorage implements MessageListener {
if (persisted != null)
sequenceNumberMap = persisted;
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
Utilities.setThreadName("RemoveExpiredEntriesTimer");
UserThread.execute(() -> removeExpiredEntries());
} catch (Throwable t) {
log.error("Executing task failed. " + t.getMessage());
t.printStackTrace();
}
}
},
CHECK_TTL_INTERVAL, CHECK_TTL_INTERVAL);
removeExpiredEntriesExecutor.scheduleAtFixedRate(() -> UserThread.execute(()
-> removeExpiredEntries()), CHECK_TTL_INTERVAL, CHECK_TTL_INTERVAL, TimeUnit.SECONDS);
}
private void removeExpiredEntries() {
@ -139,7 +131,7 @@ public class P2PDataStorage implements MessageListener {
public void shutDown() {
Log.traceCall();
timer.cancel();
MoreExecutors.shutdownAndAwaitTermination(removeExpiredEntriesExecutor, 500, TimeUnit.MILLISECONDS);
}
public boolean add(ProtectedData protectedData, @Nullable Address sender) {