mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-20 02:12:00 +01:00
Add socket timeout
This commit is contained in:
parent
6b89f19927
commit
3c3a148b57
@ -45,7 +45,8 @@ public class Connection implements MessageListener {
|
||||
private final ConnectionListener connectionListener;
|
||||
|
||||
private final String portInfo;
|
||||
private final String uid;
|
||||
private final String uid = UUID.randomUUID().toString();
|
||||
;
|
||||
private final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
// set in init
|
||||
@ -75,7 +76,6 @@ public class Connection implements MessageListener {
|
||||
this.connectionListener = connectionListener;
|
||||
|
||||
Log.traceCall();
|
||||
uid = UUID.randomUUID().toString();
|
||||
if (socket.getLocalPort() == 0)
|
||||
portInfo = "port=" + socket.getPort();
|
||||
else
|
||||
@ -131,7 +131,7 @@ public class Connection implements MessageListener {
|
||||
if (!stopped) {
|
||||
try {
|
||||
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);
|
||||
|
||||
Object objectToWrite;
|
||||
|
@ -15,10 +15,9 @@ import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
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 {
|
||||
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;
|
||||
|
||||
@ -92,6 +91,7 @@ public abstract class NetworkNode implements MessageListener, ConnectionListener
|
||||
"We will create a new outbound connection.");
|
||||
|
||||
final SettableFuture<Connection> resultFuture = SettableFuture.create();
|
||||
|
||||
ListenableFuture<Connection> future = executorService.submit(() -> {
|
||||
Thread.currentThread().setName("NetworkNode:SendMessage-to-" + peerAddress);
|
||||
try {
|
||||
@ -117,19 +117,35 @@ public abstract class NetworkNode implements MessageListener, ConnectionListener
|
||||
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>() {
|
||||
public void onSuccess(Connection connection) {
|
||||
UserThread.execute(() -> {
|
||||
timer.cancel();
|
||||
resultFuture.set(connection);
|
||||
});
|
||||
}
|
||||
|
||||
public void onFailure(@NotNull Throwable throwable) {
|
||||
UserThread.execute(() -> {
|
||||
timer.cancel();
|
||||
resultFuture.setException(throwable);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return resultFuture;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user