You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Well its been fun. And by fun I mean: not so fun. We've tried to create a simple method for getting at contextual information no matter where your code is executing. We did this by depending on a sophisticated module called continuation-local-storage .
Continuation-local storage works like thread-local storage in threaded programming, but is based on chains of Node-style callbacks instead of threads.
Someday there will be a more reliable mechanism than the userland cls module, and we can re-add first class support for getCurrentContext(). But until then I am proposing we deprecate this method with the standard warning message.
What does this mean for users of getCurrentContext()? You will get an annoying message (configurable) every time you use the method. We will still support it, and we won't break semver. But we strongly encourage you to refactor.
For an immediate work around, take a look at #1495. A more sustainable workaround will be added as a feature to the framework.
I'm concerned about this, as I have been using getCurrentContext extensively. And without any issues thusfar. I'm on mobile right now, so I don't have access to the whole discussion(s) - IIRC mainly regarding unreliable availability on different platforms - but what's the reasoning here?
Can we have a flag to silence the deprecation warning? ;-)
I abandoned getCurrentContext pretty quickly since it led to unpredictable behavior. Notably, retrieving the Access Token using something like getCurrentContext().get('accessToken') seemed to work at random, if at all, so we worked around it by getting the accessToken from the request instead. Apart from that, we have not needed it anywhere.
I was thinking about the problems of getCurrentContext we are experiencing. It seems to me that most of the issues are caused by the fact that any connector using connection pool must be CLS aware and include few lines of code to switch to the right CLS context. I am wondering we can add this small piece into loopback-datasource-juggler, so that we don't depend on connector implementation anymore?
An example to illustrate my point:
DAO.create=function(data,options,cb){// setup instance, call hooks, etc.clsCtx=// save current CLS context;connector.create(/*args*/,function(err,result){// restore CLS context from clsCtx// continue as before});};
If that's not a viable approach, then I support @fabien's request for a feature flag. This flag can have three states:
undefined - print a warning (default)
true - getCurrentContext is enabled (no warning is printed)
false - getCurrentContext is disabled (no warning is printed)
@bajtos Can you or somebody to do a quick PoC to verify your theory?
I'm willing to advocate explicit context passing as the preferred way as it is the only reliable/performant way. It's fine with me to keep the getCurrentContext() if @bajtos's idea works.
I don't think fixing the connector boundary and lack of CLS support in the connectors is the only issue. I've seen issues from our usage of async (which were eventually fixed).
I'm OK with a flag too. Opt-in and with a bit of warning in the documentation. As far as regular API - I still think we should deprecate it.
loopback.getCurrentContext() has given lot of hard time while developing unit test cases so far. We had to setup dummy context before executing tests.
However today we faced one major issue where loopback.getCurrentContext () is returned as null when a async callback function gets called.
We are making an external HTTP call from within loopback API in async manner. loopback.getCurrnetContext () is consistently coming as null in the callback made by that http request. The loopback api call is returned by that time.
Is there any way to completely avoid loopback.getCurrentContext() and still make context information available everywhere without explicitly passing it around?
Don't think so.
You can get the context everywhere except in Model.observes callbacks. Our
preference has been to avoid doing anything that needs context in
Model.observes callbacks and get by without getCurrentContext or passing
context around. We have found that we can usually get by in this way,
sometimes using beforeRemote/afterRemote as a substitute.
loopback.getCurrentContext() has given lot of hard time while developing
unit test cases so far. We had to setup dummy context before executing
tests.
However today we faced one major issue where loopback.getCurrentContext ()
is returned as null when a async callback function gets called.
We are making an external HTTP call from within loopback API in async
manner. loopback.getCurrnetContext () is consistently coming as null in the
callback made by that http request. The loopback api call is returned by
that time.
Is there any way to completely avoid loopback.getCurrentContext() and
still make context information available everywhere without explicitly
passing it around?
—
Reply to this email directly or view it on GitHub #1676 (comment)
.
It looks like setImmediate breaks cls, therefore any modules using setImmediate are going to break current-context feature. An example: express-session, see strongloop/loopback-context#6
@bajtos I was just yesterday reading through the CLS Issues on github (like this: othiym23/node-continuation-local-storage#59) to get a feel for what the current status is on solving problems like these. It seems like the "solution" is to continue monkey patching libraries that mess up CLS and hope for something native to Javascript which enables this sort of functionality. The only native work is still in early draft (Zones: https://github.com/domenic/zones). I left feeling like CLS cannot be relied upon.
With that in mind, what's Loopback's plan here? This seems like a major pain point for the framework. The Loopback Context is currently dangerously unreliable, and moving away from CLS would be a huge undertaking. However, not having a good way to pass state becomes a huge problem at times. We can explicitly pass things via options in a lot of cases, but it's not universal and it doesn't cover built-ins.
@fabien how can i get the current connected user inside a model using your solution. Can you please share an example. Do you have any other solution to get current user in models.
@fabien, thanks for sharing this, it seems we are a number of folks out there currently building custom solutions to solve context-passing related issues...
I like the general idea of your solution (RemoteCtx setters & getters, user-tempering proofness, possibility to setup in model .json) although after testing it the main cons i'd point out that would not make it a generic enough solution imo (some of these are probably out of your code reach, and more to impute to loopback inner logics) would be that the remotingContext object is not consistent in the various middleware stages with regard to the ctx object of that stage, for example:
role-resolving (Role.registerResolver(name, function (roleName,ctx, cb) {})) : the remotingContext available in ctx object at that stage is inconsistent with the remotingContext from your solution. This is by construction since your custom remotingContext object is then attached after, via a before-remote hook, as ctx.remotingContext.args.options.remotingContext: we should find a solution that enables the user to inject some params in ctx.remotingContext during role-resolving that are available at later stages in a generic way. Exemple: if i attach my own params to ctx.remotingContext.args.options, they are not overwritten by your solution, but obviously they are not available through the RemoteCtx.prototype.get getter right out the box. I can think of a mapping function using the contextProperties array you provide as a starting point, but if we want to avoid it becoming even more messy, we should probably specify where user should inject this type of data at this stage.
before-remote hook (Model.beforeRemote ('findById', function (ctx, instance, cb) {})): remotingContext is not available in ctx object (given that remoteObject.before hooks take place after Model.beforeRemote hook, see my solution in next post
after-remote hook (Model.afterRemote ('findById', function (ctx, instance, cb) {})): remotingContext is available as ctx.remoteContext
operation hooks: (Model.observe ('access', function (ctx, cb) {}) )remotingContext is available as ctx.options.remoteContext
Maybe we could we think of a generic nested ctx object format with generic getters/setters methods (as you provided, great) that the user could customize to its tastes by setting any property required?
Also: do we have a way to inject the RemoteCtx class at a sooner stage than before remote? this would help solving the issues with param injection at role resolving stage.
++ We could also argue the concept of method and contextProperties whitelist, vs. blacklists, as things may break with new loopback releases (for exemple new method names in loopback 3)
ps. really smart the optional per-model extractRemotingOptions method
it fixes a few bits to make it work in a more code-base agnostic env
it solves the issue with ctx.remotingContext not being available in Model.beforeRemote hooks
it leverages on a data object in http context to pass data from earlier stages (such as role-resolving) to the new remotingContext object available in further hooks and in remote method code
Reacted by Valentin Klinghammer and Joseph Persie III
Hey @ebarault, thanks for the gist! However, my Model.beforeRemote hooks still are being executed before the context beforeRemote hook. I have the same problem with @fabien's gist as well.
I've got your gist in a boot script and it's being run before all other boot scripts. The Model.beforeRemote is in the model's javascript definition file under common/models/MyModel.js
Any idea how to get the context hook running first?
@bostondv : so yes, I've no clean way to do this right now (there might be a better way, but i took the shortest track for my project to move on). So here it is
in the bootscript (cf. gist), i register the beforeRemote for ctx injection on ** :
for (let key in app.models) {
app.models[key].beforeRemote('**', function (ctx, instance, next) {
inject(ctx, next);
});
}
now, you need to know that the ** beforeRemote hooks are actually executed after all per-method beforeRemote registered hooks. So first, you need to register a similar ** hook in your Models if you want to get a chance for it to be executed after the ctx injection hook.
secondly, you still need to make sure that the actual registration of the ** hook in your model is done after the ctx injection one, otherwise your hook might still be executed before the ctx injection one. Depending on your application it can be done in a number of ways with emitted events so you wait for the models to be fully loaded before registering your beforeRemote hooks. Usually you can get it done with:
app.on('started', function () {
myModel.beforeRemote('**', function (ctx, instance, next) {
next();
});
});
if you don't do this the result can be random as the user defined Models are loaded in lexicographical order
Remember: you can still discriminate the method called in the beforeRemote('**',...) hook by inspecting the method name with the ctx.mehod.name property
I was having issues with remote context a while back, especially after wrapping a remote method call in a third party library callback (particularly jsdom). I have been trying to find some practical working example of this aside from the examples that have been provided in this thread. Is it safe to say that https://github.com/snowyu/loopback-component-remote-ctx.js should pretty much address this issue? If anyone knows of a non-coffee script package, references would be greatly appreciated.
@nidhhoggr : i just went through the code from @snowyu component. It is essentially a port to coffee script of one of the general idea developed in this thread (and this one) about passing the context as an argument injected in the methods definitions (options argument) : so yes it should work, though the method to access the remoteCtx may not be consistent whether you access it from methods code, or from remote methods hooks or from operation hooks
If you want a pure node implem which is also making sure the context is available the same way in methods definition, remote methods hooks and operation hooks, please see my revision of @fabien's optionnated implementation here
@ebarault: I can confirm @snowyu implementation is working and your snippet does give me a better understanding of what is happening with method injection etc. I see that your RemoteContext has a prototype method for setting the access token. Ideally would this be set in some middleware? The legacy implementation using getCurrentContext looked something like this:
app.use(loopback.context());
app.use(loopback.token());
app.use(function (req, res, next) {
var accessToken = req.query.access_token || false;
if (!accessToken) accessToken = req.headers.authorization || false;
if(accessToken) {
var loopbackContext = loopback.getCurrentContext();
if (loopbackContext) {
console.log(accessToken);
loopbackContext.set('accessToken', accessToken);
}
}
return next();
});
So my question, is there a way to set the accessToken of the remote context that is injected into each of the method with middleware as the previous implementation is doing above, or is it best to just access the accesstoken from the context request headers within that method?
@nidhhoggr : there's actually a method for getting the accessToken, not for setting it.
RemoteCtx.prototype.getAccessToken = function () {
return this.req && this.req.accessToken;
};
the accessToken is always available in the remotingContext, in the req object, so you can always get it from the helper remoteCtx.getAccessToken() provided in the snippet
Hello, the official implementation of "options" injection has been landed via #3023 (3.x) and #3048 (2.x, behind a feature flag, disabled by default for backwards compatibility) and released in 2.37.0 and 3.2.0. The documentation will be updated soon, see ttps://github.com/loopbackio/loopback.io/pull/227.
Activity
fabien commentedon Sep 16, 2015
I'm concerned about this, as I have been using getCurrentContext extensively. And without any issues thusfar. I'm on mobile right now, so I don't have access to the whole discussion(s) - IIRC mainly regarding unreliable availability on different platforms - but what's the reasoning here?
Can we have a flag to silence the deprecation warning? ;-)
csvan commentedon Sep 17, 2015
I abandoned getCurrentContext pretty quickly since it led to unpredictable behavior. Notably, retrieving the Access Token using something like
getCurrentContext().get('accessToken')seemed to work at random, if at all, so we worked around it by getting the accessToken from the request instead. Apart from that, we have not needed it anywhere.bajtos commentedon Sep 18, 2015
I was thinking about the problems of getCurrentContext we are experiencing. It seems to me that most of the issues are caused by the fact that any connector using connection pool must be CLS aware and include few lines of code to switch to the right CLS context. I am wondering we can add this small piece into loopback-datasource-juggler, so that we don't depend on connector implementation anymore?
An example to illustrate my point:
If that's not a viable approach, then I support @fabien's request for a feature flag. This flag can have three states:
undefined- print a warning (default)true- getCurrentContext is enabled (no warning is printed)false- getCurrentContext is disabled (no warning is printed)Thoughts?
raymondfeng commentedon Sep 18, 2015
@bajtos Can you or somebody to do a quick PoC to verify your theory?
I'm willing to advocate explicit context passing as the preferred way as it is the only reliable/performant way. It's fine with me to keep the getCurrentContext() if @bajtos's idea works.
ritch commentedon Sep 22, 2015
I don't think fixing the connector boundary and lack of CLS support in the connectors is the only issue. I've seen issues from our usage of
async(which were eventually fixed).I'm OK with a flag too. Opt-in and with a bit of warning in the documentation. As far as regular API - I still think we should deprecate it.
The states LGTM.
fabien commentedon Dec 3, 2015
+1 for flag, please
sachinhub commentedon Dec 23, 2015
loopback.getCurrentContext() has given lot of hard time while developing unit test cases so far. We had to setup dummy context before executing tests.
However today we faced one major issue where loopback.getCurrentContext () is returned as null when a async callback function gets called.
We are making an external HTTP call from within loopback API in async manner. loopback.getCurrnetContext () is consistently coming as null in the callback made by that http request. The loopback api call is returned by that time.
Is there any way to completely avoid loopback.getCurrentContext() and still make context information available everywhere without explicitly passing it around?
digitalsadhu commentedon Dec 23, 2015
Don't think so.
You can get the context everywhere except in Model.observes callbacks. Our
preference has been to avoid doing anything that needs context in
Model.observes callbacks and get by without getCurrentContext or passing
context around. We have found that we can usually get by in this way,
sometimes using beforeRemote/afterRemote as a substitute.
On Wed, 23 Dec 2015 at 17:42, Sachin Mane notifications@github.com wrote:
violet-day commentedon Mar 17, 2016
Any progress about
cls?get rid of loopback.getCurrentContext() as it is not reliable, instea…
bajtos commentedon Aug 17, 2016
Another bug in node-continuation-local-storage:
It looks like
setImmediatebreaks cls, therefore any modules usingsetImmediateare going to break current-context feature. An example: express-session, see strongloop/loopback-context#6doublemarked commentedon Aug 17, 2016
@bajtos I was just yesterday reading through the CLS Issues on github (like this: othiym23/node-continuation-local-storage#59) to get a feel for what the current status is on solving problems like these. It seems like the "solution" is to continue monkey patching libraries that mess up CLS and hope for something native to Javascript which enables this sort of functionality. The only native work is still in early draft (Zones: https://github.com/domenic/zones). I left feeling like CLS cannot be relied upon.
With that in mind, what's Loopback's plan here? This seems like a major pain point for the framework. The Loopback Context is currently dangerously unreliable, and moving away from CLS would be a huge undertaking. However, not having a good way to pass state becomes a huge problem at times. We can explicitly pass things via
optionsin a lot of cases, but it's not universal and it doesn't cover built-ins.What's the plan?
23 remaining items
yagobski commentedon Nov 3, 2016
@fabien how can i get the current connected user inside a model using your solution. Can you please share an example. Do you have any other solution to get current user in models.
Thanks
ebarault commentedon Nov 10, 2016
@fabien, thanks for sharing this, it seems we are a number of folks out there currently building custom solutions to solve context-passing related issues...
I like the general idea of your solution (
RemoteCtxsetters & getters, user-tempering proofness, possibility to setup in model .json) although after testing it the main cons i'd point out that would not make it a generic enough solution imo (some of these are probably out of your code reach, and more to impute to loopback inner logics) would be that theremotingContextobject is not consistent in the various middleware stages with regard to thectxobject of that stage, for example:Role.registerResolver(name, function (roleName,ctx, cb) {})) : theremotingContextavailable inctxobject at that stage is inconsistent with the remotingContext from your solution. This is by construction since your customremotingContextobject is then attached after, via a before-remote hook, asctx.remotingContext.args.options.remotingContext: we should find a solution that enables the user to inject some params inctx.remotingContextduring role-resolving that are available at later stages in a generic way. Exemple: if i attach my own params toctx.remotingContext.args.options, they are not overwritten by your solution, but obviously they are not available through theRemoteCtx.prototype.getgetter right out the box. I can think of a mapping function using thecontextPropertiesarray you provide as a starting point, but if we want to avoid it becoming even more messy, we should probably specify where user should inject this type of data at this stage.Model.beforeRemote ('findById', function (ctx, instance, cb) {})):remotingContextis not available inctxobject (given thatremoteObject.beforehooks take place afterModel.beforeRemote hook, see my solution in next postModel.afterRemote ('findById', function (ctx, instance, cb) {})):remotingContextis available asctx.remoteContextModel.observe ('access', function (ctx, cb) {}))remotingContextis available asctx.options.remoteContextMaybe we could we think of a generic nested ctx object format with generic getters/setters methods (as you provided, great) that the user could customize to its tastes by setting any property required?
Also: do we have a way to inject the
RemoteCtxclass at a sooner stage thanbefore remote? this would help solving the issues with param injection at role resolving stage.++ We could also argue the concept of method and contextProperties whitelist, vs. blacklists, as things may break with new loopback releases (for exemple new method names in loopback 3)
ps. really smart the optional per-model
extractRemotingOptionsmethodebarault commentedon Nov 12, 2016
Please, see my revised version of @fabien 's gist.
https://gist.github.com/ebarault/fdc39c5d07cc40f08664256f9e00905a
in a nutshell
ctx.remotingContextnot being available in Model.beforeRemote hooksdataobject in http context to pass data from earlier stages (such as role-resolving) to the newremotingContextobject available in further hooks and in remote method codebostondv commentedon Nov 16, 2016
Hey @ebarault, thanks for the gist! However, my
Model.beforeRemotehooks still are being executed before the contextbeforeRemotehook. I have the same problem with @fabien's gist as well.I've got your gist in a boot script and it's being run before all other boot scripts. The
Model.beforeRemoteis in the model's javascript definition file undercommon/models/MyModel.jsAny idea how to get the context hook running first?
ebarault commentedon Nov 16, 2016
@bostondv : so yes, I've no clean way to do this right now (there might be a better way, but i took the shortest track for my project to move on). So here it is
in the bootscript (cf. gist), i register the beforeRemote for ctx injection on ** :
now, you need to know that the ** beforeRemote hooks are actually executed after all per-method beforeRemote registered hooks. So first, you need to register a similar ** hook in your Models if you want to get a chance for it to be executed after the ctx injection hook.
secondly, you still need to make sure that the actual registration of the ** hook in your model is done after the ctx injection one, otherwise your hook might still be executed before the ctx injection one. Depending on your application it can be done in a number of ways with emitted events so you wait for the models to be fully loaded before registering your beforeRemote hooks. Usually you can get it done with:
if you don't do this the result can be random as the user defined Models are loaded in lexicographical order
Remember: you can still discriminate the method called in the
beforeRemote('**',...)hook by inspecting the method name with thectx.mehod.namepropertybostondv commentedon Nov 16, 2016
@ebarault thanks so much, that's exactly what I needed!
ebarault commentedon Nov 17, 2016
all: some archeology to excavate an old debate: #1362 around the ctx propagation question
nidhhoggr commentedon Dec 5, 2016
I was having issues with remote context a while back, especially after wrapping a remote method call in a third party library callback (particularly jsdom). I have been trying to find some practical working example of this aside from the examples that have been provided in this thread. Is it safe to say that https://github.com/snowyu/loopback-component-remote-ctx.js should pretty much address this issue? If anyone knows of a non-coffee script package, references would be greatly appreciated.
ebarault commentedon Dec 5, 2016
@nidhhoggr : i just went through the code from @snowyu component. It is essentially a port to coffee script of one of the general idea developed in this thread (and this one) about passing the context as an argument injected in the methods definitions (
optionsargument) : so yes it should work, though the method to access the remoteCtx may not be consistent whether you access it from methods code, or from remote methods hooks or from operation hooksIf you want a pure node implem which is also making sure the context is available the same way in methods definition, remote methods hooks and operation hooks, please see my revision of @fabien's optionnated implementation here
nidhhoggr commentedon Dec 5, 2016
@ebarault: I can confirm @snowyu implementation is working and your snippet does give me a better understanding of what is happening with method injection etc. I see that your RemoteContext has a prototype method for setting the access token. Ideally would this be set in some middleware? The legacy implementation using getCurrentContext looked something like this:
So my question, is there a way to set the accessToken of the remote context that is injected into each of the method with middleware as the previous implementation is doing above, or is it best to just access the accesstoken from the context request headers within that method?
ebarault commentedon Dec 5, 2016
@nidhhoggr : there's actually a method for getting the accessToken, not for setting it.
the accessToken is always available in the remotingContext, in the
reqobject, so you can always get it from the helperremoteCtx.getAccessToken()provided in the snippetnidhhoggr commentedon Dec 6, 2016
@ebarault Okay I see now, so it's being set from the items in contextProperties array. Thanks you Sir!
bajtos commentedon Jan 9, 2017
Hello, the official implementation of "options" injection has been landed via #3023 (3.x) and #3048 (2.x, behind a feature flag, disabled by default for backwards compatibility) and released in 2.37.0 and 3.2.0. The documentation will be updated soon, see ttps://github.com/loopbackio/loopback.io/pull/227.
I am closing this issue as done.