Rev932, Skip news event from future, Disable siteSetOwned and setAutodownloadoptional when multiuser plugin enabled, Fix sidebar double click handlers, Log peer sent commands, Send modification to less peer if have enought peers, Close peers if more than 10 per site
This commit is contained in:
parent
5b821c46e5
commit
3f6f273fb1
7 changed files with 78 additions and 28 deletions
|
@ -345,7 +345,7 @@ class Site(object):
|
|||
|
||||
if result and "ok" in result:
|
||||
published.append(peer)
|
||||
self.log.info("[OK] %s: %s" % (peer.key, result["ok"]))
|
||||
self.log.info("[OK] %s: %s %s/%s" % (peer.key, result["ok"], len(published), limit))
|
||||
else:
|
||||
if result == {"exception": "Timeout"}:
|
||||
peer.onConnectionError()
|
||||
|
@ -353,21 +353,29 @@ class Site(object):
|
|||
|
||||
# Update content.json on peers
|
||||
@util.Noparallel()
|
||||
def publish(self, limit=5, inner_path="content.json"):
|
||||
def publish(self, limit="default", inner_path="content.json"):
|
||||
published = [] # Successfully published (Peer)
|
||||
publishers = [] # Publisher threads
|
||||
|
||||
if not self.peers:
|
||||
self.announce()
|
||||
|
||||
threads = 5
|
||||
if limit == "default":
|
||||
if len(self.peers) > 50:
|
||||
limit = 3
|
||||
threads = 3
|
||||
else:
|
||||
limit = 5
|
||||
|
||||
connected_peers = self.getConnectedPeers()
|
||||
if len(connected_peers) > limit * 2: # Publish to already connected peers if possible
|
||||
peers = connected_peers
|
||||
else:
|
||||
peers = self.peers.values()
|
||||
|
||||
self.log.info("Publishing to %s/%s peers (connected: %s)..." % (
|
||||
min(len(self.peers), limit), len(self.peers), len(connected_peers)
|
||||
self.log.info("Publishing %s to %s/%s peers (connected: %s)..." % (
|
||||
inner_path, limit, len(self.peers), len(connected_peers)
|
||||
))
|
||||
|
||||
if not peers:
|
||||
|
@ -375,7 +383,7 @@ class Site(object):
|
|||
|
||||
random.shuffle(peers)
|
||||
event_done = gevent.event.AsyncResult()
|
||||
for i in range(min(len(self.peers), limit, 5)): # Max 5 thread
|
||||
for i in range(min(len(self.peers), limit, threads)):
|
||||
publisher = gevent.spawn(self.publisher, inner_path, peers, published, limit, event_done)
|
||||
publishers.append(publisher)
|
||||
|
||||
|
@ -392,12 +400,12 @@ class Site(object):
|
|||
] # Every connected passive peer that we not published to
|
||||
|
||||
self.log.info(
|
||||
"Successfuly published to %s peers, publishing to %s more passive peers" %
|
||||
(len(published), len(passive_peers))
|
||||
)
|
||||
"Successfuly %s published to %s peers, publishing to %s more passive peers" % (
|
||||
inner_path, len(published), len(passive_peers)
|
||||
))
|
||||
|
||||
for peer in passive_peers:
|
||||
gevent.spawn(self.publisher, inner_path, passive_peers, published, limit=10)
|
||||
gevent.spawn(self.publisher, inner_path, passive_peers, published, limit=limit+3)
|
||||
|
||||
# Send my hashfield to every connected peer if changed
|
||||
gevent.spawn(self.sendMyHashfield, 100)
|
||||
|
@ -765,11 +773,13 @@ class Site(object):
|
|||
def getConnectedPeers(self):
|
||||
return [peer for peer in self.peers.values() if peer.connection and peer.connection.connected]
|
||||
|
||||
# Cleanup probably dead peers
|
||||
# Cleanup probably dead peers and close connection if too much
|
||||
def cleanupPeers(self):
|
||||
peers = self.peers.values()
|
||||
if len(peers) < 20:
|
||||
return False
|
||||
|
||||
# Cleanup old peers
|
||||
removed = 0
|
||||
|
||||
for peer in peers:
|
||||
|
@ -786,6 +796,30 @@ class Site(object):
|
|||
if removed:
|
||||
self.log.debug("Cleanup peers result: Removed %s, left: %s" % (removed, len(self.peers)))
|
||||
|
||||
# Close peers if too much
|
||||
closed = 0
|
||||
connected_peers = self.getConnectedPeers()
|
||||
need_to_close = len(connected_peers) - config.connected_limit
|
||||
# First try to remove active peers
|
||||
if need_to_close > 0:
|
||||
for peer in connected_peers:
|
||||
if not peer.key.endswith(":0"): # Connectable peer
|
||||
peer.remove()
|
||||
closed += 1
|
||||
if closed >= need_to_close:
|
||||
break
|
||||
|
||||
# Also remove passive peers if still more than we need
|
||||
if closed < need_to_close:
|
||||
for peer in connected_peers:
|
||||
peer.remove()
|
||||
closed += 1
|
||||
if closed >= need_to_close:
|
||||
break
|
||||
|
||||
if need_to_close > 0:
|
||||
self.log.debug("Connected: %s, Need to close: %s, Closed: %s" % (len(connected_peers), need_to_close, closed))
|
||||
|
||||
# Send hashfield to peers
|
||||
def sendMyHashfield(self, limit=3):
|
||||
if not self.content_manager.hashfield: # No optional files
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue