Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

tornado: Use a minimal URL configuration for Tornado.

Alex Vandiver8ae2e94e039d
diff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.py
index 16ae79a9b65..09c97000770 100644
--- a/zerver/tests/test_openapi.py
+++ b/zerver/tests/test_openapi.py
@@ -558,6 +558,7 @@ so maybe we shouldn't include it in pending_endpoints.
"""
from zilencer import urls as zilencer_urlconf
+ from zproject import tornado_urls as tornado_urlconf
from zproject import urls as urlconf
# We loop through all the API patterns, looking in particular
@@ -567,6 +568,7 @@ so maybe we shouldn't include it in pending_endpoints.
urlconf.v1_api_and_json_patterns
+ urlconf.v1_api_mobile_patterns
+ zilencer_urlconf.v1_api_bouncer_patterns
+ + tornado_urlconf.api_and_json_patterns
):
methods_endpoints: dict[str, Any] = {}
if p.callback not in [rest_dispatch, remote_server_dispatch]:
diff --git a/zproject/computed_settings.py b/zproject/computed_settings.py
index c9647207860..2db60218f0b 100644
--- a/zproject/computed_settings.py
+++ b/zproject/computed_settings.py
@@ -301,6 +301,9 @@ RUNNING_INSIDE_TORNADO = (
len(sys.argv) > 1 and "manage.py" in sys.argv[0] and sys.argv[1] == "runtornado"
)
+if RUNNING_INSIDE_TORNADO:
+ ROOT_URLCONF = "zproject.tornado_urls"
+
SILENCED_SYSTEM_CHECKS = [
# auth.W004 checks that the UserProfile field named by USERNAME_FIELD has
# `unique=True`. For us this is `email`, and it's unique only per-realm.
diff --git a/zproject/tornado_urls.py b/zproject/tornado_urls.py
new file mode 100644
index 00000000000..db4c1498949
--- /dev/null
+++ b/zproject/tornado_urls.py
@@ -0,0 +1,32 @@
+from django.conf.urls import include
+from django.urls import path
+
+from zerver.lib.rest import rest_path
+from zerver.tornado.views import (
+ cleanup_event_queue,
+ get_events,
+ get_events_internal,
+ notify,
+ web_reload_clients,
+)
+
+# Minimal URL configuration for Tornado. Tornado only serves 5
+# endpoints, but without a dedicated urlconf it resolves every request
+# against the full Django URL configuration (~800 patterns), wasting
+# significant CPU on regex matching.
+
+api_and_json_patterns = [
+ rest_path(
+ "events",
+ GET=(get_events, {"narrow_user_session_cache"}),
+ DELETE=(cleanup_event_queue, {"narrow_user_session_cache"}),
+ ),
+]
+
+urlpatterns = [
+ path("api/v1/", include(api_and_json_patterns)),
+ path("json/", include(api_and_json_patterns)),
+ path("api/internal/notify_tornado", notify),
+ path("api/internal/web_reload_clients", web_reload_clients),
+ path("api/v1/events/internal", get_events_internal),
+]
diff --git a/zproject/urls.py b/zproject/urls.py
index 30f8331bad0..21b4d244ab5 100644
--- a/zproject/urls.py
+++ b/zproject/urls.py
@@ -18,13 +18,6 @@ from zerver.forms import LoggingSetPasswordForm
from zerver.lib.integrations import INCOMING_WEBHOOK_INTEGRATIONS
from zerver.lib.rest import rest_path
from zerver.lib.url_redirects import DOCUMENTATION_REDIRECTS, get_integration_category_redirects
-from zerver.tornado.views import (
- cleanup_event_queue,
- get_events,
- get_events_internal,
- notify,
- web_reload_clients,
-)
from zerver.views.alert_words import add_alert_words, list_alert_words, remove_alert_words
from zerver.views.antispam import get_challenge
from zerver.views.attachments import list_by_user, remove
@@ -295,8 +288,7 @@ INTEGRATION_CATEGORY_REDIRECT_PATHS = [
# NB: There are several other pieces of code which route requests by URL:
#
-# - runtornado.py has its own URL list for Tornado views. See the
-# invocation of web.Application in that file.
+# - tornado_urls.py has its own URL list for Tornado views.
#
# - The nginx config knows which URLs to route to Django or Tornado.
#
@@ -593,12 +585,6 @@ v1_api_and_json_patterns = [
rest_path("users/me/muted_users/<int:muted_user_id>", POST=mute_user, DELETE=unmute_user),
# used to register for an event queue in tornado
rest_path("register", POST=(events_register_backend, {"allow_anonymous_user_web"})),
- # events -> zerver.tornado.views
- rest_path(
- "events",
- GET=(get_events, {"narrow_user_session_cache"}),
- DELETE=(cleanup_event_queue, {"narrow_user_session_cache"}),
- ),
# Used to generate a Zoom video call URL
rest_path("calls/zoom/create", POST=make_zoom_video_call),
# Used to generate a BigBlueButton video call URL
@@ -896,15 +882,18 @@ for app_name in settings.EXTRA_INSTALLED_APPS:
urls += [path("", include(f"{app_name}.urls"))]
i18n_urls += import_string(f"{app_name}.urls.i18n_urlpatterns")
-# Used internally for communication between command-line, tusd, Django,
-# and Tornado processes
+# Used internally for communication between tusd and Django,
urls += [
- path("api/internal/notify_tornado", notify),
path("api/internal/tusd", handle_tusd_hook),
- path("api/internal/web_reload_clients", web_reload_clients),
- path("api/v1/events/internal", get_events_internal),
]
+if settings.TEST_SUITE:
+ # Tests talk directly to Tornado APIs via the Django server, so
+ # include those URLs for convenience
+ urls += [
+ path("", include("zproject.tornado_urls")),
+ ]
+
# Python Social Auth
urls += [path("", include("social_django.urls", namespace="social"))]