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

Question: What can we store in the RequestStore? #61

Open
randyv12 opened this issue Jun 7, 2018 · 1 comment
Open

Question: What can we store in the RequestStore? #61

randyv12 opened this issue Jun 7, 2018 · 1 comment

Comments

@randyv12
Copy link

randyv12 commented Jun 7, 2018

What's a good pattern for storing objects in the RequestStore?

I'm currently investigating server slowness for one of the apps I'm working on and one of the main causes could possibly be the incorrect usage of RequestStore. It's just a matter of fixing things, but I'm pretty sure it's not a good idea to store references to things such as:

Controller

Request Context - get_request_context(request)

Aside from the controller storing, breaks MVC. It seems like it can propagate issues beyond the controller lifecycle. Being it, hard to unit test -- maybe. But I'd like to understand what's happening under the hood, aside from my app, the way thread local storage is used when the app is running on a server such as passenger.

Below this line, I might write things I don't truly understand, so please correct me if I'm wrong.

The server is running on passenger, and some searching have sent me to

#39

  • I understood the part where if a main thread spawns threads, then the children threads can't access TLS from the main thread

https://stackoverflow.com/questions/7896298/safety-of-thread-current-usage-in-rails

  • Just some more context that allowed me to understand thread usage in rails

I want to take a step back, just to raise the question that, is this even a good way to use RequestStore?

Storing other things like a User object makes sense, but storing big things might cause trouble for the ruby GC. And, does it lead to performance issues (high CPU usage) from garbage collection on a huge object?

Thank you so much, if you could help. Like I said, I might not understand everything yet but I really appreciate any input.

@rud
Copy link

rud commented Jun 17, 2018

Somebody please correct me if I'm wrong. Storing a "large" object in RequestStore should not have any impact on performance. The controller and request object instances are both going to be around for the duration of the request, and storing another reference is no problem.

RequestStore is handy when you have something you need across layers without passing a lot of explicit context around. For instance, maybe you need access to current_user in a model to do proper audit logging conveniently. That's at least one use-case I've come across.

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

2 participants