forked from I2P_Developers/i2p.i2p
i2psnark:
- Auto-reduce tunnel quantity based on peer count - Increase max tunnels
This commit is contained in:
@@ -29,6 +29,9 @@ class IdleChecker extends SimpleTimer2.TimedEvent {
|
|||||||
private int _consec;
|
private int _consec;
|
||||||
private int _consecNotRunning;
|
private int _consecNotRunning;
|
||||||
private boolean _isIdle;
|
private boolean _isIdle;
|
||||||
|
private String _lastIn = "3";
|
||||||
|
private String _lastOut = "3";
|
||||||
|
private final Object _lock = new Object();
|
||||||
|
|
||||||
private static final long CHECK_TIME = 63*1000;
|
private static final long CHECK_TIME = 63*1000;
|
||||||
private static final int MAX_CONSEC_IDLE = 4;
|
private static final int MAX_CONSEC_IDLE = 4;
|
||||||
@@ -46,16 +49,19 @@ class IdleChecker extends SimpleTimer2.TimedEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void timeReached() {
|
public void timeReached() {
|
||||||
|
synchronized (_lock) {
|
||||||
|
locked_timeReached();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void locked_timeReached() {
|
||||||
if (_util.connected()) {
|
if (_util.connected()) {
|
||||||
boolean torrentRunning = false;
|
boolean torrentRunning = false;
|
||||||
boolean hasPeers = false;
|
int peerCount = 0;
|
||||||
for (PeerCoordinator pc : _pcs) {
|
for (PeerCoordinator pc : _pcs) {
|
||||||
if (!pc.halted()) {
|
if (!pc.halted()) {
|
||||||
torrentRunning = true;
|
torrentRunning = true;
|
||||||
if (pc.getPeers() > 0) {
|
peerCount += pc.getPeers();
|
||||||
hasPeers = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,19 +79,22 @@ class IdleChecker extends SimpleTimer2.TimedEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasPeers) {
|
if (peerCount > 0) {
|
||||||
if (_isIdle)
|
restoreTunnels(peerCount);
|
||||||
restoreTunnels();
|
|
||||||
} else {
|
} else {
|
||||||
if (!_isIdle) {
|
if (!_isIdle) {
|
||||||
if (_consec++ >= MAX_CONSEC_IDLE)
|
if (_consec++ >= MAX_CONSEC_IDLE)
|
||||||
reduceTunnels();
|
reduceTunnels();
|
||||||
|
else
|
||||||
|
restoreTunnels(1); // pretend we have one peer for now
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_isIdle = false;
|
_isIdle = false;
|
||||||
_consec = 0;
|
_consec = 0;
|
||||||
_consecNotRunning = 0;
|
_consecNotRunning = 0;
|
||||||
|
_lastIn = "3";
|
||||||
|
_lastOut = "3";
|
||||||
}
|
}
|
||||||
schedule(CHECK_TIME);
|
schedule(CHECK_TIME);
|
||||||
}
|
}
|
||||||
@@ -101,12 +110,13 @@ class IdleChecker extends SimpleTimer2.TimedEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Restore tunnel count
|
* Restore or adjust tunnel count based on current peer count
|
||||||
|
* @param peerCount greater than zero
|
||||||
*/
|
*/
|
||||||
private void restoreTunnels() {
|
private void restoreTunnels(int peerCount) {
|
||||||
_isIdle = false;
|
if (_isIdle && _log.shouldLog(Log.INFO))
|
||||||
if (_log.shouldLog(Log.INFO))
|
|
||||||
_log.info("Restoring tunnels on activity");
|
_log.info("Restoring tunnels on activity");
|
||||||
|
_isIdle = false;
|
||||||
Map<String, String> opts = _util.getI2CPOptions();
|
Map<String, String> opts = _util.getI2CPOptions();
|
||||||
String i = opts.get("inbound.quantity");
|
String i = opts.get("inbound.quantity");
|
||||||
if (i == null)
|
if (i == null)
|
||||||
@@ -120,6 +130,29 @@ class IdleChecker extends SimpleTimer2.TimedEvent {
|
|||||||
String ob= opts.get("outbound.backupQuantity");
|
String ob= opts.get("outbound.backupQuantity");
|
||||||
if (ob == null)
|
if (ob == null)
|
||||||
ob = "0";
|
ob = "0";
|
||||||
|
// we don't need more tunnels than we have peers, reduce if so
|
||||||
|
// reduce to max(peerCount / 2, 2)
|
||||||
|
int in, out;
|
||||||
|
try {
|
||||||
|
in = Integer.parseInt(i);
|
||||||
|
} catch (NumberFormatException nfe) {
|
||||||
|
in = 3;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
out = Integer.parseInt(o);
|
||||||
|
} catch (NumberFormatException nfe) {
|
||||||
|
out = 3;
|
||||||
|
}
|
||||||
|
int target = Math.max(peerCount / 2, 2);
|
||||||
|
if (target < in && in > 2) {
|
||||||
|
in = target;
|
||||||
|
i = Integer.toString(in);
|
||||||
|
}
|
||||||
|
if (target < out && out > 2) {
|
||||||
|
out = target;
|
||||||
|
o = Integer.toString(out);
|
||||||
|
}
|
||||||
|
if (!(_lastIn.equals(i) && _lastOut.equals(o)))
|
||||||
setTunnels(i, o, ib, ob);
|
setTunnels(i, o, ib, ob);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,12 +165,16 @@ class IdleChecker extends SimpleTimer2.TimedEvent {
|
|||||||
if (mgr != null) {
|
if (mgr != null) {
|
||||||
I2PSession sess = mgr.getSession();
|
I2PSession sess = mgr.getSession();
|
||||||
if (sess != null) {
|
if (sess != null) {
|
||||||
|
if (_log.shouldLog(Log.INFO))
|
||||||
|
_log.info("New tunnel settings " + i + " / " + o + " / " + ib + " / " + ob);
|
||||||
Properties newProps = new Properties();
|
Properties newProps = new Properties();
|
||||||
newProps.setProperty("inbound.quantity", i);
|
newProps.setProperty("inbound.quantity", i);
|
||||||
newProps.setProperty("outbound.quantity", o);
|
newProps.setProperty("outbound.quantity", o);
|
||||||
newProps.setProperty("inbound.backupQuantity", ib);
|
newProps.setProperty("inbound.backupQuantity", ib);
|
||||||
newProps.setProperty("outbound.backupQuantity", ob);
|
newProps.setProperty("outbound.backupQuantity", ob);
|
||||||
sess.updateOptions(newProps);
|
sess.updateOptions(newProps);
|
||||||
|
_lastIn = i;
|
||||||
|
_lastOut = o;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2268,13 +2268,13 @@ public class I2PSnarkServlet extends BasicServlet {
|
|||||||
out.write("<tr><td>");
|
out.write("<tr><td>");
|
||||||
out.write(_("Inbound Settings"));
|
out.write(_("Inbound Settings"));
|
||||||
out.write(":<td>");
|
out.write(":<td>");
|
||||||
out.write(renderOptions(1, 6, 3, options.remove("inbound.quantity"), "inbound.quantity", TUNNEL));
|
out.write(renderOptions(1, 8, 3, options.remove("inbound.quantity"), "inbound.quantity", TUNNEL));
|
||||||
out.write(" ");
|
out.write(" ");
|
||||||
out.write(renderOptions(0, 4, 3, options.remove("inbound.length"), "inbound.length", HOP));
|
out.write(renderOptions(0, 4, 3, options.remove("inbound.length"), "inbound.length", HOP));
|
||||||
out.write("<tr><td>");
|
out.write("<tr><td>");
|
||||||
out.write(_("Outbound Settings"));
|
out.write(_("Outbound Settings"));
|
||||||
out.write(":<td>");
|
out.write(":<td>");
|
||||||
out.write(renderOptions(1, 6, 3, options.remove("outbound.quantity"), "outbound.quantity", TUNNEL));
|
out.write(renderOptions(1, 8, 3, options.remove("outbound.quantity"), "outbound.quantity", TUNNEL));
|
||||||
out.write(" ");
|
out.write(" ");
|
||||||
out.write(renderOptions(0, 4, 3, options.remove("outbound.length"), "outbound.length", HOP));
|
out.write(renderOptions(0, 4, 3, options.remove("outbound.length"), "outbound.length", HOP));
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user