zulip/zerver
Tim Abbott 1ea2f188ce tornado: Rewrite Django integration to duplicate less code.
Since essentially the first use of Tornado in Zulip, we've been
maintaining our Tornado+Django system, AsyncDjangoHandler, with
several hundred lines of Django code copied into it.

The goal for that code was simple: We wanted a way to use our Django
middleware (for code sharing reasons) inside a Tornado process (since
we wanted to use Tornado for our async events system).

As part of the Django 2.2.x upgrade, I looked at upgrading this
implementation to be based off modern Django, and it's definitely
possible to do that:
* Continue forking load_middleware to save response middleware.
* Continue manually running the Django response middleware.
* Continue working out a hack involving copying all of _get_response
  to change a couple lines allowing us our Tornado code to not
  actually return the Django HttpResponse so we can long-poll.  The
  previous hack of returning None stopped being viable with the Django 2.2
  MiddlewareMixin.__call__ implementation.

But I decided to take this opportunity to look at trying to avoid
copying material Django code, and there is a way to do it:

* Replace RespondAsynchronously with a response.asynchronous attribute
  on the HttpResponse; this allows Django to run its normal plumbing
  happily in a way that should be stable over time, and then we
  proceed to discard the response inside the Tornado `get()` method to
  implement long-polling.  (Better yet might be raising an
  exception?).  This lets us eliminate maintaining a patched copy of
  _get_response.

* Removing the @asynchronous decorator, which didn't add anything now
  that we only have one API endpoint backend (with two frontend call
  points) that could call into this.  Combined with the last bullet,
  this lets us remove a significant hack from our
  never_cache_responses function.

* Calling the normal Django `get_response` method from zulip_finish
  after creating a duplicate request to process, rather than writing
  totally custom code to do that.  This lets us eliminate maintaining
  a patched copy of Django's load_middleware.

* Adding detailed comments explaining how this is supposed to work,
  what problems we encounter, and how we solve various problems, which
  is critical to being able to modify this code in the future.

A key advantage of these changes is that the exact same code should
work on Django 1.11, Django 2.2, and Django 3.x, because we're no
longer copying large blocks of core Django code and thus should be
much less vulnerable to refactors.

There may be a modest performance downside, in that we now run both
request and response middleware twice when longpolling (once for the
request we discard).  We may be able to avoid the expensive part of
it, Zulip's own request/response middleware, with a bit of additional
custom code to save work for requests where we're planning to discard
the response.  Profiling will be important to understanding what's
worth doing here.
2020-02-13 16:13:11 -08:00
..
data_import emoji_codes: Replace JS module with JSON module. 2020-02-12 10:09:12 -08:00
lib tornado: Rewrite Django integration to duplicate less code. 2020-02-13 16:13:11 -08:00
management install: Don't create internal realm in the installation process. 2020-02-12 12:00:10 -08:00
migrations auth: Add support for GitLab authentication. 2020-02-11 13:54:17 -08:00
openapi api: Rename subject_links to topic_links. 2020-02-07 14:35:22 -08:00
templatetags openapi: Pass api_url to curl example generation. 2019-08-17 11:35:08 -07:00
tests install: Don't create internal realm in the installation process. 2020-02-12 12:00:10 -08:00
tornado tornado: Rewrite Django integration to duplicate less code. 2020-02-13 16:13:11 -08:00
views registration: Add support for mobile and desktop flows. 2020-02-12 11:22:16 -08:00
webhooks webhooks: Fix hellosign webhook. 2020-02-12 22:36:11 -08:00
worker slow queries: Use internal_send_stream_message(). 2020-02-11 12:20:54 -08:00
__init__.py
apps.py cleanup: Delete leading newlines. 2019-08-06 23:29:11 -07:00
context_processors.py login: Make authentication_methods data available to JavaScript. 2020-02-02 20:22:49 -08:00
decorator.py tornado: Rewrite Django integration to duplicate less code. 2020-02-13 16:13:11 -08:00
filters.py cleanup: Delete leading newlines. 2019-08-06 23:29:11 -07:00
forms.py rate_limiter: Limit the amount of password reset emails to one address. 2020-02-02 19:15:13 -08:00
logging_handlers.py version: Only let `git describe` match tags beginning with a digit. 2019-10-24 14:54:45 -07:00
middleware.py tornado: Rewrite Django integration to duplicate less code. 2020-02-13 16:13:11 -08:00
models.py auth: Add support for GitLab authentication. 2020-02-11 13:54:17 -08:00
signals.py onboarding: Use delivery_email in "new login" notifications. 2019-11-14 12:19:47 -08:00