public class TunnelPool extends Object
| Modifier and Type | Field and Description |
|---|---|
protected boolean | _aliveWhether this pool is running |
protected RouterContext | _contextThe router context |
protected Log | _logThe log |
(package private) static long | DEFAULT_PRUNE_EARLY_EXPIRYDefault early expiration time for pruned tunnels (30 seconds) |
(package private) static Comparator<TunnelInfo> | QUALITY_COMPARATORBest tunnels first for LeaseSet publication: freshest (longest remaining
life) so the lease survives floodfill propagation, then lowest average
latency, then fewest consecutive failures. |
| Constructor and Description |
|---|
TunnelPool(RouterContext ctx,
TunnelPoolManager mgr,
TunnelPoolSettings settings,
TunnelPeerSelector sel)Tunnel pool |
| Modifier and Type | Method and Description |
|---|---|
protected void | addTunnel(TunnelInfo info)Add a tunnel to the pool. |
(package private) void | buildComplete(PooledTunnelCreatorConfig cfg,
BuildExecutor.Result result)Remove from the _inprogress list and call addTunnel() if result is SUCCESS. |
(package private) boolean | buildFallback()This will build a fallback (zero-hop) tunnel ONLY if
this pool is exploratory, or the settings allow it. |
List<PooledTunnelCreatorConfig> | cancelExcessInProgress(int maxAllowed)Cancel excess in-progress tunnel builds to stay within budget. |
void | clearFailingOnTraffic()Clear the FAILING flag on inbound tunnels that have provably carried
real traffic recently. |
(package private) PooledTunnelCreatorConfig | configureNewTunnel()This only sets the peers and creation/expiration times in the configuration. |
(package private) void | ensureSufficientTunnels()Ensure the pool has at least target valid tunnels, building replacements
proactively when the count drops below target. |
static String | formatPoolIdentity(TunnelPoolSettings settings)Format a pool identity for log messages, combining nickname and truncated hash. |
int | getActiveTunnelCount()Count GOOD and TESTING (non-failed, not expired) tunnels in the pool. |
int | getAvgBWPerTunnel()Average bandwidth per tunnel in the pool. |
(package private) static int | getBuildTriesQuantityOverride(RouterContext ctx)Quantity override threshold from config or default (12). |
(package private) int | getConsecutiveBuildTimeouts()Count of consecutive tunnel build timeouts. |
(package private) int | getEffectiveTarget()The tunnel count the pool maintains, never less than 2 per direction
regardless of the configured quantity, unless the pool is a ping pool
or expressly zero-hop. |
LeaseSet | getInboundTunnelsAsLeaseSet()Build and return current LeaseSet from our tunnels. |
int | getInProgressCount()Count tunnels currently being built. |
(package private) static long | getLeaseMaxDuration(RouterContext ctx)Maximum lease set lease duration from config or default (10 minutes). |
(package private) static long | getLeaseSetBuildMinInterval(RouterContext ctx)LeaseSet build minimum interval from config or default (2 minutes). |
static long | getLeaseViabilityWindow(RouterContext ctx)A lease counts toward a re-mint while it has at least this much
remaining: three minutes covers the re-sign request, floodfill
propagation, and initial connection establishment, and a tunnel closer
to expiry than this is already inside the pool's proactive replacement
window (BuildExecutor starts replacing GOOD tunnels 330s out), so
counting it would overstate the pool's health. |
long | getLifetimeProcessed()Total lifetime processed bytes for this pool. |
(package private) static int | getMaxConcurrentBuildsPerDirection(RouterContext ctx)Max concurrent builds per direction from config or default (6). |
(package private) TunnelPool | getPairedPool()Paired pool (inbound <-> outbound for same destination). |
(package private) long | getPruneEarlyExpiry()Early expiry time for pruned tunnels. |
String | getRateName()RateStat name for the bandwidth graph |
(package private) static long | getRefreshThrottle(RouterContext ctx)Refresh throttle interval from config or default (2 minutes). |
TunnelPoolSettings | getSettings()Pool settings. |
(package private) static long | getStartupTime(RouterContext ctx)Startup suppression period from config or default (5 minutes). |
int | getTestingTunnelCount()Count tunnels that have been built but not yet passed their first test. |
TunnelInfo | getTunnel(TunnelId gatewayId)Tunnel by its gateway tunnel ID. |
(package private) static int | getTunnelAvgLatency(TunnelInfo t)Average test latency for a tunnel. |
static int | getTunnelLifetime(RouterContext ctx)Tunnel lifetime from config or default (11 minutes). |
TunnelPoolManager | getTunnelPoolManager()TunnelPoolManager that owns this pool. |
(package private) int | getUsableTunnelCount()Count tunnels that are usable for routing — not failed, not expired,
not expiring within 5 minutes. |
int | getValidTunnelCount()Count valid (non-failed, not expired) tunnels in the pool. |
(package private) void | incrementBuildTimeout()Increment consecutive build timeout counter. |
boolean | isAlive()Is this pool running AND either exploratory, or tracked by the client manager?
A pool will be alive but not tracked after the client manager removes it
but before all the tunnels have expired. |
(package private) boolean | isExpiringSoon(long now)Check if the LeaseSet is expiring soon (within 1 minute). |
boolean | isStruggling()Whether this pool is struggling to meet its tunnel targets. |
List<PooledTunnelCreatorConfig> | listPending()List of tunnelInfo instances of tunnels currently being built. |
List<TunnelInfo> | listTunnels()List of tunnels currently in the pool. |
protected LeaseSet | locked_buildNewLeaseSet()Build a leaseSet with the required tunnels that aren't about to expire. |
(package private) static long | maxExpirationStagger(long lifetimeMs)Upper bound for the expiration stagger: keeps lifetime plus stagger
under the NetDb's 15-minute future-lease rejection boundary with a
15-second margin, and never exceeds 4 minutes. |
(package private) boolean | needFallback()Do we really need more fallbacks?
Used to prevent a zillion of them. |
(package private) void | notifyServerPoolTestFailed()Called by TestJob when a server pool tunnel fails a test but is retained. |
(package private) static boolean | passesScanGates(TunnelInfo info,
long now,
boolean longTunnelsOnly)Whether a pooled tunnel passes the scan eligibility gates: not
failed, not over the consecutive-test-failure cap (test failures are
often reply-path problems, not tunnel quality), non-expired, and —
on the first pass — longer than a single hop. |
(package private) void | proactiveRepublishIfHealthy()Proactively republish the LeaseSet when all tunnels are healthy (all > 5 min expiry). |
int | pruneExcessTunnels()Prune excess tunnels from the pool to stay within budget. |
(package private) void | refreshLeaseSet()No-op for outbound and exploratory pools. |
(package private) void | refreshLeaseSet(boolean force)Refresh the LeaseSet, throttled to prevent flooding but not on initial creation. |
(package private) void | removeInProgress(PooledTunnelCreatorConfig cfg)Remove a config that was never actually sent to the network (e.g. |
(package private) void | removeTunnel(TunnelInfo info)Remove a tunnel from the pool. |
(package private) boolean | removeTunnelSynchronous(TunnelInfo info)Synchronous tunnel removal for use during recovery or critical situations. |
void | requestFreshTunnelBuild()Request new inbound tunnels so the pool holds the target count with at
least getLeaseViabilityWindow(RouterContext) remaining. |
(package private) TunnelInfo | selectTunnel()Pull a random tunnel out of the pool. |
(package private) TunnelInfo | selectTunnel(Hash closestTo)Return the tunnel from the pool that is XOR-closest to the target. |
(package private) void | setPairedPool(TunnelPool pool)Paired pool (inbound <-> outbound for same destination). |
(package private) void | setSettings(TunnelPoolSettings settings)Update the settings for this pool |
(package private) static boolean | shouldWarnSelectPeersFailure(long uptime,
long now,
long lastWarnTime)The selectPeers-failure WARN is rate-limited: never before
SELECT_PEERS_FAILURE_SILENCE_MS of uptime, then at most
once per SELECT_PEERS_FAILURE_WARN_INTERVAL_MS per pool. |
(package private) void | shutdown()Shut down the pool and clean up resources. |
int | size() |
(package private) void | startup()Warning, this may be called more than once
(without an intervening shutdown()) if the
tunnel is stopped and then restarted by the client manager with the same
Destination (i.e. |
String | toString()Description of the pool and its settings. |
(package private) void | tunnelFailed(TunnelInfo cfg)Remove tunnel and blame all peers (not necessarily equally). |
(package private) void | tunnelFailed(TunnelInfo cfg,
Hash blamePeer)Remove the tunnel and blame only one peer. |
protected volatile boolean _alive
protected final RouterContext _context
protected final Log _log
static final long DEFAULT_PRUNE_EARLY_EXPIRY
static final Comparator<TunnelInfo> QUALITY_COMPARATOR
TunnelPool(RouterContext ctx, TunnelPoolManager mgr, TunnelPoolSettings settings, TunnelPeerSelector sel)
protected void addTunnel(TunnelInfo info)
info - the tunnel to addvoid buildComplete(PooledTunnelCreatorConfig cfg, BuildExecutor.Result result)
cfg - the completed tunnel configurationresult - the build resultboolean buildFallback()
public List<PooledTunnelCreatorConfig> cancelExcessInProgress(int maxAllowed)
maxAllowed - the maximum number of in-progress builds allowedpublic void clearFailingOnTraffic()
PooledTunnelCreatorConfig configureNewTunnel()
void ensureSufficientTunnels()
public static String formatPoolIdentity(TunnelPoolSettings settings)
settings - the pool settingspublic int getActiveTunnelCount()
public int getAvgBWPerTunnel()
static int getBuildTriesQuantityOverride(RouterContext ctx)
ctx - the router contextint getConsecutiveBuildTimeouts()
int getEffectiveTarget()
public LeaseSet getInboundTunnelsAsLeaseSet()
public int getInProgressCount()
static long getLeaseMaxDuration(RouterContext ctx)
ctx - the router contextstatic long getLeaseSetBuildMinInterval(RouterContext ctx)
ctx - the router contextpublic static long getLeaseViabilityWindow(RouterContext ctx)
ctx - the router contextpublic long getLifetimeProcessed()
static int getMaxConcurrentBuildsPerDirection(RouterContext ctx)
ctx - the router contextTunnelPool getPairedPool()
long getPruneEarlyExpiry()
public String getRateName()
static long getRefreshThrottle(RouterContext ctx)
ctx - the router contextpublic TunnelPoolSettings getSettings()
static long getStartupTime(RouterContext ctx)
ctx - the router contextpublic int getTestingTunnelCount()
public TunnelInfo getTunnel(TunnelId gatewayId)
gatewayId - for inbound, the GW rcv tunnel ID; for outbound, the GW send tunnel ID.static int getTunnelAvgLatency(TunnelInfo t)
t - the tunnel to querypublic static int getTunnelLifetime(RouterContext ctx)
ctx - the router contextpublic TunnelPoolManager getTunnelPoolManager()
int getUsableTunnelCount()
public int getValidTunnelCount()
void incrementBuildTimeout()
public boolean isAlive()
boolean isExpiringSoon(long now)
now - current timepublic boolean isStruggling()
public List<PooledTunnelCreatorConfig> listPending()
public List<TunnelInfo> listTunnels()
protected LeaseSet locked_buildNewLeaseSet()
static long maxExpirationStagger(long lifetimeMs)
lifetimeMs - the configured tunnel lifetime in msboolean needFallback()
void notifyServerPoolTestFailed()
static boolean passesScanGates(TunnelInfo info, long now, boolean longTunnelsOnly)
info - the candidate tunnelnow - current time in mslongTunnelsOnly - first-pass mode (zero-hop-avoiding pools): long tunnels onlyvoid proactiveRepublishIfHealthy()
public int pruneExcessTunnels()
void refreshLeaseSet()
void refreshLeaseSet(boolean force)
force - if true, bypass throttle (for critical refresh when below minimum or near expiry)void removeInProgress(PooledTunnelCreatorConfig cfg)
cfg - the config to remove from the in-progress listvoid removeTunnel(TunnelInfo info)
info - the tunnel to removeboolean removeTunnelSynchronous(TunnelInfo info)
info - tunnel to removepublic void requestFreshTunnelBuild()
getLeaseViabilityWindow(RouterContext) remaining.
Called by
the LeaseSet republisher before a re-mint: the normal build logic
replaces tunnels only when below target or inside its proactive
replacement window (BuildExecutor starts 330s out), so a pool at
target with aging leases never rebuilds on its own, and a re-mint
would re-sign the same near-expired leases.
Respects the in-progress cap, the per-period dedup guard and pool
backoff to avoid build storms.TunnelInfo selectTunnel()
TunnelInfo selectTunnel(Hash closestTo)
closestTo - the hash to find the closest tunnel tovoid setPairedPool(TunnelPool pool)
pool - the paired poolvoid setSettings(TunnelPoolSettings settings)
settings - the new settings, may be nullstatic boolean shouldWarnSelectPeersFailure(long uptime,
long now,
long lastWarnTime)SELECT_PEERS_FAILURE_SILENCE_MS of uptime, then at most
once per SELECT_PEERS_FAILURE_WARN_INTERVAL_MS per pool.uptime - the router uptime in msnow - the current time in mslastWarnTime - the last WARN time in msvoid shutdown()
public int size()
void startup()
public String toString()
void tunnelFailed(TunnelInfo cfg)
cfg - the tunnel that failedvoid tunnelFailed(TunnelInfo cfg, Hash blamePeer)
cfg - the tunnel that failedblamePeer - the peer to blame