Refactorings, small improvements/fixes

This commit is contained in:
Manfred Karrer 2016-01-31 00:38:03 +01:00
parent 0b5cdc2581
commit 186f85c44e
5 changed files with 36 additions and 42 deletions

View file

@ -177,14 +177,12 @@ public class OpenOfferManager {
if (!shutDownRequested) { if (!shutDownRequested) {
log.debug("shutDown"); log.debug("shutDown");
shutDownRequested = true; shutDownRequested = true;
int numOffers = openOffers.size();
// we remove own offers from offerbook when we go offline // we remove own offers from offerbook when we go offline
for (OpenOffer openOffer : openOffers) { openOffers.forEach(openOffer -> offerBookService.removeOfferAtShutDown(openOffer.getOffer()));
offerBookService.removeOfferAtShutDown(openOffer.getOffer());
}
// delay a bit before we signal that we are done to give time for network
if (completeHandler != null) if (completeHandler != null)
UserThread.runAfter(() -> completeHandler.run(), 500, TimeUnit.MILLISECONDS); UserThread.runAfter(completeHandler::run, numOffers * 200 + 300, TimeUnit.MILLISECONDS);
} }
} }

View file

@ -75,7 +75,7 @@ import static io.bitsquare.app.BitsquareEnvironment.APP_NAME_KEY;
public class BitsquareApp extends Application { public class BitsquareApp extends Application {
private static final Logger log = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(BitsquareApp.class); private static final Logger log = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(BitsquareApp.class);
public static final boolean DEV_MODE = false; public static final boolean DEV_MODE = true;
public static final boolean IS_RELEASE_VERSION = !DEV_MODE && true; public static final boolean IS_RELEASE_VERSION = !DEV_MODE && true;
private static Environment env; private static Environment env;

View file

@ -239,7 +239,7 @@ public class TraderDisputeView extends ActivatableView<VBox, Void> {
tempAttachments.add(new DisputeDirectMessage.Attachment(result.getName(), filesAsBytes)); tempAttachments.add(new DisputeDirectMessage.Attachment(result.getName(), filesAsBytes));
inputTextArea.setText(inputTextArea.getText() + "\n[Attachment " + result.getName() + "]"); inputTextArea.setText(inputTextArea.getText() + "\n[Attachment " + result.getName() + "]");
} else { } else {
new Popup().error("The max. allowed file size is " + maxSizeInKB + " kB.").show(); new Popup().warning("The max. allowed file size is " + maxSizeInKB + " kB.").show();
} }
} catch (java.io.IOException e) { } catch (java.io.IOException e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -23,10 +23,7 @@ import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException; import java.net.SocketException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.util.Date; import java.util.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.*; import java.util.concurrent.*;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
@ -39,8 +36,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
public class Connection implements MessageListener { public class Connection implements MessageListener {
private static final Logger log = LoggerFactory.getLogger(Connection.class); private static final Logger log = LoggerFactory.getLogger(Connection.class);
private static final int MAX_MSG_SIZE = 100 * 1024; // 100 kb of compressed data private static final int MAX_MSG_SIZE = 100 * 1024; // 100 kb of compressed data
private static final int MAX_MSG_PER_SEC = 10; // With MAX_MSG_SIZE of 100kb results in bandwidth of 10 mbit/sec private static final int MSG_THROTTLE_PER_SEC = 10; // With MAX_MSG_SIZE of 100kb results in bandwidth of 10 mbit/sec
private static final int MAX_MSG_PER_10SEC = 50; // With MAX_MSG_SIZE of 100kb results in bandwidth of 5 mbit/sec for 10 sec private static final int MSG_THROTTLE_PER_10SEC = 50; // With MAX_MSG_SIZE of 100kb results in bandwidth of 5 mbit/sec for 10 sec
//timeout on blocking Socket operations like ServerSocket.accept() or SocketInputStream.read() //timeout on blocking Socket operations like ServerSocket.accept() or SocketInputStream.read()
private static final int SOCKET_TIMEOUT = 30 * 60 * 1000; // 30 min. private static final int SOCKET_TIMEOUT = 30 * 60 * 1000; // 30 min.
@ -86,7 +83,7 @@ public class Connection implements MessageListener {
private final boolean useCompression = false; private final boolean useCompression = false;
private PeerType peerType; private PeerType peerType;
private final ObjectProperty<NodeAddress> nodeAddressProperty = new SimpleObjectProperty<>(); private final ObjectProperty<NodeAddress> nodeAddressProperty = new SimpleObjectProperty<>();
private List<Long> messageTimeStamps = new ArrayList<>();
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Constructor // Constructor
@ -204,6 +201,26 @@ public class Connection implements MessageListener {
sharedModel.reportIllegalRequest(illegalRequest); sharedModel.reportIllegalRequest(illegalRequest);
} }
public boolean violatesThrottleLimit() {
long now = System.currentTimeMillis();
boolean violated = false;
if (messageTimeStamps.size() >= MSG_THROTTLE_PER_SEC) {
// check if we got more than 10 msg per sec.
long compareTo = messageTimeStamps.get(messageTimeStamps.size() - MSG_THROTTLE_PER_SEC);
violated = now - compareTo < 1000;
}
if (messageTimeStamps.size() >= MSG_THROTTLE_PER_10SEC) {
// check if we got more than 50 msg per 10 sec.
long compareTo = messageTimeStamps.get(messageTimeStamps.size() - MSG_THROTTLE_PER_10SEC);
violated = violated || now - compareTo < 10000;
// we limit to max 50 entries
messageTimeStamps.remove(0);
}
messageTimeStamps.add(now);
return violated;
}
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// MessageListener implementation // MessageListener implementation
@ -428,7 +445,6 @@ public class Connection implements MessageListener {
private Date lastActivityDate; private Date lastActivityDate;
private volatile boolean stopped; private volatile boolean stopped;
private ConnectionListener.Reason shutDownReason; private ConnectionListener.Reason shutDownReason;
private List<Long> messageTimeStamps = new CopyOnWriteArrayList<>();
public SharedModel(Connection connection, Socket socket) { public SharedModel(Connection connection, Socket socket) {
Log.traceCall(); Log.traceCall();
@ -518,27 +534,6 @@ public class Connection implements MessageListener {
this.stopped = true; this.stopped = true;
} }
private boolean tooManyMessages() {
long now = System.currentTimeMillis();
boolean exceeds = false;
if (messageTimeStamps.size() >= MAX_MSG_PER_SEC) {
// check if we got more than 10 msg per sec.
long compareTo = messageTimeStamps.get(messageTimeStamps.size() - MAX_MSG_PER_SEC);
exceeds = now - compareTo < 1000;
}
if (messageTimeStamps.size() >= MAX_MSG_PER_10SEC) {
// check if we got more than 50 msg per 10 sec.
long compareTo = messageTimeStamps.get(messageTimeStamps.size() - MAX_MSG_PER_10SEC);
exceeds = exceeds || now - compareTo < 10000;
// we limit to max 50 entries
messageTimeStamps.remove(0);
}
messageTimeStamps.add(now);
return exceeds;
}
public synchronized ConnectionListener.Reason getShutDownReason() { public synchronized ConnectionListener.Reason getShutDownReason() {
return shutDownReason; return shutDownReason;
} }
@ -635,6 +630,12 @@ public class Connection implements MessageListener {
sharedModel.reportIllegalRequest(IllegalRequest.MaxSizeExceeded); sharedModel.reportIllegalRequest(IllegalRequest.MaxSizeExceeded);
return; return;
} }
if (sharedModel.connection.violatesThrottleLimit()) {
sharedModel.reportIllegalRequest(IllegalRequest.ViolatedThrottleLimit);
return;
}
if (!(serializable instanceof Message)) { if (!(serializable instanceof Message)) {
sharedModel.reportIllegalRequest(IllegalRequest.InvalidDataType); sharedModel.reportIllegalRequest(IllegalRequest.InvalidDataType);
return; return;
@ -646,11 +647,6 @@ public class Connection implements MessageListener {
return; return;
} }
if (sharedModel.tooManyMessages()) {
sharedModel.reportIllegalRequest(IllegalRequest.TooManyMessages);
return;
}
Connection connection = sharedModel.connection; Connection connection = sharedModel.connection;
sharedModel.updateLastActivityDate(); sharedModel.updateLastActivityDate();
if (message instanceof CloseConnectionMessage) { if (message instanceof CloseConnectionMessage) {

View file

@ -4,7 +4,7 @@ public enum IllegalRequest {
MaxSizeExceeded(1), MaxSizeExceeded(1),
InvalidDataType(0), InvalidDataType(0),
WrongNetworkId(0), WrongNetworkId(0),
TooManyMessages(1); ViolatedThrottleLimit(1);
public final int maxTolerance; public final int maxTolerance;