Transport: Start first GeoIP lookup when netdb is ready

This commit is contained in:
zzz
2019-05-09 20:11:03 +00:00
parent de8a079cb4
commit c7d052646e
4 changed files with 27 additions and 12 deletions

View File

@ -184,6 +184,12 @@ public abstract class CommSystemFacade implements Service {
*/ */
public DHSessionKeyBuilder.Factory getDHFactory() { return null; } public DHSessionKeyBuilder.Factory getDHFactory() { return null; }
/**
* Router must call after netdb is initialized
* @since 0.9.41
*/
public void initGeoIP() {}
/* /*
* Reachability status codes * Reachability status codes
* *

View File

@ -875,6 +875,7 @@ public class Router implements RouterClock.ClockShiftListener {
// so we probably don't need to throw it to the timer queue, // so we probably don't need to throw it to the timer queue,
// but just to be safe // but just to be safe
_context.simpleTimer2().addEvent(r, 0); _context.simpleTimer2().addEvent(r, 0);
_context.commSystem().initGeoIP();
} }
} }

View File

@ -32,6 +32,7 @@ import net.i2p.util.I2PThread;
import net.i2p.util.Log; import net.i2p.util.Log;
import net.i2p.util.SimpleTimer; import net.i2p.util.SimpleTimer;
import net.i2p.util.SimpleTimer2; import net.i2p.util.SimpleTimer2;
import net.i2p.util.SystemVersion;
import net.i2p.util.Translate; import net.i2p.util.Translate;
public class CommSystemFacadeImpl extends CommSystemFacade { public class CommSystemFacadeImpl extends CommSystemFacade {
@ -55,7 +56,6 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
_netMonitorStatus = true; _netMonitorStatus = true;
_geoIP = new GeoIP(_context); _geoIP = new GeoIP(_context);
_manager = new TransportManager(_context); _manager = new TransportManager(_context);
startGeoIP();
} }
public synchronized void startup() { public synchronized void startup() {
@ -363,8 +363,17 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
* tunnel selection, banlisting, etc. * tunnel selection, banlisting, etc.
*/ */
/**
* Router must call after netdb is initialized
* @since 0.9.41
*/
@Override
public void initGeoIP() {
startGeoIP();
}
/* We hope the routerinfos are read in and things have settled down by now, but it's not required to be so */ /* We hope the routerinfos are read in and things have settled down by now, but it's not required to be so */
private static final int START_DELAY = 5*60*1000; private static final int START_DELAY = SystemVersion.isSlow() ? 5*60*1000 : 5*1000;
private static final int LOOKUP_TIME = 30*60*1000; private static final int LOOKUP_TIME = 30*60*1000;
private void startGeoIP() { private void startGeoIP() {

View File

@ -126,7 +126,7 @@ public class GeoIP {
* Blocking lookup of all pending IPs. * Blocking lookup of all pending IPs.
* Results will be added to the table and available via get() after completion. * Results will be added to the table and available via get() after completion.
*/ */
public void blockingLookup() { void blockingLookup() {
if (! _context.getBooleanPropertyDefaultTrue(PROP_GEOIP_ENABLED)) { if (! _context.getBooleanPropertyDefaultTrue(PROP_GEOIP_ENABLED)) {
_pendingSearch.clear(); _pendingSearch.clear();
_pendingIPv6Search.clear(); _pendingIPv6Search.clear();
@ -153,6 +153,7 @@ public class GeoIP {
return; return;
File geoip2 = getGeoIP2(); File geoip2 = getGeoIP2();
DatabaseReader dbr = null; DatabaseReader dbr = null;
long start = _context.clock().now();
try { try {
// clear the negative cache every few runs, to prevent it from getting too big // clear the negative cache every few runs, to prevent it from getting too big
if (((++_lookupRunCount) % CLEAR) == 0) if (((++_lookupRunCount) % CLEAR) == 0)
@ -295,6 +296,8 @@ public class GeoIP {
if (dbr != null) try { dbr.close(); } catch (IOException ioe) {} if (dbr != null) try { dbr.close(); } catch (IOException ioe) {}
_lock.set(false); _lock.set(false);
} }
if (_log.shouldInfo())
_log.info("GeoIP processing finished, time: " + (_context.clock().now() - start));
} }
} }
@ -423,7 +426,6 @@ public class GeoIP {
} }
String[] rv = new String[search.length]; String[] rv = new String[search.length];
int idx = 0; int idx = 0;
long start = _context.clock().now();
BufferedReader br = null; BufferedReader br = null;
try { try {
String buf = null; String buf = null;
@ -459,9 +461,6 @@ public class GeoIP {
if (br != null) try { br.close(); } catch (IOException ioe) {} if (br != null) try { br.close(); } catch (IOException ioe) {}
} }
if (_log.shouldLog(Log.INFO)) {
_log.info("GeoIP processing finished, time: " + (_context.clock().now() - start));
}
return rv; return rv;
} }
@ -499,7 +498,7 @@ public class GeoIP {
* Add to the list needing lookup * Add to the list needing lookup
* @param ip IPv4 or IPv6 * @param ip IPv4 or IPv6
*/ */
public void add(String ip) { void add(String ip) {
byte[] pib = Addresses.getIP(ip); byte[] pib = Addresses.getIP(ip);
if (pib == null) return; if (pib == null) return;
add(pib); add(pib);
@ -509,7 +508,7 @@ public class GeoIP {
* Add to the list needing lookup * Add to the list needing lookup
* @param ip IPv4 or IPv6 * @param ip IPv4 or IPv6
*/ */
public void add(byte ip[]) { void add(byte ip[]) {
add(toLong(ip)); add(toLong(ip));
} }
@ -529,7 +528,7 @@ public class GeoIP {
* @param ip IPv4 or IPv6 * @param ip IPv4 or IPv6
* @return lower-case code, generally two letters, or null. * @return lower-case code, generally two letters, or null.
*/ */
public String get(String ip) { String get(String ip) {
byte[] pib = Addresses.getIP(ip); byte[] pib = Addresses.getIP(ip);
if (pib == null) return null; if (pib == null) return null;
return get(pib); return get(pib);
@ -540,7 +539,7 @@ public class GeoIP {
* @param ip IPv4 or IPv6 * @param ip IPv4 or IPv6
* @return lower-case code, generally two letters, or null. * @return lower-case code, generally two letters, or null.
*/ */
public String get(byte ip[]) { String get(byte ip[]) {
return get(toLong(ip)); return get(toLong(ip));
} }
@ -597,7 +596,7 @@ public class GeoIP {
* @param code two-letter lower case code * @param code two-letter lower case code
* @return untranslated name or null * @return untranslated name or null
*/ */
public String fullName(String code) { String fullName(String code) {
return _codeToName.get(code); return _codeToName.get(code);
} }