-
Notifications
You must be signed in to change notification settings - Fork 309
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
Define a CURRENT_JUPYTER_HANDLER context var #1251
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #1251 +/- ##
==========================================
+ Coverage 80.49% 80.51% +0.01%
==========================================
Files 68 69 +1
Lines 8270 8299 +29
Branches 1601 1607 +6
==========================================
+ Hits 6657 6682 +25
- Misses 1191 1194 +3
- Partials 422 423 +1
... and 2 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report in Codecov by Sentry. |
440b2f3
to
e9084b3
Compare
This is great @Zsailer - thank you! I was thinking this would be a bit more generic where the current handler would be one item available and with which managers (and anything else) could add their own items into. Perhaps we could have For activity that is generated from within the server (like periodic callbacks), they may want to set some state as well since the |
Hi @Zsailer - I've gone ahead and pushed the I had originally implemented it as a dictionary of name/contextVar pairs but because the dictionary instance itself should be context-sensitive, it seemed a little redundant to have each variable (those desired by the application and the mapping itself) manifested as context variables. We can revisit that if necessary - the tests passed either way. I did leave a TODO about what to return from I'm happy to circle back to the main description and title to reflect these changes but thought I should first wait to make sure we're on the same page here. |
e.g. Can this be used to implement a HTTP request counter per user (USER_A has hit the endpoints 5 times since the start of his/her session which is 2 days ago, while USER_B has hit the endpoints 45643 times since the start of his/her session which is 1 hour ago). |
Hi @echarles.
No. This is purely relative to the current request and is essentially user-agnostic (although user-specific by definition). It's tantamount to thread-local storage. If current session behavior doesn't account for this, I think it would make sense to extend |
I know we briefly discussed this, but thinking about this more— I find it a bit confusing that the contextvar is in the "sessions" module. Annoyingly, jupyter_server uses "sessions" to mean a few different things. The sessions module+service, however, is specifically about notebook-kernel mapping, right? It's not related to anything about the user, request, or context. That said, I don't know a better place for this. Maybe we need a "session" outside of "services" module? Or we can name it something else? Either way, I think the mixing of what we mean by "session" in this PR is adding the confusion. |
Hmm - ok. I was thinking this would be associated with the longer-term stuff we wanted to do with session - essentially what @echarles was asking about here. I'm okay moving this but don't have anything I believe to be a better location/name. I would, though, like to see the server become more session-centric and really believe that will be necessary when moving forward in multi-user/tenancy. |
Co-authored-by: Kevin Bates <[email protected]>
Maybe call it |
I like this. And we should put it at the top level, next to |
But this is available to any application and services (sans REST API). As a result, I don't think
What does this mean in this context? |
I meant framework in terms of |
Thanks. The file is framework-agnostic. Does its location in the package change that designation?
If we were to place this at the top level (and not in |
I think |
BTW, I didn't say this earlier but @kevin-bates this CallContext object is sweet! Well done! 🚀 |
Yeah, that sounds good. As this evolves we might want to move it. Hmm, in that case, would it make sense to add an entry into https://github.com/jupyter-server/jupyter_server/blob/main/jupyter_server/__init__.py so folks wouldn't need to adjust import statements? from .base.call_context import CallContext and consumer imports would be: from jupyter_server import CallContext Or is this bad practice? |
Yeah, I think that seems reasonable enough to me. |
OK - I'll go ahead and push these relocation changes... |
This LGTM! Thanks, @kevin-bates |
I like it. It is less confusing as not related to any session (nor http, nor kernel) |
@kevin-bates I'll let you do the honors of merging, since I opened the initial PR 😎 |
I found some bugs about CallContext when I changed this jupyter to suppor multi-user/tenancy ,
` copy_context is a shallow copy,_name_value_map is a dict,so when multi user visite jupyter,if we record some user infomation in , it will casue context values bleed |
@dualc would you like to open an issue for this so we can track and test? I'm happy to do it to if you prefer. |
Fixes #1250
This PR defines a
CURRENT_JUPYTER_HANDLER
context variable that provides access to the current JupyterHandler context from within any manager, extension, etc.This is a super simple way to enable e.g. manager methods to access information/context about the server request that called the method. The most compelling use-case might be if you want to get the
current_user
from within a manager's method.To do this, simply import the context variable and call
.get()
. For example, if you want to know the current_user within thestart_kernel
method of the MappingKernelManager, it might look like:I've included a fairly round about unit test to demonstrate that the variable remains isolated per request.