Skip to content

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

Saleor 4422/3.1/increase timeout for sync webhooks (#8156)

L3str4nge4972a66204ab
diff --git a/saleor/plugins/webhook/tasks.py b/saleor/plugins/webhook/tasks.py
index 56380cb379..542619ba22 100644
--- a/saleor/plugins/webhook/tasks.py
+++ b/saleor/plugins/webhook/tasks.py
@@ -12,6 +12,7 @@ from requests.exceptions import RequestException
from ...celeryconf import app
from ...payment import PaymentError
+from ...settings import WEBHOOK_SYNC_TIMEOUT, WEBHOOK_TIMEOUT
from ...site.models import Site
from ...webhook.event_types import WebhookEventType
from ...webhook.models import Webhook
@@ -23,8 +24,6 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
task_logger = get_task_logger(__name__)
-WEBHOOK_TIMEOUT = 10
-
class WebhookSchemes(str, Enum):
HTTP = "http"
@@ -80,7 +79,9 @@ def trigger_webhook_sync(event_type: str, data: str, app: "App"):
)
-def send_webhook_using_http(target_url, message, domain, signature, event_type):
+def send_webhook_using_http(
+ target_url, message, domain, signature, event_type, timeout=WEBHOOK_TIMEOUT
+):
headers = {
"Content-Type": "application/json",
"X-Saleor-Event": event_type,
@@ -88,9 +89,7 @@ def send_webhook_using_http(target_url, message, domain, signature, event_type):
"X-Saleor-Signature": signature,
}
- response = requests.post(
- target_url, data=message, headers=headers, timeout=WEBHOOK_TIMEOUT
- )
+ response = requests.post(target_url, data=message, headers=headers, timeout=timeout)
response.raise_for_status()
return response
@@ -186,7 +185,12 @@ def send_webhook_request_sync(target_url, secret, event_type, data: str):
)
try:
response = send_webhook_using_http(
- target_url, message, domain, signature, event_type
+ target_url,
+ message,
+ domain,
+ signature,
+ event_type,
+ timeout=WEBHOOK_SYNC_TIMEOUT,
)
response_data = response.json()
except RequestException as e:
diff --git a/saleor/plugins/webhook/tests/test_payment_webhook.py b/saleor/plugins/webhook/tests/test_payment_webhook.py
index 3975a4e783..c565d60e49 100644
--- a/saleor/plugins/webhook/tests/test_payment_webhook.py
+++ b/saleor/plugins/webhook/tests/test_payment_webhook.py
@@ -1,7 +1,9 @@
import json
+from collections import namedtuple
from unittest import mock
import pytest
+from django.conf import settings
from ....app.models import App
from ....payment import PaymentError, TransactionKind
@@ -10,6 +12,7 @@ from ....webhook.event_types import WebhookEventType
from ....webhook.models import Webhook, WebhookEvent
from ...manager import get_plugins_manager
from ..tasks import (
+ send_webhook_request,
send_webhook_request_sync,
signature_for_payload,
trigger_webhook_sync,
@@ -41,6 +44,18 @@ def webhook_plugin(settings):
return factory
+WebhookTestData = namedtuple("WebhookTestData", "secret, event_type, data, message")
+
+
+def webhook_data():
+ secret = "secret"
+ event_type = WebhookEventType.ANY
+ data = json.dumps({"key": "value"})
+ message = data.encode("utf-8")
+ return WebhookTestData(secret, event_type, data, message)
+
+
@mock.patch("saleor.plugins.webhook.tasks.send_webhook_request_sync")
def test_trigger_webhook_sync(mock_request, payment_app):
data = {"key": "value"}
@@ -89,20 +104,39 @@ def test_trigger_webhook_sync_no_webhook_available():
("http://payment-gateway.com/api/", "https://payment-gateway.com/api/"),
)
@mock.patch("saleor.plugins.webhook.tasks.send_webhook_using_http")
-def test_send_webhook_request_sync(mock_send_http, target_url, site_settings):
- secret = "secret"
- event_type = WebhookEventType.PAYMENT_CAPTURE
- data = json.dumps({"key": "value"})
- message = data.encode("utf-8")
-
- send_webhook_request_sync(target_url, secret, event_type, data)
+def test_send_webhook_request_sync(
+ mock_send_http, target_url, site_settings, webhook_data
+):
+ send_webhook_request_sync(
+ target_url, webhook_data.secret, webhook_data.event_type, webhook_data.data
+ )
mock_send_http.assert_called_once_with(
target_url,
- message,
+ webhook_data.message,
site_settings.site.domain,
- signature_for_payload(message, secret),
- event_type,
+ signature_for_payload(webhook_data.message, webhook_data.secret),
+ webhook_data.event_type,
+ timeout=settings.WEBHOOK_SYNC_TIMEOUT,
+ )
+
+
+ "target_url",
+ ("http://payment-gateway.com/api/", "https://payment-gateway.com/api/"),
+)
[email protected]("saleor.plugins.webhook.tasks.requests.post")
+def test_send_webhook_request_with_proper_timeout(
+ mock_post, target_url, site_settings, webhook_data
+):
+ send_webhook_request(
+ "ID",
+ target_url,
+ webhook_data.secret,
+ webhook_data.event_type,
+ webhook_data.data,
)
+ assert mock_post.call_count == 1
+ assert mock_post.call_args.kwargs["timeout"] == settings.WEBHOOK_TIMEOUT
def test_send_webhook_request_sync_invalid_scheme():
diff --git a/saleor/settings.py b/saleor/settings.py
index 9c98f5896f..2ba71412c8 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -557,6 +557,11 @@ if (
"if ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL is enabled."
)
+# Timeouts for webhook requests. Sync webhooks (eg. payment webhook) need more time
+# for getting response from the server.
+WEBHOOK_TIMEOUT = 10
+WEBHOOK_SYNC_TIMEOUT = 20
+
# Initialize a simple and basic Jaeger Tracing integration
# for open-tracing if enabled.
#