Propagate http abort/close from nginx to uwsgi / Django

后端 未结 2 1248
后悔当初
后悔当初 2021-02-02 10:53

I have a Django application web application, and I was wondering if it was possible to have nginx propagate the abort/close to uwsgi/Django.

Basically I know that nginx

2条回答
  •  不知归路
    2021-02-02 11:55

    Now obviously there may be certain pages, where I don't want the request to be prematurely aborted for any reason.

    This is precisely the problem behind taking this one way or the other.

    • Obviously, you may not want to continue spending system resources processing a connection that has since been aborted, e.g., an expensive search operation.

    • But then maybe the connection was important enough that it still has to be processed even if the client has disconnected.

      • E.g., the very same expensive search operation, but one that's actually not client-specific, and will be cached by nginx for all subsequent clients, too.

      • Or maybe an operation that modifies the state of your application — you clearly wouldn't want to have your application to have an inconsistent state!

    As mentioned, the problem is with uWSGI, not with NGINX. However, you cannot have uWSGI automatically decide what was your intention, without you revealing such intention yourself to uWSGI.

    And how exactly will you reveal your intention in your code? A whole bunch of programming languages don't really support multithreaded and/or asynchronous programming models, which makes it entirely non-trivial to cancel operations.

    As such, there is no magic solution here. Even the concurrency-friendly programming languages like Golang are having issues around the WithCancel context — you may have to pass it around in every function call that could possibly block, making the code very ugly.

    Are you already doing the above context passing in Django? If not, then the solution is ugly but very simple — any time you can clearly abort the request, check whether the client is still connected with uwsgi.is_connected(uwsgi.connection_fd()):

    • http://lists.unbit.it/pipermail/uwsgi/2013-February/005362.html

提交回复
热议问题