Skip to content

Commit

Permalink
Refactor TriblerNetworkRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jan 20, 2023
1 parent 7d66b22 commit 92c36a4
Show file tree
Hide file tree
Showing 33 changed files with 1,026 additions and 638 deletions.
19 changes: 19 additions & 0 deletions src/tribler/core/utilities/limited_ordered_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from collections import OrderedDict


class LimitedOrderedDict(OrderedDict):
""" This class is an implementation of OrderedDict with size limit.
If the size of the dict exceeds the limit, the oldest entries will be deleted.
"""
def __init__(self, *args, limit: int = 200, **kwargs):
self.limit = limit
super().__init__(*args, **kwargs)

def __setitem__(self, key, value):
super().__setitem__(key, value)
self._adjust_size()

def _adjust_size(self):
while len(self) > self.limit:
self.popitem(last=False)
27 changes: 27 additions & 0 deletions src/tribler/core/utilities/tests/test_limited_ordered_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from tribler.core.utilities.limited_ordered_dict import LimitedOrderedDict


def test_order():
d = LimitedOrderedDict()
d['first'] = '1'
d['second'] = '2'
d['third'] = '3'

assert list(d.keys()) == ['first', 'second', 'third']


def test_limit():
d = LimitedOrderedDict(limit=2)
d['first'] = '1'
d['second'] = '2'
d['third'] = '3'

assert list(d.keys()) == ['second', 'third']


def test_merge():
d1 = {'first': 1, 'second': 2}
d2 = {'third': 3, 'fourth': 4}

d = LimitedOrderedDict({**d1, **d2}, limit=2)
assert len(d) == 2
6 changes: 4 additions & 2 deletions src/tribler/gui/core_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from tribler.gui.app_manager import AppManager
from tribler.gui.event_request_manager import EventRequestManager
from tribler.gui.exceptions import CoreConnectTimeoutError, CoreCrashedError
from tribler.gui.tribler_request_manager import ShutdownRequest, request_manager
from tribler.gui.network.request.shutdown_request import ShutdownRequest
from tribler.gui.network.request_manager import request_manager
from tribler.gui.utilities import connect


Expand Down Expand Up @@ -237,7 +238,8 @@ def send_shutdown_request(initial=False):
else:
self._logger.warning("Re-sending shutdown request to Tribler Core")

ShutdownRequest(shutdown_request_processed, on_cancel=send_shutdown_request)
request = ShutdownRequest(shutdown_request_processed, on_cancel=send_shutdown_request)
request_manager.add(request)

send_shutdown_request(initial=True)

Expand Down
Loading

0 comments on commit 92c36a4

Please sign in to comment.