From 733335ef4294b9ef76d0dfb91020a2566348666a Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Tue, 19 Jan 2016 21:09:46 +0100 Subject: [PATCH] Improved performance and network stability --- .../bitmessage/networking/Connection.java | 8 +++----- .../networking/DefaultNetworkHandler.java | 17 ++++++++++++----- .../bitmessage/repository/JdbcInventory.java | 5 ++++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java index 176517b..76094ff 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java @@ -139,6 +139,9 @@ class Connection { @SuppressWarnings("RedundantIfStatement") private boolean syncFinished(NetworkMessage msg) { + if (mode != SYNC){ + return false; + } if (Thread.interrupted()) { return true; } @@ -239,11 +242,6 @@ class Connection { } finally { if (commonRequestedObjects.remove(objectMessage.getInventoryVector())) { LOG.debug("Received object that wasn't requested."); -// if (!requestedObjects.isEmpty()) { -// DebugUtils.saveToFile(objectMessage); -// LOG.debug(objectMessage.getInventoryVector() + " was not in " -// + requestedObjects.toString()); -// } } } break; diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java b/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java index bd58d8e..d3bec17 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java @@ -43,16 +43,16 @@ import static ch.dissem.bitmessage.networking.Connection.Mode.CLIENT; import static ch.dissem.bitmessage.networking.Connection.Mode.SERVER; import static ch.dissem.bitmessage.networking.Connection.State.ACTIVE; import static ch.dissem.bitmessage.utils.DebugUtils.inc; -import static ch.dissem.bitmessage.utils.UnixTime.MINUTE; import static java.util.Collections.newSetFromMap; /** * Handles all the networky stuff. */ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder { - public final static int NETWORK_MAGIC_NUMBER = 8; private final static Logger LOG = LoggerFactory.getLogger(DefaultNetworkHandler.class); - private static final Random RANDOM = new Random(); + + public final static int NETWORK_MAGIC_NUMBER = 8; + private final Collection connections = new ConcurrentLinkedQueue<>(); private final ExecutorService pool; private InternalContext ctx; @@ -155,12 +155,14 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder { if (diff == 0) break; } } + boolean forcedDisconnect = false; for (Iterator iterator = connections.iterator(); iterator.hasNext(); ) { Connection c = iterator.next(); // Just in case they were all created at the same time, don't disconnect // all at once. - if (now - c.getStartTime() + RANDOM.nextInt(5 * MINUTE) > ctx.getConnectionTTL()) { + if (!forcedDisconnect && now - c.getStartTime() > ctx.getConnectionTTL()) { c.disconnect(); + forcedDisconnect = true; } switch (c.getState()) { case DISCONNECTED: @@ -306,8 +308,13 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder { } } Iterator iterator = inventoryVectors.iterator(); + InventoryVector next; + if (iterator.hasNext()) { + next = iterator.next(); + } else { + return; + } boolean firstRound = true; - InventoryVector next = iterator.next(); while (firstRound || iterator.hasNext()) { if (!firstRound) { next = iterator.next(); diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java index 9c9c9e7..3336475 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java @@ -80,7 +80,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory { @Override public List getMissing(List offer, long... streams) { for (long stream : streams) { - getCache(stream).forEach((iv, t) -> offer.remove(iv)); + offer.removeAll(getCache(stream).keySet()); } return offer; } @@ -132,6 +132,9 @@ public class JdbcInventory extends JdbcHelper implements Inventory { @Override public void storeObject(ObjectMessage object) { + if (getCache(object.getStream()).containsKey(object.getInventoryVector())) + return; + try (Connection connection = config.getConnection()) { PreparedStatement ps = connection.prepareStatement("INSERT INTO Inventory (hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)"); InventoryVector iv = object.getInventoryVector();