So it sort of looks like a DSL for a variant of ThreadLocal that gives you an idea of the lifeline of variable instead of just being global. Apparently with virtual threads they are expecting an increase in the number of running (virtual) threads several orders of magnitude larger than what was previously seen and that with ThreadLocals would just be too much.
Which stores the object referenced by `context` to the ScopedValue container `CONTEXT` ^ for the duration of the thread running `Application.handle(request,response)`.
Not really sexy, but I understand how you might really benefit if you are processing lots of data, but for most enterprise CRUD apps this seems inapplicable.
^ I think this a "monad"? I'm not a functional programmer.
The problem with thread locals is that you need to copy the value into each platform thread. And the expectation of virtual threads is that any platform thread from the pool would end up running them.
This is super applicable to any system, enterprise or not. If the web framework is running on virtual threads as soon as you query the database your virtual thread will be parked and then another platform thread could potentially pick it up finish running it. Managing thread locals in this situation would be impossible, but you still need some mechanism to manage the global context. It’s really nothing to do with data volume.
ThreadLocal in Java understands the new lightweight "virtual" threads. If you write into a ThreadLocal, when you read it out on the same virtual thread (even if it's scheduled to a different platform thread) you'll get the same value back.
That absolutely isn't true at all. There's plenty of contextual information you might want to have saved in a scoped value that are currently saved in thread locals. The application I build currently is multi-tenanted. We store the current tenant, the current user, and other data in thread locals. If we were to switch to virtual threads, they would have to be changed to scoped values.
Implicit parameter smuggling by any mechanism is just too much magic in my experience. Golang's context.Context solves the same problem, but explicitly in a way that has been widely adopted through their language ecosystem.
That said - in Java I do think it would be near impossible for the ecosystem to adopt a similar mechanic, so from a practical standpoint this may be the only way to reliably propagate context.
I think it is due to java already featured the threadlocal mechanism so the ecosystem get used to it. I just recently rediscovering java (w/o framework) when virtual thread landed and I gravitated towards context passing. Even more recently I started reading golang code only to then seeing they were doing the same thing.
So it sort of looks like a DSL for a variant of ThreadLocal that gives you an idea of the lifeline of variable instead of just being global. Apparently with virtual threads they are expecting an increase in the number of running (virtual) threads several orders of magnitude larger than what was previously seen and that with ThreadLocals would just be too much.
So the have added this:
> ScopedVaule.where(CONTEXT, context).run(() -> Application.handle(request, response));
Which stores the object referenced by `context` to the ScopedValue container `CONTEXT` ^ for the duration of the thread running `Application.handle(request,response)`.
Not really sexy, but I understand how you might really benefit if you are processing lots of data, but for most enterprise CRUD apps this seems inapplicable.
^ I think this a "monad"? I'm not a functional programmer.
The problem with thread locals is that you need to copy the value into each platform thread. And the expectation of virtual threads is that any platform thread from the pool would end up running them.
This is super applicable to any system, enterprise or not. If the web framework is running on virtual threads as soon as you query the database your virtual thread will be parked and then another platform thread could potentially pick it up finish running it. Managing thread locals in this situation would be impossible, but you still need some mechanism to manage the global context. It’s really nothing to do with data volume.
ThreadLocal in Java understands the new lightweight "virtual" threads. If you write into a ThreadLocal, when you read it out on the same virtual thread (even if it's scheduled to a different platform thread) you'll get the same value back.
> If the web framework
So it's applicable to those writing the framework, not those using it.
That absolutely isn't true at all. There's plenty of contextual information you might want to have saved in a scoped value that are currently saved in thread locals. The application I build currently is multi-tenanted. We store the current tenant, the current user, and other data in thread locals. If we were to switch to virtual threads, they would have to be changed to scoped values.
Implicit parameter smuggling by any mechanism is just too much magic in my experience. Golang's context.Context solves the same problem, but explicitly in a way that has been widely adopted through their language ecosystem.
That said - in Java I do think it would be near impossible for the ecosystem to adopt a similar mechanic, so from a practical standpoint this may be the only way to reliably propagate context.
I think it is due to java already featured the threadlocal mechanism so the ecosystem get used to it. I just recently rediscovering java (w/o framework) when virtual thread landed and I gravitated towards context passing. Even more recently I started reading golang code only to then seeing they were doing the same thing.
Tramp parameters are a common occurrence, if not avoided with care.
I see value in this proposal
Interesting proposal, I can see myself using this.