Skip to content

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

Add timeout to pubsub requests (#17103)

Maciej Korycinskibe23aaf4548b
diff --git a/saleor/settings.py b/saleor/settings.py
index 82ab707165..44c1f018bd 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -964,8 +964,10 @@ REQUESTS_CONN_EST_TIMEOUT = 2
# Default timeout for external requests.
COMMON_REQUESTS_TIMEOUT = (REQUESTS_CONN_EST_TIMEOUT, 18)
-WEBHOOK_TIMEOUT = (REQUESTS_CONN_EST_TIMEOUT, 18)
-WEBHOOK_SYNC_TIMEOUT = (REQUESTS_CONN_EST_TIMEOUT, 18)
+WEBHOOK_WAITING_FOR_RESPONSE_TIMEOUT = 18
+
+WEBHOOK_TIMEOUT = (REQUESTS_CONN_EST_TIMEOUT, WEBHOOK_WAITING_FOR_RESPONSE_TIMEOUT)
+WEBHOOK_SYNC_TIMEOUT = (REQUESTS_CONN_EST_TIMEOUT, WEBHOOK_WAITING_FOR_RESPONSE_TIMEOUT)
# The max number of rules with order_predicate defined
ORDER_RULES_LIMIT = os.environ.get("ORDER_RULES_LIMIT", 100)
diff --git a/saleor/webhook/tests/test_webhook_protocols.py b/saleor/webhook/tests/test_webhook_protocols.py
index 96c185884a..9852720234 100644
--- a/saleor/webhook/tests/test_webhook_protocols.py
+++ b/saleor/webhook/tests/test_webhook_protocols.py
@@ -9,6 +9,7 @@ from django.core.serializers import serialize
from google.cloud.pubsub_v1 import PublisherClient
from requests_hardened import HTTPSession
+from ...core import EventDeliveryStatus
from ...core.models import EventDelivery
from ..event_types import WebhookEventAsyncType
from ..transport import signature_for_payload
@@ -158,9 +159,13 @@ def test_trigger_webhooks_with_google_pub_sub(
permission_manage_users,
permission_manage_products,
monkeypatch,
+ settings,
):
+ # given
+ mocked_future_result = MagicMock()
+ mocked_future_result.return_value = "message_id"
mocked_publisher = MagicMock(spec=PublisherClient)
- mocked_publisher.publish.return_value.result.return_value = "message_id"
+ mocked_publisher.publish.return_value.result = mocked_future_result
monkeypatch.setattr(
"saleor.webhook.transport.utils.pubsub_v1.PublisherClient",
lambda: mocked_publisher,
@@ -171,12 +176,71 @@ def test_trigger_webhooks_with_google_pub_sub(
expected_data = serialize("json", [order_with_lines])
expected_signature = signature_for_payload(expected_data.encode("utf-8"), None)
+ # when
trigger_webhooks_async(
expected_data,
WebhookEventAsyncType.ORDER_CREATED,
[webhook],
allow_replica=False,
)
+
+ # then
+ mocked_future_result.assert_called_once_with(
+ timeout=settings.WEBHOOK_WAITING_FOR_RESPONSE_TIMEOUT
+ )
+ mocked_publisher.publish.assert_called_once_with(
+ "projects/saleor/topics/test",
+ expected_data.encode("utf-8"),
+ saleorDomain="mirumee.com",
+ saleorApiUrl="http://mirumee.com/graphql/",
+ eventType=WebhookEventAsyncType.ORDER_CREATED,
+ signature=expected_signature,
+ )
+
+
+@patch("saleor.webhook.transport.asynchronous.transport.handle_webhook_retry")
+def test_trigger_webhooks_with_google_pub_sub_when_timout_error_raised(
+ mocked_retry,
+ webhook,
+ order_with_lines,
+ permission_manage_orders,
+ permission_manage_users,
+ permission_manage_products,
+ monkeypatch,
+ settings,
+):
+ # given
+ mocked_future_result = MagicMock()
+ mocked_future_result.side_effect = TimeoutError
+
+ mocked_publisher = MagicMock(spec=PublisherClient)
+ mocked_publisher.publish.return_value.result = mocked_future_result
+ monkeypatch.setattr(
+ "saleor.webhook.transport.utils.pubsub_v1.PublisherClient",
+ lambda: mocked_publisher,
+ )
+ webhook.app.permissions.add(permission_manage_orders)
+ webhook.target_url = "gcpubsub://cloud.google.com/projects/saleor/topics/test"
+ webhook.save()
+ expected_data = serialize("json", [order_with_lines])
+ expected_signature = signature_for_payload(expected_data.encode("utf-8"), None)
+
+ # when
+ trigger_webhooks_async(
+ expected_data,
+ WebhookEventAsyncType.ORDER_CREATED,
+ [webhook],
+ allow_replica=False,
+ )
+
+ # then
+ event_delivery = EventDelivery.objects.get()
+ assert event_delivery.status == EventDeliveryStatus.FAILED
+ assert mocked_retry.called
+
+ mocked_future_result.assert_called_once_with(
+ timeout=settings.WEBHOOK_WAITING_FOR_RESPONSE_TIMEOUT
+ )
mocked_publisher.publish.assert_called_once_with(
"projects/saleor/topics/test",
expected_data.encode("utf-8"),
diff --git a/saleor/webhook/transport/utils.py b/saleor/webhook/transport/utils.py
index 53bd2546af..62085645d2 100644
--- a/saleor/webhook/transport/utils.py
+++ b/saleor/webhook/transport/utils.py
@@ -288,10 +288,16 @@ def send_webhook_using_google_cloud_pubsub(
eventType=event_type,
signature=signature,
)
- except (pubsub_v1.publisher.exceptions.MessageTooLargeError, RuntimeError) as e:
+ response = future.result(
+ timeout=settings.WEBHOOK_WAITING_FOR_RESPONSE_TIMEOUT
+ )
+ except (
+ pubsub_v1.publisher.exceptions.MessageTooLargeError,
+ RuntimeError,
+ TimeoutError,
+ ) as e:
return WebhookResponse(content=str(e), status=EventDeliveryStatus.FAILED)
response_duration = duration()
- response = future.result()
return WebhookResponse(content=response, duration=response_duration)