(self, peers_protected=[])
| 924 | |
| 925 | # Cleanup probably dead peers and close connection if too much |
| 926 | def cleanupPeers(self, peers_protected=[]): |
| 927 | peers = list(self.peers.values()) |
| 928 | if len(peers) > 20: |
| 929 | # Cleanup old peers |
| 930 | removed = 0 |
| 931 | if len(peers) > 1000: |
| 932 | ttl = 60 * 60 * 1 |
| 933 | else: |
| 934 | ttl = 60 * 60 * 4 |
| 935 | |
| 936 | for peer in peers: |
| 937 | if peer.connection and peer.connection.connected: |
| 938 | continue |
| 939 | if peer.connection and not peer.connection.connected: |
| 940 | peer.connection = None # Dead connection |
| 941 | if time.time() - peer.time_found > ttl: # Not found on tracker or via pex in last 4 hour |
| 942 | peer.remove("Time found expired") |
| 943 | removed += 1 |
| 944 | if removed > len(peers) * 0.1: # Don't remove too much at once |
| 945 | break |
| 946 | |
| 947 | if removed: |
| 948 | self.log.debug("Cleanup peers result: Removed %s, left: %s" % (removed, len(self.peers))) |
| 949 | |
| 950 | # Close peers over the limit |
| 951 | closed = 0 |
| 952 | connected_peers = [peer for peer in self.getConnectedPeers() if peer.connection.connected] # Only fully connected peers |
| 953 | need_to_close = len(connected_peers) - config.connected_limit |
| 954 | |
| 955 | if closed < need_to_close: |
| 956 | # Try to keep connections with more sites |
| 957 | for peer in sorted(connected_peers, key=lambda peer: min(peer.connection.sites, 5)): |
| 958 | if not peer.connection: |
| 959 | continue |
| 960 | if peer.key in peers_protected: |
| 961 | continue |
| 962 | if peer.connection.sites > 5: |
| 963 | break |
| 964 | peer.connection.close("Cleanup peers") |
| 965 | peer.connection = None |
| 966 | closed += 1 |
| 967 | if closed >= need_to_close: |
| 968 | break |
| 969 | |
| 970 | if need_to_close > 0: |
| 971 | self.log.debug("Connected: %s, Need to close: %s, Closed: %s" % (len(connected_peers), need_to_close, closed)) |
| 972 | |
| 973 | # Send hashfield to peers |
| 974 | def sendMyHashfield(self, limit=5): |
no test coverage detected