Skip to content

Access and modify a ThreadLocal value from a WebGraphQlHandlerInterceptor #347

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

Closed
alejandropm-san opened this issue Apr 4, 2022 · 11 comments

Comments

@alejandropm-san
Copy link

Hi, I want to access the values saved in ReactorContext by my ThreadLocalAccessor impl in a WebGraphQlHandlerInterceptor, but ReactorContextManager save them inside a map using a private Key name and from what i could see there isn't a way for retrieving those values without declaring again the Key name in my project. Am I right? Is there going to be a better way to do this?

As i understood, ThreadLocalAccessor extract and restore Servlet context from/to DataFetchers, what about WebGraphQlHandlerInterceptor? Am i missing something?

Thanks in advance.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 4, 2022
@rstoyanchev
Copy link
Contributor

I presume that you want to access them as ThreadLocal values? Currently we don't restore context for interceptors but we can do that.

@rstoyanchev rstoyanchev added the status: waiting-for-feedback We need additional information before we can continue label Apr 12, 2022
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Apr 19, 2022
@alejandropm-san
Copy link
Author

Hi again, thank you for answering. I could review the code to better understand what was happening and explain it clearly:

I am using a WebGraphQlHandlerInterceptor to include some values in context with request/response info. For Reactive apps was everything ok, but for Servlet apps i was using ThreadLocal contexts and then notice these can be empty because of changing the execution thread.
Then i used Reactor Context to obtain ThreadLocal context values as i have a ThreadLocalAccessor to save them, but the access to these values is not as easy as expected because they are included in a Map with a key "...ReactorContextManager.THREAD_VALUES_ACCESSOR" which has private access scope. So i had to define this key in my interceptor and read it directly from Context.

I was expecting maybe a public static method in ReactorContextManager to extract that saved map, or even better to extract a value saved with a concrete ThreadLocalAccessor.
About your proposed solution, restoring context for interceptors sounds great but as i understand, thread may change in async interceptors, and saving/restoring contexts in every interceptor doesn't seem optimal, is it?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Apr 25, 2022
@rstoyanchev
Copy link
Contributor

I'm a bit unclear on what exactly you're trying to achieve.

It sounds to me like you have some Threadlocal values in Spring MVC and you want to use a WebGraphQlInterceptor to add them to the GraphQLContext? I wonder if you need to do that though since those ThreadLocal values are captured and will be restored for data fetchers to access.

If you could please take a step back and explain the use case and what your goal is, without referring to a solution and details like ThreadLocalAccessor or ReactorContext, which are not intended to be used directly by applications.

@rstoyanchev rstoyanchev added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels May 5, 2022
@alejandropm-san
Copy link
Author

I'm sorry for being unclear with my explanation.

I want to save something in a ThreadLocal object of mine based on the info of the WebGraphQlResponse (to check if there are errors or not), and for every response (that's important, as i need a solution for any mapping/dataFetcher).

I found that WebGraphQlInterceptor can be useful for this, but as i read here in Spring GraphQl doc an async interceptor in Spring MVC app could change the execution to a different thread, so i can't use ThreadLocal values directly.

Let me know if it's ok up to here or there is something wrong, thanks.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 9, 2022
@rstoyanchev
Copy link
Contributor

At what layer, level, or component is the ThreadLocal created?
At what layer, level, or component do you want to modify it?
At what layer, level, or component do you want to access it?

Please, be as specific as you can be, e.g. "Servlet filter on the way in", etc.

@rstoyanchev rstoyanchev added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels May 13, 2022
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label May 20, 2022
@alejandropm-san
Copy link
Author

We create the ThreadLocal object in a Servlet filter, we want to modify it in an WebGraphQlHandlerInterceptor based on GraphQl response, and want to access it in another Servlet filter before return statement.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels May 23, 2022
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Oct 24, 2022

Thanks for the clarification. That makes the scenario clear.

After #459, for the upcoming 1.1 release, you can register a ThreadLocalAccessor with a key, and the ThreadLocal value will be inserted into the Reactor context under that same key. So a WebGraphQlHandlerInterceptor can access this value from the Reactor context and it can also change the value in the Reactor context.

That said, currently we don't update the ThreadLocal after the call to the WebGraphQlHandlerInterceptor chain, which means that the Filter chain won't see any change on the way out. We could make that a feature, or alternatively, you could use WebGraphQlHandlerInterceptor to add HTTP response headers, if that's all you need to do from the Servlet Filter.

@rstoyanchev rstoyanchev added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Oct 24, 2022
@rstoyanchev rstoyanchev changed the title Use ReactorContext from ThreadLocalAccessor in WebGraphQlHandlerInterceptor Access and modify a ThreadLocal value from a WebGraphQlHandlerInterceptor Oct 24, 2022
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Oct 31, 2022
@spring-projects-issues
Copy link
Collaborator

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues added status: waiting-for-triage An issue we've not yet triaged and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels Nov 7, 2022
@rstoyanchev rstoyanchev closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2022
@rstoyanchev rstoyanchev removed the status: waiting-for-triage An issue we've not yet triaged label Nov 7, 2022
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

No branches or pull requests

3 participants