forked from I2P_Developers/i2p.i2p
* I2CP: Cache b32 lookups client-side
* I2PTunnelHTTPClient: Use existing session for b32 lookups rather than a new SimpleSession * Naming: Increase b32 lookup timeout to 15 sec.
This commit is contained in:
@ -18,6 +18,7 @@ import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
@ -140,6 +141,11 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
|
||||
private long _lastActivity;
|
||||
private boolean _isReduced;
|
||||
|
||||
/**
|
||||
* @since 0.8.9
|
||||
*/
|
||||
private static final LookupCache _lookupCache = new LookupCache(16);
|
||||
|
||||
/** SSL interface (only) @since 0.8.3 */
|
||||
protected static final String PROP_ENABLE_SSL = "i2cp.SSL";
|
||||
|
||||
@ -809,6 +815,9 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
|
||||
/** called by the message handler */
|
||||
void destReceived(Destination d) {
|
||||
Hash h = d.calculateHash();
|
||||
synchronized (_lookupCache) {
|
||||
_lookupCache.put(h, d);
|
||||
}
|
||||
for (LookupWaiter w : _pendingLookups) {
|
||||
if (w.hash.equals(h)) {
|
||||
w.destination = d;
|
||||
@ -872,6 +881,11 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
|
||||
* @return null on failure
|
||||
*/
|
||||
public Destination lookupDest(Hash h, long maxWait) throws I2PSessionException {
|
||||
synchronized (_lookupCache) {
|
||||
Destination rv = _lookupCache.get(h);
|
||||
if (rv != null)
|
||||
return rv;
|
||||
}
|
||||
if (_closed)
|
||||
return null;
|
||||
LookupWaiter waiter = new LookupWaiter(h);
|
||||
@ -952,4 +966,21 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
|
||||
buf.append(getPrefix());
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.8.9
|
||||
*/
|
||||
private static class LookupCache extends LinkedHashMap<Hash, Destination> {
|
||||
private final int _max;
|
||||
|
||||
public LookupCache(int max) {
|
||||
super(max, 0.75f, true);
|
||||
_max = max;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<Hash, Destination> eldest) {
|
||||
return size() > _max;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,8 @@ import net.i2p.data.Hash;
|
||||
* Obviously this can take a while.
|
||||
*
|
||||
* All calls are blocking and return null on failure.
|
||||
* Timeout is set to 10 seconds in I2PSimpleSession.
|
||||
* Timeout is 15 seconds.
|
||||
* To do: Add methods that allow specifying the timeout.
|
||||
*
|
||||
* As of 0.8.3, standard I2PSessions support lookups,
|
||||
* including multiple lookups in parallel, and overriding
|
||||
@ -32,6 +33,8 @@ import net.i2p.data.Hash;
|
||||
*/
|
||||
class LookupDest {
|
||||
|
||||
private static final long DEFAULT_TIMEOUT = 15*1000;
|
||||
|
||||
protected LookupDest(I2PAppContext context) {}
|
||||
|
||||
/** @param key 52 chars (do not include the .b32.i2p suffix) */
|
||||
@ -67,7 +70,7 @@ class LookupDest {
|
||||
opts.put(I2PClient.PROP_TCP_PORT, s);
|
||||
I2PSession session = client.createSession(null, opts);
|
||||
session.connect();
|
||||
rv = session.lookupDest(key);
|
||||
rv = session.lookupDest(key, DEFAULT_TIMEOUT);
|
||||
session.destroySession();
|
||||
} catch (I2PSessionException ise) {}
|
||||
return rv;
|
||||
|
Reference in New Issue
Block a user