Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
outgoing_webhook: Set a default timeout of 10s.
Alex Vandiveraf2684955489
diff --git a/templates/zerver/api/outgoing-webhooks.md b/templates/zerver/api/outgoing-webhooks.mdindex a1155b5750b..bc78301ae43 100644--- a/templates/zerver/api/outgoing-webhooks.md+++ b/templates/zerver/api/outgoing-webhooks.md@@ -34,6 +34,16 @@ There are currently two ways to trigger an outgoing webhook:2. **Send a private message** with the bot as one of the recipients.If the bot replies, its reply will be sent to that thread.+## Timeouts++The remote server must respond to a `POST` request in a timely manner.+The default timeout for outgoing webhooks is 10 seconds, though this+can be configured by the administrator of the Zulip server by setting+`OUTGOING_WEBHOOKS_TIMEOUT_SECONDS` in the [server's+settings][settings].++[settings]: https://zulip.readthedocs.io/en/latest/subsystems/settings.html#server-settings+## Outgoing webhook formatThis is an example of the JSON payload that the Zulip server will `POST`diff --git a/zerver/lib/outgoing_webhook.py b/zerver/lib/outgoing_webhook.pyindex b03f68b3d30..1fd200a27bc 100644--- a/zerver/lib/outgoing_webhook.py+++ b/zerver/lib/outgoing_webhook.py@@ -5,8 +5,10 @@ from time import perf_counterfrom typing import Any, AnyStr, Dict, Optionalimport requests+from django.conf import settingsfrom django.utils.translation import gettext as _from requests import Response, Session+from urllib3 import HTTPResponsefrom version import ZULIP_VERSIONfrom zerver.decorator import JsonableError@@ -26,12 +28,26 @@ from zerver.models import ()+class TimeoutAdapter(requests.adapters.HTTPAdapter):+ def __init__(self, timeout: Optional[int] = None, *args: Any, **kwargs: Any) -> None:+ self.timeout = timeout+ super().__init__(*args, *kwargs)++ def send(self, *args: Any, **kwargs: Any) -> HTTPResponse:+ if kwargs.get("timeout") is None:+ kwargs["timeout"] = self.timeout+ return super().send(*args, **kwargs)++class OutgoingWebhookServiceInterface(metaclass=abc.ABCMeta):def __init__(self, token: str, user_profile: UserProfile, service_name: str) -> None:self.token: str = tokenself.user_profile: UserProfile = user_profileself.service_name: str = service_nameself.session: Session = Session()+ timeout_adapter = TimeoutAdapter(timeout=settings.OUTGOING_WEBHOOK_TIMEOUT_SECONDS)+ self.session.mount("http://", timeout_adapter)+ self.session.mount("https://", timeout_adapter)self.session.headers.update({"X-Smokescreen-Role": "webhook",@@ -363,7 +379,9 @@ def do_rest_call(event["command"],event["service_name"],)- failure_message = "A timeout occurred."+ failure_message = (+ f"Request timed out after {settings.OUTGOING_WEBHOOK_TIMEOUT_SECONDS} seconds."+ )request_retry(event, failure_message=failure_message)return Nonediff --git a/zerver/tests/test_outgoing_webhook_system.py b/zerver/tests/test_outgoing_webhook_system.pyindex 2463c7ac6a4..fc322cf27e1 100644--- a/zerver/tests/test_outgoing_webhook_system.py+++ b/zerver/tests/test_outgoing_webhook_system.py@@ -207,7 +207,7 @@ The webhook got a response with status code *400*.""",self.assertEqual(bot_owner_notification.recipient_id, bot_user.bot_owner.recipient_id)with self.assertLogs(level="INFO") as i:- helper(side_effect=timeout_error, error_text="A timeout occurred.")+ helper(side_effect=timeout_error, error_text="Request timed out after")helper(side_effect=connection_error, error_text="A connection error occurred.")log_output = [diff --git a/zproject/default_settings.py b/zproject/default_settings.pyindex c78a8181c11..a7f380caf6a 100644--- a/zproject/default_settings.py+++ b/zproject/default_settings.py@@ -450,3 +450,6 @@ DEFAULT_DATA_EXPORT_IMPORT_PARALLELISM = (len(os.sched_getaffinity(0)) // 2) or# Default is 18 months, constructed as 12 months before someone should# upgrade, plus 6 months for the system administrator to get around to it.SERVER_UPGRADE_NAG_DEADLINE_DAYS = 30 * 18++# How long servers have to respond to outgoing webhook requests+OUTGOING_WEBHOOK_TIMEOUT_SECONDS = 10diff --git a/zproject/prod_settings_template.py b/zproject/prod_settings_template.pyindex 3d4398d94da..2f9db977717 100644--- a/zproject/prod_settings_template.py+++ b/zproject/prod_settings_template.py@@ -588,6 +588,9 @@ SOCIAL_AUTH_SAML_SUPPORT_CONTACT = {################## Miscellaneous settings.+## How long outgoing webhook requests time out after+# OUTGOING_WEBHOOK_TIMEOUT_SECONDS = 10+## Support for mobile push notifications. Setting controls whether## push notifications will be forwarded through a Zulip push## notification bouncer server to the mobile apps. See