mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-11-05 05:46:44 +00:00
progressive read timeout
This commit is contained in:
parent
b448927a6e
commit
32327928b5
1 changed files with 9 additions and 3 deletions
|
@ -21,7 +21,7 @@ public class TcpTransport implements Transport, Closeable {
|
||||||
private static final Logger log = LoggerFactory.getLogger(TcpTransport.class);
|
private static final Logger log = LoggerFactory.getLogger(TcpTransport.class);
|
||||||
|
|
||||||
public static final int DEFAULT_PORT = 50001;
|
public static final int DEFAULT_PORT = 50001;
|
||||||
private static final int READ_TIMEOUT_SECS = 15;
|
private static final int[] READ_TIMEOUT_SECS = {3, 8, 16, 34};
|
||||||
|
|
||||||
protected final HostAndPort server;
|
protected final HostAndPort server;
|
||||||
protected final SocketFactory socketFactory;
|
protected final SocketFactory socketFactory;
|
||||||
|
@ -37,6 +37,7 @@ public class TcpTransport implements Transport, Closeable {
|
||||||
private boolean running = false;
|
private boolean running = false;
|
||||||
private volatile boolean reading = true;
|
private volatile boolean reading = true;
|
||||||
private boolean firstRead = true;
|
private boolean firstRead = true;
|
||||||
|
private int readTimeoutIndex;
|
||||||
|
|
||||||
private final JsonRpcServer jsonRpcServer = new JsonRpcServer();
|
private final JsonRpcServer jsonRpcServer = new JsonRpcServer();
|
||||||
private final SubscriptionService subscriptionService = new SubscriptionService();
|
private final SubscriptionService subscriptionService = new SubscriptionService();
|
||||||
|
@ -77,14 +78,19 @@ public class TcpTransport implements Transport, Closeable {
|
||||||
|
|
||||||
private String readResponse() throws IOException {
|
private String readResponse() throws IOException {
|
||||||
try {
|
try {
|
||||||
if(!readLock.tryLock(READ_TIMEOUT_SECS, TimeUnit.SECONDS)) {
|
if(!readLock.tryLock(READ_TIMEOUT_SECS[readTimeoutIndex], TimeUnit.SECONDS)) {
|
||||||
log.debug("No response from server");
|
readTimeoutIndex = Math.min(readTimeoutIndex + 1, READ_TIMEOUT_SECS.length - 1);
|
||||||
|
log.debug("No response from server, setting read timeout to " + READ_TIMEOUT_SECS[readTimeoutIndex] + " secs");
|
||||||
throw new IOException("No response from server");
|
throw new IOException("No response from server");
|
||||||
}
|
}
|
||||||
} catch(InterruptedException e) {
|
} catch(InterruptedException e) {
|
||||||
throw new IOException("Read thread interrupted");
|
throw new IOException("Read thread interrupted");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(readTimeoutIndex == READ_TIMEOUT_SECS.length - 1) {
|
||||||
|
readTimeoutIndex--;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if(firstRead) {
|
if(firstRead) {
|
||||||
readingCondition.signal();
|
readingCondition.signal();
|
||||||
|
|
Loading…
Reference in a new issue