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

RFC/1684 Context objects #2063

Merged
merged 6 commits into from
Mar 17, 2021
Merged

RFC/1684 Context objects #2063

merged 6 commits into from
Mar 17, 2021

Conversation

ahopkins
Copy link
Member

@ahopkins ahopkins commented Mar 15, 2021

Resolves #1684

The proposal is to add a request.ctx like API to Sanic applications, blueprints, and connection objects.

A common practice is to add arbitrary objects on the Sanic app instance. This introduces a __fake_slots__ that is meant to be replaced by a real __slots__ in the future. In the meantime, we will check and raise a warning to direct the developer to using app.ctx.


Perhaps the more exciting introduction is request.connection.ctx.

This object will remain alive so long as a single transport is kept open. Since the KEEP_ALIVE will not close a transport layer, we can leverage that to place a context object on it.

A practical benefit of this might be only needing to check authentication or perform some expensive operation once for several calls.

@ahopkins ahopkins added this to the v21.6 milestone Mar 15, 2021
@codecov
Copy link

codecov bot commented Mar 15, 2021

Codecov Report

Merging #2063 (208e2d4) into master (01f238d) will increase coverage by 0.018%.
The diff coverage is 97.872%.

Impacted file tree graph

@@              Coverage Diff              @@
##            master     #2063       +/-   ##
=============================================
+ Coverage   92.199%   92.217%   +0.018%     
=============================================
  Files           38        38               
  Lines         3461      3482       +21     
  Branches       581       583        +2     
=============================================
+ Hits          3191      3211       +20     
  Misses         184       184               
- Partials        86        87        +1     
Impacted Files Coverage Δ
sanic/request.py 97.627% <83.333%> (-0.297%) ⬇️
sanic/app.py 92.276% <100.000%> (+0.047%) ⬆️
sanic/base.py 100.000% <100.000%> (ø)
sanic/blueprints.py 100.000% <100.000%> (ø)
sanic/server.py 88.021% <100.000%> (+0.063%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 01f238d...208e2d4. Read the comment docs.

@vltr
Copy link
Member

vltr commented Mar 17, 2021

Perhaps the more exciting introduction is request.connection.ctx.

This object will remain alive so long as a single transport is kept open. Since the KEEP_ALIVE will not close a transport layer, we can leverage that to place a context object on it.

A practical benefit of this might be only needing to check authentication or perform some expensive operation once for several calls.

This is awesome! More interestingly would be, in some cases (I can see myself doing that at least), making request.ctx to actually be request.connection.ctx ... I don't know if by some future option or even a custom implementation where needed. Either way, this is awesome.

sanic/app.py Show resolved Hide resolved
sanic/blueprints.py Outdated Show resolved Hide resolved
vltr
vltr previously approved these changes Mar 17, 2021
Copy link
Member

@vltr vltr left a comment

Choose a reason for hiding this comment

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

I made some comments to some parts, nothing too fancy neither blocking, so I'll leave them there in case you want but approve the PR already, as this seems awesome 😎

@ahopkins
Copy link
Member Author

The PR is ready, and we could potentially squeeze it in. My only concern is if there are any edge cases we are not considering where keep alive would apply, but that connection object is recreated. 🤔 Maybe we merge as "beta" feature and finalize it in 21.6?

@ahopkins ahopkins marked this pull request as ready for review March 17, 2021 18:25
@ahopkins ahopkins requested a review from a team as a code owner March 17, 2021 18:25
@ahopkins ahopkins requested a review from vltr March 17, 2021 18:31
Copy link
Member

@vltr vltr left a comment

Choose a reason for hiding this comment

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

awesome 😎

@ahopkins ahopkins merged commit 8a2ea62 into master Mar 17, 2021
@ahopkins ahopkins deleted the ctx branch March 17, 2021 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RFC: User context in app, connection and request
2 participants