Skip to content

Instantly share code, notes, and snippets.

@anacrolix
Last active January 23, 2019 01:06
Show Gist options
  • Save anacrolix/97655f203966c9c27bca9d3994278719 to your computer and use it in GitHub Desktop.
Save anacrolix/97655f203966c9c27bca9d3994278719 to your computer and use it in GitHub Desktop.
# this would be called before adding a connection to the connection manager
# (presumably we just completed dial or accept routines to completion)
def before_adding_conn():
if numconns < highwater:
return
# amortize the cost of evicting conns, drop connection count to the low water mark
for numconns > lowwater:
if not optimistic_evict():
force_eviction()
# see if a subsystem is happy to lose a connection
def optimistic_evict():
for ss in subsystems:
if ss.optimistic_evict():
return True
return False
# evict a connection from the naughtiest subsystem for which only naughtier subsystems have a stake in the chosen connection
def force_evict():
ordered_subsystems = sort(subsystems, key=suitability_for_eviction, reverse=True)
for i, ss in enum(ordered_subsystems):
# connections that are used by less naughty subsystems are not eligible for eviction by a naughtier subsystem.
ineligible_conns = {conn for conn in oss.conns() for oss in ordered_subsystems[i+1:]}
eligible_conns = set(ss.conns()) - ineligible_conns
# if there are no eligible connections, we must let a less naughty subsystem choose
if eligible_conns:
ss.choose_evictee(eligible_conns)
# example implementation:
# determine a fair share of the total available conns for this subsystem,
# and return the difference with its actual usage.
def suitability_for_eviction(subsystem):
return subsystem.num_conns() - total_conns*quota(subsystem)
# subsystems will implement this (Go)
type ConnEvictor interface{
OptimisticEvict() (_ Conn, ok bool)
ChooseEvictee([]Conn) Conn
}
# example for DHT
OptimisticEvict: return conn that's eligible for replacement in routing table
ChooseEvictee: return newest entry in fullest bucket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment