Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update request-id recipe to use contextvars #2382

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

EricGoulart
Copy link

@EricGoulart EricGoulart commented Oct 20, 2024

Summary of Changes

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.

Related Issues

Closes #2260.

EricGoulart and others added 2 commits October 20, 2024 14:19
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 falconry#2260
Copy link

codecov bot commented Oct 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (ab2ce4c) to head (560fe6a).

Additional details and impacted files
@@             Coverage Diff             @@
##           master     #2382      +/-   ##
===========================================
+ Coverage   99.92%   100.00%   +0.07%     
===========================================
  Files          64        64              
  Lines        7726      7726              
  Branches     1071      1071              
===========================================
+ Hits         7720      7726       +6     
+ Misses          5         0       -5     
+ Partials        1         0       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@vytas7 vytas7 changed the title refactor: request-id to use contextvars refactor: update request-id recipe to use contextvars Oct 21, 2024
Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this improvement @EricGoulart, your code looks good. 👍
Could you also try adding a couple of tests to tests/test_recipes.py? (Check out other recipe tests for inspiration.)

@EricGoulart
Copy link
Author

Thanks for this improvement @EricGoulart, your code looks good. 👍 Could you also try adding a couple of tests to tests/test_recipes.py? (Check out other recipe tests for inspiration.)

Thank you for the feedback! I'll go ahead and add a couple of tests to tests/test_recipes.py as suggested.

vytas7 and others added 5 commits October 23, 2024 20:36
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 falconry#2260
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 falconry#2260
@vytas7
Copy link
Member

vytas7 commented Oct 31, 2024

Btw I don't know if you are participating in Hacktoberfest, but this is already good enough to qualify, I'll add the label just in case.

CaselIT
CaselIT previously approved these changes Oct 31, 2024
Copy link
Member

@CaselIT CaselIT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks! 💯

(The tests need a minor change before merging though.)

import pytest

from examples.recipes.request_id_middleware import RequestIDMiddleware
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a good idea to import this way, it may not work correctly when running pytest from another directory, for instance.

Let's import directly from the Python file, as it is done in other tests in this file.

return response.json['request_id']

loop = asyncio.get_event_loop()
request_id1, request_id2 = loop.run_until_complete(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These won't run asynchronously anyway, there is no real benefit vs just running make_request() twice.
(If we were testing an async app, we could achieve this with ASGIConductor.)

Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that something is still failing on import errors on your fork. Could you check?

@EricGoulart
Copy link
Author

Sure, I’ll check into it and make sure the import errors are resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update request-id recipe to use contextvars instead of threading.local()
3 participants