Connections are now severed after a configurable time (12h by default) or when a limit is exceeded (150 by default)
This commit is contained in:
@ -40,6 +40,7 @@ import static ch.dissem.bitmessage.entity.Plaintext.Status.*;
|
||||
import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST;
|
||||
import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
|
||||
import static ch.dissem.bitmessage.utils.UnixTime.DAY;
|
||||
import static ch.dissem.bitmessage.utils.UnixTime.HOUR;
|
||||
|
||||
/**
|
||||
* <p>Use this class if you want to create a Bitmessage client.</p>
|
||||
@ -295,6 +296,8 @@ public class BitmessageContext {
|
||||
Security security;
|
||||
MessageCallback messageCallback;
|
||||
Listener listener;
|
||||
int connectionLimit = 150;
|
||||
long connectionTTL = 12 * HOUR;
|
||||
|
||||
public Builder() {
|
||||
}
|
||||
@ -349,6 +352,16 @@ public class BitmessageContext {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder connectionLimit(int connectionLimit) {
|
||||
this.connectionLimit = connectionLimit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder connectionTTL(int hours) {
|
||||
this.connectionTTL = hours * HOUR;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BitmessageContext build() {
|
||||
nonNull("inventory", inventory);
|
||||
nonNull("nodeRegistry", nodeRegistry);
|
||||
|
@ -56,6 +56,8 @@ public class InternalContext {
|
||||
private final long clientNonce;
|
||||
private final long networkNonceTrialsPerByte = 1000;
|
||||
private final long networkExtraBytes = 1000;
|
||||
private long connectionTTL;
|
||||
private int connectionLimit;
|
||||
|
||||
public InternalContext(BitmessageContext.Builder builder) {
|
||||
this.security = builder.security;
|
||||
@ -68,6 +70,8 @@ public class InternalContext {
|
||||
this.clientNonce = security.randomNonce();
|
||||
this.messageCallback = builder.messageCallback;
|
||||
this.port = builder.port;
|
||||
this.connectionLimit = builder.connectionLimit;
|
||||
this.connectionTTL = builder.connectionTTL;
|
||||
|
||||
Singleton.initialize(security);
|
||||
|
||||
@ -166,7 +170,7 @@ public class InternalContext {
|
||||
.payload(payload)
|
||||
.build();
|
||||
if (object.isSigned()) {
|
||||
object.sign( from.getPrivateKey());
|
||||
object.sign(from.getPrivateKey());
|
||||
}
|
||||
if (payload instanceof Broadcast) {
|
||||
((Broadcast) payload).encrypt();
|
||||
@ -232,6 +236,14 @@ public class InternalContext {
|
||||
return clientNonce;
|
||||
}
|
||||
|
||||
public long getConnectionTTL() {
|
||||
return connectionTTL;
|
||||
}
|
||||
|
||||
public int getConnectionLimit() {
|
||||
return connectionLimit;
|
||||
}
|
||||
|
||||
public interface ContextHolder {
|
||||
void setContext(InternalContext context);
|
||||
}
|
||||
|
@ -25,7 +25,11 @@ public class Singleton {
|
||||
private static Security security;
|
||||
|
||||
public static void initialize(Security security) {
|
||||
Singleton.security = security;
|
||||
synchronized (Singleton.class) {
|
||||
if (Singleton.security == null) {
|
||||
Singleton.security = security;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Security security() {
|
||||
|
Reference in New Issue
Block a user