2020-07-07 13:37:23 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-06-13 06:58:17 +00:00
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
Select random online peer in a communicator instance and have them "cool down"
|
|
|
|
"""
|
2020-07-26 03:28:32 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
from onionrutils import epoch
|
|
|
|
from communicator import onlinepeers
|
2020-07-26 03:28:32 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from deadsimplekv import DeadSimpleKV
|
2020-07-07 13:37:23 +00:00
|
|
|
"""
|
2019-06-13 06:58:17 +00:00
|
|
|
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/>.
|
2020-07-07 13:37:23 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-06-13 06:58:17 +00:00
|
|
|
def cooldown_peer(comm_inst):
|
2020-07-07 13:37:23 +00:00
|
|
|
"""Randomly add an online peer to cooldown, so we can connect a new one."""
|
2020-07-26 03:28:32 +00:00
|
|
|
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
|
2019-07-18 17:40:48 +00:00
|
|
|
config = comm_inst.config
|
2020-07-26 03:28:32 +00:00
|
|
|
online_peer_amount = len(kv.get('onlinePeers'))
|
2019-06-13 06:58:17 +00:00
|
|
|
minTime = 300
|
2020-07-07 13:37:23 +00:00
|
|
|
cooldown_time = 600
|
|
|
|
to_cool = ''
|
2020-07-29 08:57:06 +00:00
|
|
|
tempConnectTimes = dict(kv.get('connectTimes'))
|
2019-06-13 06:58:17 +00:00
|
|
|
|
|
|
|
# Remove peers from cooldown that have been there long enough
|
2020-07-27 00:15:26 +00:00
|
|
|
tempCooldown = dict(kv.get('cooldownPeer'))
|
2019-06-13 06:58:17 +00:00
|
|
|
for peer in tempCooldown:
|
2020-07-07 13:37:23 +00:00
|
|
|
if (epoch.get_epoch() - tempCooldown[peer]) >= cooldown_time:
|
2020-07-27 00:15:26 +00:00
|
|
|
del kv.get('cooldownPeer')[peer]
|
2019-06-13 06:58:17 +00:00
|
|
|
|
|
|
|
# Cool down a peer, if we have max connections alive for long enough
|
2020-07-07 13:37:23 +00:00
|
|
|
if online_peer_amount >= config.get('peers.max_connect', 10, save=True):
|
2019-06-13 06:58:17 +00:00
|
|
|
finding = True
|
|
|
|
|
|
|
|
while finding:
|
|
|
|
try:
|
2020-07-07 13:37:23 +00:00
|
|
|
to_cool = min(tempConnectTimes, key=tempConnectTimes.get)
|
|
|
|
if (epoch.get_epoch() - tempConnectTimes[to_cool]) < minTime:
|
|
|
|
del tempConnectTimes[to_cool]
|
2019-06-13 06:58:17 +00:00
|
|
|
else:
|
|
|
|
finding = False
|
|
|
|
except ValueError:
|
|
|
|
break
|
|
|
|
else:
|
2020-07-07 13:37:23 +00:00
|
|
|
onlinepeers.remove_online_peer(comm_inst, to_cool)
|
2020-07-27 00:15:26 +00:00
|
|
|
kv.get('cooldownPeer')[to_cool] = epoch.get_epoch()
|
2019-06-13 06:58:17 +00:00
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
comm_inst.decrementThreadCount('cooldown_peer')
|