mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-06-26 07:40:35 -04:00
Add socket timeout
This commit is contained in:
parent
6b89f19927
commit
3c3a148b57
2 changed files with 23 additions and 7 deletions
|
@ -45,7 +45,8 @@ public class Connection implements MessageListener {
|
||||||
private final ConnectionListener connectionListener;
|
private final ConnectionListener connectionListener;
|
||||||
|
|
||||||
private final String portInfo;
|
private final String portInfo;
|
||||||
private final String uid;
|
private final String uid = UUID.randomUUID().toString();
|
||||||
|
;
|
||||||
private final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
|
private final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
|
||||||
|
|
||||||
// set in init
|
// set in init
|
||||||
|
@ -75,7 +76,6 @@ public class Connection implements MessageListener {
|
||||||
this.connectionListener = connectionListener;
|
this.connectionListener = connectionListener;
|
||||||
|
|
||||||
Log.traceCall();
|
Log.traceCall();
|
||||||
uid = UUID.randomUUID().toString();
|
|
||||||
if (socket.getLocalPort() == 0)
|
if (socket.getLocalPort() == 0)
|
||||||
portInfo = "port=" + socket.getPort();
|
portInfo = "port=" + socket.getPort();
|
||||||
else
|
else
|
||||||
|
@ -131,7 +131,7 @@ public class Connection implements MessageListener {
|
||||||
if (!stopped) {
|
if (!stopped) {
|
||||||
try {
|
try {
|
||||||
log.info("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" +
|
log.info("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" +
|
||||||
"Write object to outputStream to peer: {} ({objectId=})\nmessage={}"
|
"Write object to outputStream to peer: {} (uid={})\nmessage={}"
|
||||||
+ "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", getPeerAddress(), uid, message);
|
+ "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", getPeerAddress(), uid, message);
|
||||||
|
|
||||||
Object objectToWrite;
|
Object objectToWrite;
|
||||||
|
|
|
@ -15,10 +15,9 @@ import java.io.IOException;
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.HashSet;
|
import java.util.*;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
|
@ -26,7 +25,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
public abstract class NetworkNode implements MessageListener, ConnectionListener {
|
public abstract class NetworkNode implements MessageListener, ConnectionListener {
|
||||||
private static final Logger log = LoggerFactory.getLogger(NetworkNode.class);
|
private static final Logger log = LoggerFactory.getLogger(NetworkNode.class);
|
||||||
|
|
||||||
private static final int CREATE_SOCKET_TIMEOUT = 1 * 1000; // 10 sec.
|
private static final int CREATE_SOCKET_TIMEOUT = 10 * 1000; // 10 sec.
|
||||||
|
|
||||||
protected final int servicePort;
|
protected final int servicePort;
|
||||||
|
|
||||||
|
@ -92,6 +91,7 @@ public abstract class NetworkNode implements MessageListener, ConnectionListener
|
||||||
"We will create a new outbound connection.");
|
"We will create a new outbound connection.");
|
||||||
|
|
||||||
final SettableFuture<Connection> resultFuture = SettableFuture.create();
|
final SettableFuture<Connection> resultFuture = SettableFuture.create();
|
||||||
|
|
||||||
ListenableFuture<Connection> future = executorService.submit(() -> {
|
ListenableFuture<Connection> future = executorService.submit(() -> {
|
||||||
Thread.currentThread().setName("NetworkNode:SendMessage-to-" + peerAddress);
|
Thread.currentThread().setName("NetworkNode:SendMessage-to-" + peerAddress);
|
||||||
try {
|
try {
|
||||||
|
@ -117,19 +117,35 @@ public abstract class NetworkNode implements MessageListener, ConnectionListener
|
||||||
throw throwable;
|
throw throwable;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Timer timer = new Timer();
|
||||||
|
timer.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Thread.currentThread().setName("TimerTask-" + new Random().nextInt(10000));
|
||||||
|
future.cancel(true);
|
||||||
|
String message = "Timeout occurred when trying to create Socket.";
|
||||||
|
log.warn(message);
|
||||||
|
resultFuture.setException(new TimeoutException(message));
|
||||||
|
}
|
||||||
|
}, CREATE_SOCKET_TIMEOUT);
|
||||||
|
|
||||||
Futures.addCallback(future, new FutureCallback<Connection>() {
|
Futures.addCallback(future, new FutureCallback<Connection>() {
|
||||||
public void onSuccess(Connection connection) {
|
public void onSuccess(Connection connection) {
|
||||||
UserThread.execute(() -> {
|
UserThread.execute(() -> {
|
||||||
|
timer.cancel();
|
||||||
resultFuture.set(connection);
|
resultFuture.set(connection);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onFailure(@NotNull Throwable throwable) {
|
public void onFailure(@NotNull Throwable throwable) {
|
||||||
UserThread.execute(() -> {
|
UserThread.execute(() -> {
|
||||||
|
timer.cancel();
|
||||||
resultFuture.setException(throwable);
|
resultFuture.setException(throwable);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return resultFuture;
|
return resultFuture;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue