issue #15: fixed socket timeouts and connection shutdown - no exceptions should be thrown if the network node is being shut down, although it can take up to 5 seconds = max(READ_TIMEOUT, CONNECT_TIMEOUT)
This commit is contained in:
parent
fb25852bac
commit
884171fe18
@ -50,9 +50,9 @@ import static ch.dissem.bitmessage.networking.Connection.State.*;
|
|||||||
* A connection to a specific node
|
* A connection to a specific node
|
||||||
*/
|
*/
|
||||||
public class Connection implements Runnable {
|
public class Connection implements Runnable {
|
||||||
|
public static final int READ_TIMEOUT = 2000;
|
||||||
private final static Logger LOG = LoggerFactory.getLogger(Connection.class);
|
private final static Logger LOG = LoggerFactory.getLogger(Connection.class);
|
||||||
private static final int CONNECT_TIMEOUT = 10000;
|
private static final int CONNECT_TIMEOUT = 5000;
|
||||||
|
|
||||||
private InternalContext ctx;
|
private InternalContext ctx;
|
||||||
|
|
||||||
private Mode mode;
|
private Mode mode;
|
||||||
@ -106,8 +106,10 @@ public class Connection implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try (Socket socket = this.socket) {
|
try (Socket socket = this.socket) {
|
||||||
if (!socket.isConnected()) {
|
if (!socket.isConnected()) {
|
||||||
|
LOG.debug("Trying to connect to node " + node);
|
||||||
socket.connect(new InetSocketAddress(node.toInetAddress(), node.getPort()), CONNECT_TIMEOUT);
|
socket.connect(new InetSocketAddress(node.toInetAddress(), node.getPort()), CONNECT_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
socket.setSoTimeout(READ_TIMEOUT);
|
||||||
this.in = socket.getInputStream();
|
this.in = socket.getInputStream();
|
||||||
this.out = socket.getOutputStream();
|
this.out = socket.getOutputStream();
|
||||||
if (mode == CLIENT) {
|
if (mode == CLIENT) {
|
||||||
@ -171,8 +173,8 @@ public class Connection implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException | NodeException e) {
|
} catch (IOException | NodeException e) {
|
||||||
LOG.debug("disconnection from node " + node + ": " + e.getMessage(), e);
|
|
||||||
disconnect();
|
disconnect();
|
||||||
|
LOG.debug("Disconnected from node " + node + ": " + e.getMessage());
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
disconnect();
|
disconnect();
|
||||||
throw e;
|
throw e;
|
||||||
@ -180,6 +182,7 @@ public class Connection implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void activateConnection() {
|
private void activateConnection() {
|
||||||
|
LOG.info("Successfully established connection with node " + node);
|
||||||
state = ACTIVE;
|
state = ACTIVE;
|
||||||
sendAddresses();
|
sendAddresses();
|
||||||
sendInventory();
|
sendInventory();
|
||||||
@ -188,7 +191,9 @@ public class Connection implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendQueue() {
|
private void sendQueue() {
|
||||||
LOG.debug("Sending " + sendingQueue.size() + " messages to node " + node);
|
if (sendingQueue.size() > 0) {
|
||||||
|
LOG.debug("Sending " + sendingQueue.size() + " messages to node " + node);
|
||||||
|
}
|
||||||
for (MessagePayload msg = sendingQueue.poll(); msg != null; msg = sendingQueue.poll()) {
|
for (MessagePayload msg = sendingQueue.poll(); msg != null; msg = sendingQueue.poll()) {
|
||||||
send(msg);
|
send(msg);
|
||||||
}
|
}
|
||||||
|
@ -131,7 +131,7 @@ public class NetworkNode implements NetworkHandler, ContextHolder {
|
|||||||
c.disconnect();
|
c.disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pool.shutdownNow();
|
pool.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startConnection(Connection c) {
|
private void startConnection(Connection c) {
|
||||||
|
@ -45,11 +45,7 @@ public class JdbcConfig {
|
|||||||
this("jdbc:h2:~/jabit;AUTO_SERVER=TRUE", "sa", null);
|
this("jdbc:h2:~/jabit;AUTO_SERVER=TRUE", "sa", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection getConnection() {
|
public Connection getConnection() throws SQLException {
|
||||||
try {
|
return DriverManager.getConnection(dbUrl, dbUser, dbPassword);
|
||||||
return DriverManager.getConnection(dbUrl, dbUser, dbPassword);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user