From 3aa9b5f0b6f3eee4bf2cb00ce4e49954472f19ed Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sat, 8 May 2021 22:36:41 +0100 Subject: [PATCH] Increased timeout when syncing multiple blocks from 4s to 10s. --- .../org/qortal/controller/Synchronizer.java | 5 +++- src/main/java/org/qortal/network/Peer.java | 23 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/qortal/controller/Synchronizer.java b/src/main/java/org/qortal/controller/Synchronizer.java index a160ea62..cba5d3c0 100644 --- a/src/main/java/org/qortal/controller/Synchronizer.java +++ b/src/main/java/org/qortal/controller/Synchronizer.java @@ -63,6 +63,9 @@ public class Synchronizer { /* Minimum peer version that supports syncing multiple blocks at once via GetBlocksMessage */ private static final long PEER_VERSION_160 = 0x0100060000L; + /** Maximum time to wait for a peer to respond with blocks (ms) */ + private static final int FETCH_BLOCKS_TIMEOUT = 10000; + private static Synchronizer instance; @@ -1189,7 +1192,7 @@ public class Synchronizer { private List fetchBlocks(Repository repository, Peer peer, byte[] parentSignature, int numberRequested) throws InterruptedException { Message getBlocksMessage = new GetBlocksMessage(parentSignature, numberRequested); - Message message = peer.getResponse(getBlocksMessage); + Message message = peer.getResponseWithTimeout(getBlocksMessage, FETCH_BLOCKS_TIMEOUT); if (message == null || message.getType() != MessageType.BLOCKS) { return null; } diff --git a/src/main/java/org/qortal/network/Peer.java b/src/main/java/org/qortal/network/Peer.java index cc4ad918..e5bd369d 100644 --- a/src/main/java/org/qortal/network/Peer.java +++ b/src/main/java/org/qortal/network/Peer.java @@ -538,7 +538,23 @@ public class Peer { } /** - * Send message to peer and await response. + * Send message to peer and await response, using default RESPONSE_TIMEOUT. + *

+ * Message is assigned a random ID and sent. If a response with matching ID is received then it is returned to caller. + *

+ * If no response with matching ID within timeout, or some other error/exception occurs, then return null.
+ * (Assume peer will be rapidly disconnected after this). + * + * @param message + * @return Message if valid response received; null if not or error/exception occurs + * @throws InterruptedException + */ + public Message getResponse(Message message) throws InterruptedException { + return getResponseWithTimeout(message, RESPONSE_TIMEOUT); + } + + /** + * Send message to peer and await response, using custom timeout. *

* Message is assigned a random ID and sent. If a response with matching ID is received then it is returned to caller. *

@@ -546,10 +562,11 @@ public class Peer { * (Assume peer will be rapidly disconnected after this). * * @param message + * @param timeout * @return Message if valid response received; null if not or error/exception occurs * @throws InterruptedException */ - public Message getResponse(Message message) throws InterruptedException { + public Message getResponseWithTimeout(Message message, int timeout) throws InterruptedException { BlockingQueue blockingQueue = new ArrayBlockingQueue<>(1); // Assign random ID to this message @@ -570,7 +587,7 @@ public class Peer { } try { - return blockingQueue.poll(RESPONSE_TIMEOUT, TimeUnit.MILLISECONDS); + return blockingQueue.poll(timeout, TimeUnit.MILLISECONDS); } finally { this.replyQueues.remove(id); }