Skip to content

Commit

Permalink
Improve logs and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
djing-chan committed Dec 19, 2023
1 parent 189e94a commit f811f28
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ protected Connection(Socket socket,
} catch (Exception exception) {
//todo StreamCorruptedException from i2p at shutdown. prob it send some text data at shut down
if (isInputStreamActive()) {
log.debug("Call shutdown from startListen read handler {} due exception={}", this, exception.toString());
log.debug("Exception at input handler on {}", this, exception);
close(CloseReason.EXCEPTION.exception(exception));
// EOFException expected if connection got closed

// EOFException expected if connection got closed (Socket closed message)
// TODO maybe also filter SocketException
if (!(exception instanceof EOFException)) {
errorHandler.accept(this, exception);
}
Expand Down Expand Up @@ -191,6 +193,8 @@ Connection send(EnvelopePayloadMessage envelopePayloadMessage, AuthorizationToke
} catch (Throwable throwable) {
if (!isStopped) {
throw throwable;
} else {
log.warn("Send message failed at stopped connection", throwable);
}
}
}
Expand All @@ -207,11 +211,10 @@ Connection send(EnvelopePayloadMessage envelopePayloadMessage, AuthorizationToke
return this;
} catch (IOException exception) {
if (!isStopped) {
log.error("Call shutdown from send {} due exception={}", this, exception.toString());
log.error("Send message failed. {}", this, exception);
close(CloseReason.EXCEPTION.exception(exception));
}
// We wrap any exception (also expected EOFException in case of connection close), to inform the caller
// that the "send proto" intent failed.
// We wrap any exception (also expected EOFException in case of connection close), to leave handling of the exception to the caller.
throw new ConnectionException(exception);
}
}
Expand Down
41 changes: 22 additions & 19 deletions network/network/src/main/java/bisq/network/p2p/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@

import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.*;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Map;
Expand Down Expand Up @@ -311,6 +308,7 @@ public Connection send(EnvelopePayloadMessage envelopePayloadMessage, Address ad

public Connection send(EnvelopePayloadMessage envelopePayloadMessage, Connection connection) {
if (connection.isStopped()) {
log.debug("Send message failed as connection is already stopped {}", this);
throw new ConnectionClosedException(connection);
}
try {
Expand All @@ -322,6 +320,7 @@ public Connection send(EnvelopePayloadMessage envelopePayloadMessage, Connection
} catch (Throwable throwable) {
if (connection.isRunning()) {
handleException(connection, throwable);
log.debug("Send message failed on {}", this, throwable);
closeConnection(connection, CloseReason.EXCEPTION.exception(throwable));
}
throw new ConnectionClosedException(connection);
Expand Down Expand Up @@ -349,15 +348,17 @@ public Connection getConnection(Address address) {
///////////////////////////////////////////////////////////////////////////////////////////////////

private Connection createOutboundConnection(Address address) {
return myCapability.map(capability -> createOutboundConnection(address, capability)).orElseGet(() -> {
int port = networkId.getAddressByTransportTypeMap().get(transportType).getPort();
log.warn("We create an outbound connection but we have not initialized our server. " +
"We create a server on port {} now but clients better control node " +
"life cycle themselves.", port);
initialize();
checkArgument(myCapability.isPresent(), "myCapability must be present after initializeServer got called");
return createOutboundConnection(address, myCapability.get());
});
log.debug("Create outbound connection to {}", address);
return myCapability.map(capability -> createOutboundConnection(address, capability))
.orElseGet(() -> {
int port = networkId.getAddressByTransportTypeMap().get(transportType).getPort();
log.warn("We create an outbound connection but we have not initialized our server. " +
"We create a server on port {} now but clients better control node " +
"life cycle themselves.", port);
initialize();
checkArgument(myCapability.isPresent(), "myCapability must be present after initializeServer got called");
return createOutboundConnection(address, myCapability.get());
});
}

private Connection createOutboundConnection(Address address, Capability myCapability) {
Expand Down Expand Up @@ -640,17 +641,19 @@ private void handleException(Throwable exception) {
if (isShutdown()) {
return;
}
String msg = "Exception: ";
if (exception instanceof EOFException) {
log.debug(exception.toString(), exception);
log.info(msg, exception);
} else if (exception instanceof ConnectException) {
log.debug(msg, exception);
} else if (exception instanceof SocketException) {
log.debug(exception.toString(), exception);
log.debug(msg, exception);
} else if (exception instanceof UnknownHostException) {
log.warn("UnknownHostException. Might happen if we try to connect to wrong network type");
log.warn(exception.toString(), exception);
log.warn("UnknownHostException. Might happen if we try to connect to wrong network type.", exception);
} else if (exception instanceof SocketTimeoutException) {
log.warn(exception.toString(), exception);
log.info(msg, exception);
} else {
log.error(exception.toString(), exception);
log.error(msg, exception);
}
}

Expand Down

0 comments on commit f811f28

Please sign in to comment.