PyBitmessage-2021-04-27/src/knownnodes.py
Peter Surda 339e375958
Bootstrap provider mode and minor knownNodes changes
- if knownNodes grows to 20000, instead of ignoring new nodes, forget
  the 1000 oldest ones
- drop connection after sendaddr if too many connections, even if it's
  an outbound one
- if maximum total connections are lower than maximum outbound
  connections, active bootstrap provider mode
- in this mode, check all addresses received before announcing them
- so basically it only annouces those addresses it successfully
  connected to
2017-02-27 23:31:12 +01:00

26 lines
703 B
Python

import pickle
import threading
import state
knownNodesLock = threading.Lock()
knownNodes = {}
knownNodesMax = 20000
knownNodesTrimAmount = 2000
def saveKnownNodes(dirName = None):
if dirName is None:
dirName = state.appdata
with knownNodesLock:
with open(dirName + 'knownnodes.dat', 'wb') as output:
pickle.dump(knownNodes, output)
def trimKnownNodes(recAddrStream = 1):
if len(knownNodes[recAddrStream]) < knownNodesMax:
return
with knownNodesLock:
oldestList = sorted(knownNodes[recAddrStream], key=knownNodes[recAddrStream].get)[:knownNodeTrimAmount]
for oldest in oldestList:
del knownNodes[recAddrStream][oldest]