2019-06-13 06:58:17 +00:00
|
|
|
'''
|
|
|
|
Onionr - Private P2P Communication
|
|
|
|
|
|
|
|
Select a random online peer in a communicator instance and have them "cool down"
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
'''
|
2019-06-25 23:07:35 +00:00
|
|
|
from onionrutils import epoch
|
2019-07-11 07:39:35 +00:00
|
|
|
from communicator import onlinepeers
|
2019-06-13 06:58:17 +00:00
|
|
|
def cooldown_peer(comm_inst):
|
|
|
|
'''Randomly add an online peer to cooldown, so we can connect a new one'''
|
2019-07-18 17:40:48 +00:00
|
|
|
config = comm_inst.config
|
2019-06-13 06:58:17 +00:00
|
|
|
onlinePeerAmount = len(comm_inst.onlinePeers)
|
|
|
|
minTime = 300
|
|
|
|
cooldownTime = 600
|
|
|
|
toCool = ''
|
|
|
|
tempConnectTimes = dict(comm_inst.connectTimes)
|
|
|
|
|
|
|
|
# Remove peers from cooldown that have been there long enough
|
|
|
|
tempCooldown = dict(comm_inst.cooldownPeer)
|
|
|
|
for peer in tempCooldown:
|
2019-06-25 23:07:35 +00:00
|
|
|
if (epoch.get_epoch() - tempCooldown[peer]) >= cooldownTime:
|
2019-06-13 06:58:17 +00:00
|
|
|
del comm_inst.cooldownPeer[peer]
|
|
|
|
|
|
|
|
# Cool down a peer, if we have max connections alive for long enough
|
2019-07-18 17:40:48 +00:00
|
|
|
if onlinePeerAmount >= config.get('peers.max_connect', 10, save = True):
|
2019-06-13 06:58:17 +00:00
|
|
|
finding = True
|
|
|
|
|
|
|
|
while finding:
|
|
|
|
try:
|
|
|
|
toCool = min(tempConnectTimes, key=tempConnectTimes.get)
|
2019-06-25 23:07:35 +00:00
|
|
|
if (epoch.get_epoch() - tempConnectTimes[toCool]) < minTime:
|
2019-06-13 06:58:17 +00:00
|
|
|
del tempConnectTimes[toCool]
|
|
|
|
else:
|
|
|
|
finding = False
|
|
|
|
except ValueError:
|
|
|
|
break
|
|
|
|
else:
|
2019-07-11 07:39:35 +00:00
|
|
|
onlinepeers.remove_online_peer(comm_inst, toCool)
|
2019-06-25 23:07:35 +00:00
|
|
|
comm_inst.cooldownPeer[toCool] = epoch.get_epoch()
|
2019-06-13 06:58:17 +00:00
|
|
|
|
|
|
|
comm_inst.decrementThreadCount('cooldown_peer')
|