Skip to content

Commit

Permalink
Merge pull request #3214 from christophsturm/optimize-hotspots
Browse files Browse the repository at this point in the history
Optimize some hotspots
  • Loading branch information
sqrrm authored Sep 6, 2019
2 parents b113c7a + ecaf592 commit 118d33a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
13 changes: 8 additions & 5 deletions p2p/src/main/java/bisq/network/p2p/network/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,7 @@ public void run() {

NetworkEnvelope networkEnvelope = networkProtoResolver.fromProto(proto);
lastReadTimeStamp = now;
log.debug("<< Received networkEnvelope of type: " + networkEnvelope.getClass().getSimpleName());

log.debug("<< Received networkEnvelope of type: {}", networkEnvelope.getClass().getSimpleName());
int size = proto.getSerializedSize();
// We comment out that part as only debug and trace log level is used. For debugging purposes
// we leave the code though.
Expand Down Expand Up @@ -761,7 +760,9 @@ public void run() {
boolean exceeds;
if (networkEnvelope instanceof ExtendedDataSizePermission) {
exceeds = size > MAX_PERMITTED_MESSAGE_SIZE;
log.debug("size={}; object={}", size, Utilities.toTruncatedString(proto, 100));
if (log.isDebugEnabled()) {
log.debug("size={}; object={}", size, Utilities.toTruncatedString(proto, 100));
}
} else {
exceeds = size > PERMITTED_MESSAGE_SIZE;
}
Expand Down Expand Up @@ -819,8 +820,10 @@ && reportInvalidRequest(RuleViolation.WRONG_NETWORK_ID)) {

if (networkEnvelope instanceof CloseConnectionMessage) {
// If we get a CloseConnectionMessage we shut down
log.debug("CloseConnectionMessage received. Reason={}\n\t" +
"connection={}", proto.getCloseConnectionMessage().getReason(), this);
if (log.isDebugEnabled()) {
log.debug("CloseConnectionMessage received. Reason={}\n\t" +
"connection={}", proto.getCloseConnectionMessage().getReason(), this);
}
if (CloseConnectionReason.PEER_BANNED.name().equals(proto.getCloseConnectionMessage().getReason())) {
log.warn("We got shut down because we are banned by the other peer. (InputHandler.run CloseConnectionMessage)");
shutDown(CloseConnectionReason.PEER_BANNED);
Expand Down
44 changes: 27 additions & 17 deletions p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public abstract class NetworkNode implements MessageListener {

public SettableFuture<Connection> sendMessage(@NotNull NodeAddress peersNodeAddress,
NetworkEnvelope networkEnvelope) {
log.debug("sendMessage: peersNodeAddress=" + peersNodeAddress + "\n\tmessage=" + Utilities.toTruncatedString(networkEnvelope));
if (log.isDebugEnabled()) {
log.debug("sendMessage: peersNodeAddress=" + peersNodeAddress + "\n\tmessage=" + Utilities.toTruncatedString(networkEnvelope));
}
checkNotNull(peersNodeAddress, "peerAddress must not be null");

Connection connection = getOutboundConnection(peersNodeAddress);
Expand All @@ -123,11 +125,15 @@ public SettableFuture<Connection> sendMessage(@NotNull NodeAddress peersNodeAddr
try {
// can take a while when using tor
long startTs = System.currentTimeMillis();
log.debug("Start create socket to peersNodeAddress {}", peersNodeAddress.getFullAddress());
if (log.isDebugEnabled()) {
log.debug("Start create socket to peersNodeAddress {}", peersNodeAddress.getFullAddress());
}
Socket socket = createSocket(peersNodeAddress);
long duration = System.currentTimeMillis() - startTs;
log.debug("Socket creation to peersNodeAddress {} took {} ms", peersNodeAddress.getFullAddress(),
duration);
if (log.isDebugEnabled()) {
log.debug("Socket creation to peersNodeAddress {} took {} ms", peersNodeAddress.getFullAddress(),
duration);
}

if (duration > CREATE_SOCKET_TIMEOUT)
throw new TimeoutException("A timeout occurred when creating a socket.");
Expand All @@ -139,11 +145,14 @@ public SettableFuture<Connection> sendMessage(@NotNull NodeAddress peersNodeAddr
existingConnection = getOutboundConnection(peersNodeAddress);

if (existingConnection != null) {
log.debug("We found in the meantime a connection for peersNodeAddress {}, " +
"so we use that for sending the message.\n" +
"That can happen if Tor needs long for creating a new outbound connection.\n" +
"We might have got a new inbound or outbound connection.",
peersNodeAddress.getFullAddress());
if (log.isDebugEnabled()) {
log.debug("We found in the meantime a connection for peersNodeAddress {}, " +
"so we use that for sending the message.\n" +
"That can happen if Tor needs long for creating a new outbound connection.\n" +
"We might have got a new inbound or outbound connection.",
peersNodeAddress.getFullAddress());

}
try {
socket.close();
} catch (Throwable throwable) {
Expand Down Expand Up @@ -184,14 +193,15 @@ public void onError(Throwable throwable) {
peersNodeAddress,
networkProtoResolver);

log.debug("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" +
"NetworkNode created new outbound connection:"
+ "\nmyNodeAddress=" + getNodeAddress()
+ "\npeersNodeAddress=" + peersNodeAddress
+ "\nuid=" + outboundConnection.getUid()
+ "\nmessage=" + networkEnvelope
+ "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");

if (log.isDebugEnabled()) {
log.debug("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" +
"NetworkNode created new outbound connection:"
+ "\nmyNodeAddress=" + getNodeAddress()
+ "\npeersNodeAddress=" + peersNodeAddress
+ "\nuid=" + outboundConnection.getUid()
+ "\nmessage=" + networkEnvelope
+ "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
}
// can take a while when using tor
outboundConnection.sendMessage(networkEnvelope);
return outboundConnection;
Expand Down

0 comments on commit 118d33a

Please sign in to comment.