Skip to content

Commit

Permalink
Set libtorrent max rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
xoriole committed Oct 29, 2020
1 parent 1c4335c commit 1fce01f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/tribler-common/tribler_common/simpledefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ class NTFY(Enum):
LOW_SPACE = "low_space"
EVENTS_START = "events_start"
TRIBLER_EXCEPTION = "tribler_exception"


# Max download or upload rate limit for libtorrent.
# On Win64, the compiled version of libtorrent only supported 2^31 - 1
# as rate limit values instead of sys.maxsize or 2^63 -1. Since 2^31
# is a sufficiently large value for download/upload rate limit,
# here we set the max values for these parameters.
MAX_LIBTORRENT_RATE_LIMIT = 2**31 - 1 # bytes per second
14 changes: 14 additions & 0 deletions src/tribler-core/tribler_core/config/test_tribler_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from tribler_common.simpledefs import MAX_LIBTORRENT_RATE_LIMIT

from tribler_core.config.tribler_config import CONFIG_FILENAME, TriblerConfig
from tribler_core.tests.tools.base_test import TriblerCoreTest
Expand Down Expand Up @@ -196,6 +197,19 @@ def test_get_set_methods_libtorrent(self):
self.tribler_config.set_libtorrent_dht_enabled(False)
self.assertFalse(self.tribler_config.get_libtorrent_dht_enabled())

# Add tests for setting libtorrent rate limits
rate_limit = MAX_LIBTORRENT_RATE_LIMIT - 1024 # lower than the max value set
self.tribler_config.set_libtorrent_max_upload_rate(rate_limit)
self.assertEqual(self.tribler_config.get_libtorrent_max_upload_rate(), rate_limit)
self.tribler_config.set_libtorrent_max_download_rate(rate_limit)
self.assertEqual(self.tribler_config.get_libtorrent_max_download_rate(), rate_limit)

rate_limit = MAX_LIBTORRENT_RATE_LIMIT + 1024 # higher than the max value set
self.tribler_config.set_libtorrent_max_upload_rate(rate_limit)
self.assertEqual(self.tribler_config.get_libtorrent_max_upload_rate(), MAX_LIBTORRENT_RATE_LIMIT)
self.tribler_config.set_libtorrent_max_download_rate(rate_limit)
self.assertEqual(self.tribler_config.get_libtorrent_max_download_rate(), MAX_LIBTORRENT_RATE_LIMIT)

def test_get_set_methods_tunnel_community(self):
"""
Check whether tunnel community get and set methods are working as expected.
Expand Down
6 changes: 4 additions & 2 deletions src/tribler-core/tribler_core/config/tribler_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from validate import Validator

from tribler_common.simpledefs import MAX_LIBTORRENT_RATE_LIMIT

from tribler_core.exceptions import InvalidConfigException
from tribler_core.modules.libtorrent.download_config import get_default_dest_dir
from tribler_core.utilities import path_util
Expand Down Expand Up @@ -406,7 +408,7 @@ def get_libtorrent_max_upload_rate(self):
:return: the maximum upload rate in kB / s
"""
return self.config['libtorrent'].as_int('max_upload_rate')
return min(self.config['libtorrent'].as_int('max_upload_rate'), MAX_LIBTORRENT_RATE_LIMIT)

def set_libtorrent_max_download_rate(self, value):
"""
Expand All @@ -423,7 +425,7 @@ def get_libtorrent_max_download_rate(self):
:return: the maximum download rate in kB / s
"""
return self.config['libtorrent'].as_int('max_download_rate')
return min(self.config['libtorrent'].as_int('max_download_rate'), MAX_LIBTORRENT_RATE_LIMIT)

def set_libtorrent_dht_enabled(self, value):
self.config['libtorrent']['dht'] = value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from tribler_common.simpledefs import MAX_LIBTORRENT_RATE_LIMIT

from tribler_core.modules.libtorrent.download_config import DownloadConfig
from tribler_core.restapi.base_api_test import AbstractApiTest
from tribler_core.tests.tools.base_test import MockObject
Expand Down Expand Up @@ -88,3 +90,26 @@ async def test_set_settings(self):
self.assertEqual(self.session.config.get_seeding_mode(), 'ratio')
self.assertEqual(self.session.config.get_seeding_ratio(), 3)
self.assertEqual(self.session.config.get_seeding_time(), 123)

@timeout(10)
async def test_set_rate_settings(self):
"""
Testing whether libtorrent rate limits works for large number without overflow error.
"""

dcfg = DownloadConfig()
download = MockObject()
download.config = dcfg
self.session.dlmgr.get_downloads = lambda: [download]

extra_rate = 1024 * 1024 * 1024 # 1GB/s
post_data = {
'libtorrent': {
'max_download_rate': MAX_LIBTORRENT_RATE_LIMIT + extra_rate,
'max_upload_rate': MAX_LIBTORRENT_RATE_LIMIT + extra_rate
}
}
await self.do_request('settings', expected_code=200, request_type='POST', post_data=post_data)

self.assertEqual(self.session.config.get_libtorrent_max_download_rate(), MAX_LIBTORRENT_RATE_LIMIT)
self.assertEqual(self.session.config.get_libtorrent_max_upload_rate(), MAX_LIBTORRENT_RATE_LIMIT)
17 changes: 9 additions & 8 deletions src/tribler-gui/tribler_gui/widgets/settingspage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import sys

from PIL.ImageQt import ImageQt

from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QFileDialog, QLabel, QSizePolicy, QWidget

from tribler_common.simpledefs import MAX_LIBTORRENT_RATE_LIMIT

import tribler_core.utilities.json_util as json

from tribler_gui.defs import (
Expand Down Expand Up @@ -455,23 +455,24 @@ def save_settings(self):

try:
if self.window().upload_rate_limit_input.text():
user_upload_rate_limit = int(self.window().upload_rate_limit_input.text()) * 1024
if user_upload_rate_limit < sys.maxsize:
user_upload_rate_limit = int(float(self.window().upload_rate_limit_input.text()) * 1024)
if user_upload_rate_limit < MAX_LIBTORRENT_RATE_LIMIT:
settings_data['libtorrent']['max_upload_rate'] = user_upload_rate_limit
else:
raise ValueError
if self.window().download_rate_limit_input.text():
user_download_rate_limit = int(self.window().download_rate_limit_input.text()) * 1024
if user_download_rate_limit < sys.maxsize:
user_download_rate_limit = int(float(self.window().download_rate_limit_input.text()) * 1024)
if user_download_rate_limit < MAX_LIBTORRENT_RATE_LIMIT:
settings_data['libtorrent']['max_download_rate'] = user_download_rate_limit
else:
raise ValueError
except ValueError:
ConfirmationDialog.show_error(
self.window(),
"Invalid value for bandwidth limit",
"You've entered an invalid value for the maximum upload/download rate. "
"Please enter a whole number (max: %d)" % (sys.maxsize / 1000),
"You've entered an invalid value for the maximum upload/download rate. \n"
"The rate is specified in KB/s and the value permitted is between 0 and %d KB/s.\n"
"Note that the decimal values are truncated." % (MAX_LIBTORRENT_RATE_LIMIT / 1024),
)
return

Expand Down

0 comments on commit 1fce01f

Please sign in to comment.