falconry / falcon

The no-magic web data plane API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.
https://falcon.readthedocs.io/en/stable/
Apache License 2.0
9.53k stars 944 forks source link

Update request-id recipe to use `contextvars` instead of `threading.local()` #2260

Open vytas7 opened 3 months ago

vytas7 commented 3 months ago

contextvars is a newer module in the stdlib (3.7+) that is more advanced than theading.local() as it not only understand threads, but also asyncio coroutines, etc.

Update the Request ID Logging recipe to use contextvars.

Edit: also add tests for this recipe.

CaselIT commented 3 months ago

I don't think there is any need to use threadlocal or contextvar. just use req or resp context.

re-reading all that page it proposes several approaches. so yes, context var is better than threadlocal

vytas7 commented 3 months ago

Indeed, using global thread-locals is usually not a very bright idea and it leads to a spaghetti code base. But sometimes, particularly when working with a code base migrated from another framework that builds on this paradigm, or when very deep inside nested code, it might be a useful escape hatch.

EricGoulart commented 1 month ago

Hello, I would like to work on this issue. Please let me know if there is any information that I should know before proceeding. Thank you.

vytas7 commented 1 month ago

Hi @EricGoulart! We are glad to hear, sure, go ahead! You can skim through our docs on how to contribute to Falcon.

Regarding the issue itself, I hope the description is clear enough, but just ask here or on Gitter if you run into any problems. The recipe that needs to be updated is this file: docs/user/recipes/request-id.rst (but it also includes snippets from small Python files).

You can render out the docs with tox -e docs. Then open docs/_build/html/user/recipes/request-id.html in your browser.

EricGoulart commented 4 days ago

Hello, just wanto to say sorry about that, It`s my first time contribuiting to a public repository and I am making some mistakes.