Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Implement rlimits (#17692)

Filip Owczarek3b387e40a2b3
diff --git a/saleor/core/rlimit.py b/saleor/core/rlimit.py
new file mode 100644
index 0000000000..822dce191e
--- /dev/null
+++ b/saleor/core/rlimit.py
@@ -0,0 +1,60 @@
+import resource
+
+from django.core.exceptions import ImproperlyConfigured
+
+RLIMIT_TYPE = resource.RLIMIT_DATA
+
+
+def is_soft_limit_set_without_hard_limit(soft_limit_in_MB, hard_limit_in_MB):
+ return soft_limit_in_MB is not None and hard_limit_in_MB is None
+
+
+def is_hard_limit_set_without_soft_limit(soft_limit_in_MB, hard_limit_in_MB):
+ return soft_limit_in_MB is None and hard_limit_in_MB is not None
+
+
+def validate_and_set_rlimit(soft_limit_in_MB, hard_limit_in_MB):
+ """Set the memory limit for the process.
+
+ This function sets the soft and hard memory limits for the process using
+ the resource module. The limits are specified in megabytes (MB) and
+ are converted to bytes before being set. If the limits are not specified,
+ the function sets the limits to infinity (no limit).
+ If the soft limit is reached, the process will raise a `MemoryError`.
+ """
+
+ try:
+ soft_limit_in_MB = int(soft_limit_in_MB) if soft_limit_in_MB else None
+ hard_limit_in_MB = int(hard_limit_in_MB) if hard_limit_in_MB else None
+ except ValueError as e:
+ raise ImproperlyConfigured(
+ "Memory limits must be integers(`SOFT_MEMORY_LIMIT_IN_MB` or `HARD_MEMORY_LIMIT_IN_MB`)."
+ ) from e
+
+ if is_soft_limit_set_without_hard_limit(
+ soft_limit_in_MB, hard_limit_in_MB
+ ) or is_hard_limit_set_without_soft_limit(soft_limit_in_MB, hard_limit_in_MB):
+ raise ImproperlyConfigured(
+ "Both `SOFT_MEMORY_LIMIT_IN_MB` and `HARD_MEMORY_LIMIT_IN_MB` must be set to enable memory limits."
+ )
+
+ soft_memory_limit = (
+ soft_limit_in_MB * 1000 * 1000 if soft_limit_in_MB else resource.RLIM_INFINITY
+ )
+ hard_memory_limit = (
+ hard_limit_in_MB * 1000 * 1000 if hard_limit_in_MB else resource.RLIM_INFINITY
+ )
+ if soft_memory_limit > hard_memory_limit:
+ raise ImproperlyConfigured(
+ "Soft memory limit cannot be greater than hard memory limit."
+ )
+ if soft_memory_limit < 0 and soft_memory_limit != resource.RLIM_INFINITY:
+ raise ImproperlyConfigured(
+ "Soft memory limit(SOFT_MEMORY_LIMIT_IN_MB) cannot be negative."
+ )
+ if hard_memory_limit < 0 and hard_memory_limit != resource.RLIM_INFINITY:
+ raise ImproperlyConfigured(
+ "Hard memory limit(HARD_MEMORY_LIMIT_IN_MB) cannot be negative."
+ )
+
+ resource.setrlimit(RLIMIT_TYPE, (soft_memory_limit, hard_memory_limit))
diff --git a/saleor/core/tests/test_rlimit.py b/saleor/core/tests/test_rlimit.py
new file mode 100644
index 0000000000..874fc59dbc
--- /dev/null
+++ b/saleor/core/tests/test_rlimit.py
@@ -0,0 +1,159 @@
+import resource
+from unittest.mock import patch
+
+import pytest
+from django.core.exceptions import ImproperlyConfigured
+
+from ..rlimit import RLIMIT_TYPE, validate_and_set_rlimit
+
+
+@patch("saleor.core.rlimit.resource.setrlimit")
+def test_limit_passed_as_string(mock_setrlimit):
+ # given
+ soft_limit = "1000"
+ hard_limit = "2000"
+ expected_soft_limit = int(soft_limit) * 1000 * 1000
+ expected_hard_limit = int(hard_limit) * 1000 * 1000
+
+ # when
+ validate_and_set_rlimit(soft_limit, hard_limit)
+
+ # then
+ mock_setrlimit.assert_called_once_with(
+ RLIMIT_TYPE,
+ (expected_soft_limit, expected_hard_limit),
+ )
+
+
+@patch("saleor.core.rlimit.resource.setrlimit")
+def test_limit_passed_as_int(mock_setrlimit):
+ # given
+ soft_limit = 1000
+ hard_limit = 2000
+ expected_soft_limit = int(soft_limit) * 1000 * 1000
+ expected_hard_limit = int(hard_limit) * 1000 * 1000
+
+ # when
+ validate_and_set_rlimit(soft_limit, hard_limit)
+
+ # then
+ mock_setrlimit.assert_called_once_with(
+ RLIMIT_TYPE,
+ (expected_soft_limit, expected_hard_limit),
+ )
+
+
+@patch("saleor.core.rlimit.resource.setrlimit")
+def test_no_limits(mock_setrlimit):
+ # given
+ soft_limit = None
+ hard_limit = None
+ expected_soft_limit = resource.RLIM_INFINITY
+ expected_hard_limit = resource.RLIM_INFINITY
+
+ # when
+ validate_and_set_rlimit(soft_limit, hard_limit)
+
+ # then
+ mock_setrlimit.assert_called_once_with(
+ RLIMIT_TYPE,
+ (expected_soft_limit, expected_hard_limit),
+ )
+
+
+@patch("saleor.core.rlimit.resource.setrlimit")
+def test_only_soft_limit_set(mock_setrlimit):
+ # given
+ soft_limit = 1000
+ hard_limit = None
+
+ # when
+ with pytest.raises(ImproperlyConfigured):
+ validate_and_set_rlimit(soft_limit, hard_limit)
+
+ # then
+ mock_setrlimit.assert_not_called()
+
+
+@patch("saleor.core.rlimit.resource.setrlimit")
+def test_only_hard_limit_set(mock_setrlimit):
+ # given
+ soft_limit = None
+ hard_limit = 1000
+
+ # when
+ with pytest.raises(ImproperlyConfigured):
+ validate_and_set_rlimit(soft_limit, hard_limit)
+
+ # then
+ mock_setrlimit.assert_not_called()
+
+
+@patch("saleor.core.rlimit.resource.setrlimit")
+def test_negative_soft_limit(mock_setrlimit):
+ # given
+ soft_limit = -10
+ hard_limit = 1000
+
+ # when
+ with pytest.raises(ImproperlyConfigured):
+ validate_and_set_rlimit(soft_limit, hard_limit)
+
+ # then
+ mock_setrlimit.assert_not_called()
+
+
+@patch("saleor.core.rlimit.resource.setrlimit")
+def test_negative_hard_limit(mock_setrlimit):
+ # given
+ soft_limit = 1000
+ hard_limit = -10
+
+ # when
+ with pytest.raises(ImproperlyConfigured):
+ validate_and_set_rlimit(soft_limit, hard_limit)
+
+ # then
+ mock_setrlimit.assert_not_called()
+
+
+@patch("saleor.core.rlimit.resource.setrlimit")
+def test_invalid_soft_limit(mock_setrlimit):
+ # given
+ soft_limit = "invalid"
+ hard_limit = 1000
+
+ # when
+ with pytest.raises(ImproperlyConfigured):
+ validate_and_set_rlimit(soft_limit, hard_limit)
+
+ # then
+ mock_setrlimit.assert_not_called()
+
+
+@patch("saleor.core.rlimit.resource.setrlimit")
+def test_invalid_hard_limit(mock_setrlimit):
+ # given
+ soft_limit = 1000
+ hard_limit = "invalid"
+
+ # when
+ with pytest.raises(ImproperlyConfigured):
+ validate_and_set_rlimit(soft_limit, hard_limit)
+
+ # then
+ mock_setrlimit.assert_not_called()
+
+
+@patch("saleor.core.rlimit.resource.setrlimit")
+def test_soft_limit_greater_than_hard_limit(mock_setrlimit):
+ # given
+ soft_limit = 2000
+ hard_limit = 1000
+
+ # when
+ with pytest.raises(ImproperlyConfigured):
+ validate_and_set_rlimit(soft_limit, hard_limit)
+
+ # then
+ mock_setrlimit.assert_not_called()
diff --git a/saleor/settings.py b/saleor/settings.py
index 0947093453..7fea43e892 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -30,6 +30,7 @@ from . import PatchedSubscriberExecutionContext, __version__
from .account.i18n_rules_override import i18n_rules_override
from .core.db.patch import patch_db
from .core.languages import LANGUAGES as CORE_LANGUAGES
+from .core.rlimit import validate_and_set_rlimit
from .core.schedules import initiated_promotion_webhook_schedule
from .graphql.executor import patch_executor
from .graphql.promise import patch_promise
@@ -62,6 +63,13 @@ def get_url_from_env(name, *, schemes=None) -> str | None:
return None
+# Possibility to set memory limits for the process. Function `validate_and_set_rlimit` set the
+# maximum size of the process's heap(`resource.RLIMIT_DATA`). If you set the memory limit and process will try to
+# allocate more memory than the limit, it will raise `MemoryError`.
+SOFT_MEMORY_LIMIT_IN_MB = os.environ.get("SOFT_MEMORY_LIMIT_IN_MB", None)
+HARD_MEMORY_LIMIT_IN_MB = os.environ.get("HARD_MEMORY_LIMIT_IN_MB", None)
+validate_and_set_rlimit(SOFT_MEMORY_LIMIT_IN_MB, HARD_MEMORY_LIMIT_IN_MB)
+
DEBUG = get_bool_from_env("DEBUG", True)
SITE_ID = 1