-
-
Notifications
You must be signed in to change notification settings - Fork 948
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update request-id recipe to use contextvars (#2404)
* refactor: request-id to use contextvars Migrate the request-id handling from threading.local() to contextvars to improve compatibility with async coroutines and avoid issues with threading. This change ensures that request-id is properly tracked in asynchronous environments, providing more robust handling in both sync and async contexts. Previously, threading.local() was used, which does not handle coroutines effectively. By using contextvars, we ensure that the request-id remains consistent across async calls. Closes #2260 * adding test to test_recipes * test(request_id): add tests for request id middleware context handling Add tests to verify that request_id is unique per request and correctly set in response headers. Tests include checks for isolation in async calls and persistence in synchronous requests. Related to issue #2260 * test(request_id): add tests for request id middleware context handling Add tests to verify that request_id is unique per request and correctly set in response headers. Tests include checks for isolation in async calls and persistence in synchronous requests. Related to issue #2260 * minor changes in tests * fix(recipes): fix request-id recipe tests --------- Co-authored-by: Vytautas Liuolia <[email protected]>
- Loading branch information
1 parent
11076b8
commit 77d5e63
Showing
5 changed files
with
60 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
# context.py | ||
|
||
import threading | ||
import contextvars | ||
|
||
|
||
class _Context: | ||
def __init__(self): | ||
self._thread_local = threading.local() | ||
self._request_id_var = contextvars.ContextVar('request_id', default=None) | ||
|
||
@property | ||
def request_id(self): | ||
return getattr(self._thread_local, 'request_id', None) | ||
return self._request_id_var.get() | ||
|
||
@request_id.setter | ||
def request_id(self, value): | ||
self._thread_local.request_id = value | ||
self._request_id_var.set(value) | ||
|
||
|
||
ctx = _Context() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters