Improved performance and network stability
This commit is contained in:
parent
e29310102f
commit
733335ef42
@ -139,6 +139,9 @@ class Connection {
|
|||||||
|
|
||||||
@SuppressWarnings("RedundantIfStatement")
|
@SuppressWarnings("RedundantIfStatement")
|
||||||
private boolean syncFinished(NetworkMessage msg) {
|
private boolean syncFinished(NetworkMessage msg) {
|
||||||
|
if (mode != SYNC){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -239,11 +242,6 @@ class Connection {
|
|||||||
} finally {
|
} finally {
|
||||||
if (commonRequestedObjects.remove(objectMessage.getInventoryVector())) {
|
if (commonRequestedObjects.remove(objectMessage.getInventoryVector())) {
|
||||||
LOG.debug("Received object that wasn't requested.");
|
LOG.debug("Received object that wasn't requested.");
|
||||||
// if (!requestedObjects.isEmpty()) {
|
|
||||||
// DebugUtils.saveToFile(objectMessage);
|
|
||||||
// LOG.debug(objectMessage.getInventoryVector() + " was not in "
|
|
||||||
// + requestedObjects.toString());
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -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.Mode.SERVER;
|
||||||
import static ch.dissem.bitmessage.networking.Connection.State.ACTIVE;
|
import static ch.dissem.bitmessage.networking.Connection.State.ACTIVE;
|
||||||
import static ch.dissem.bitmessage.utils.DebugUtils.inc;
|
import static ch.dissem.bitmessage.utils.DebugUtils.inc;
|
||||||
import static ch.dissem.bitmessage.utils.UnixTime.MINUTE;
|
|
||||||
import static java.util.Collections.newSetFromMap;
|
import static java.util.Collections.newSetFromMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles all the networky stuff.
|
* Handles all the networky stuff.
|
||||||
*/
|
*/
|
||||||
public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
||||||
public final static int NETWORK_MAGIC_NUMBER = 8;
|
|
||||||
private final static Logger LOG = LoggerFactory.getLogger(DefaultNetworkHandler.class);
|
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<Connection> connections = new ConcurrentLinkedQueue<>();
|
private final Collection<Connection> connections = new ConcurrentLinkedQueue<>();
|
||||||
private final ExecutorService pool;
|
private final ExecutorService pool;
|
||||||
private InternalContext ctx;
|
private InternalContext ctx;
|
||||||
@ -155,12 +155,14 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
|||||||
if (diff == 0) break;
|
if (diff == 0) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
boolean forcedDisconnect = false;
|
||||||
for (Iterator<Connection> iterator = connections.iterator(); iterator.hasNext(); ) {
|
for (Iterator<Connection> iterator = connections.iterator(); iterator.hasNext(); ) {
|
||||||
Connection c = iterator.next();
|
Connection c = iterator.next();
|
||||||
// Just in case they were all created at the same time, don't disconnect
|
// Just in case they were all created at the same time, don't disconnect
|
||||||
// all at once.
|
// all at once.
|
||||||
if (now - c.getStartTime() + RANDOM.nextInt(5 * MINUTE) > ctx.getConnectionTTL()) {
|
if (!forcedDisconnect && now - c.getStartTime() > ctx.getConnectionTTL()) {
|
||||||
c.disconnect();
|
c.disconnect();
|
||||||
|
forcedDisconnect = true;
|
||||||
}
|
}
|
||||||
switch (c.getState()) {
|
switch (c.getState()) {
|
||||||
case DISCONNECTED:
|
case DISCONNECTED:
|
||||||
@ -306,8 +308,13 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Iterator<InventoryVector> iterator = inventoryVectors.iterator();
|
Iterator<InventoryVector> iterator = inventoryVectors.iterator();
|
||||||
|
InventoryVector next;
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
next = iterator.next();
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
boolean firstRound = true;
|
boolean firstRound = true;
|
||||||
InventoryVector next = iterator.next();
|
|
||||||
while (firstRound || iterator.hasNext()) {
|
while (firstRound || iterator.hasNext()) {
|
||||||
if (!firstRound) {
|
if (!firstRound) {
|
||||||
next = iterator.next();
|
next = iterator.next();
|
||||||
|
@ -80,7 +80,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
|||||||
@Override
|
@Override
|
||||||
public List<InventoryVector> getMissing(List<InventoryVector> offer, long... streams) {
|
public List<InventoryVector> getMissing(List<InventoryVector> offer, long... streams) {
|
||||||
for (long stream : streams) {
|
for (long stream : streams) {
|
||||||
getCache(stream).forEach((iv, t) -> offer.remove(iv));
|
offer.removeAll(getCache(stream).keySet());
|
||||||
}
|
}
|
||||||
return offer;
|
return offer;
|
||||||
}
|
}
|
||||||
@ -132,6 +132,9 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void storeObject(ObjectMessage object) {
|
public void storeObject(ObjectMessage object) {
|
||||||
|
if (getCache(object.getStream()).containsKey(object.getInventoryVector()))
|
||||||
|
return;
|
||||||
|
|
||||||
try (Connection connection = config.getConnection()) {
|
try (Connection connection = config.getConnection()) {
|
||||||
PreparedStatement ps = connection.prepareStatement("INSERT INTO Inventory (hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)");
|
PreparedStatement ps = connection.prepareStatement("INSERT INTO Inventory (hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)");
|
||||||
InventoryVector iv = object.getInventoryVector();
|
InventoryVector iv = object.getInventoryVector();
|
||||||
|
Loading…
Reference in New Issue
Block a user