Repository navigation
Description
Now that we're all ES6 for Sequelize v4, the door is open to be using coroutines. Personally, I think this is a great thing - it'll simplify Sequelize's codebase a lot.
However, I don't think we're quite ready to do that - I'd like to conclude my research on bluebird and CLS, to make absolutely sure that introducing coroutines won't break CLS support, before we take the plunge.
But when we do start using coroutines, it will be a semver major change, due to a change in the way errors are handled.
Error handling
At present some Sequelize async methods throw synchronously if the arguments they are passed are illegal in some way. Once those methods become coroutines, this wouldn't be the case any more - they'll return rejected promises instead.
How it is now:
Model.findAll = function(options) {
if (!options) throw new Error('You must provide options!');
}
// Model.findAll() throws synchronously
try {
Model.findAll();
} catch (err) {
console.log('It throws!');
}How it will be with coroutines:
Model.findAll = co.wrap( function*(options) {
if (!options) throw new Error('You must provide options!');
} );
// Model.findAll() returns a rejected promise
Model.findAll().catch( function(err) {
console.log('It returns a rejected promise');
} );A proposal to "prepare the way"
I would like to see coroutines in Sequelize as soon as possible, but I don't want to hold up the release of v4 until the CLS issues are resolved.
I propose that we "prepare the way" now by converting all Sequelize methods from throwing synchronously to returning rejected promises.
This isn't hard to do using Promise.method():
Model.findAll = Promise.method( function(options) {
if (!options) throw new Error('You must provide options!');
} );
// Model.findAll() returns a rejected promise
Model.findAll().catch( function(err) {
console.log('It returns a rejected promise');
} );This way:
- The whole API switches away from sync throwing in one go.
- It can all be done as a single change in Sequelize v4.0.0.
- Methods can then be refactored with coroutines as and when we feel like it, without creating any further breaking changes.
- We won't have to wait until Sequelize v5 to start using coroutines.
What do you think?
Activity
mickhansen commentedon Jun 15, 2016
We're in 4.0.0-pre so it's the time for breaking changes!
4.0.0 is about cleaning up all the code we want so we can be more productive.
overlookmotel commentedon Jun 15, 2016
@mickhansen So that's a yes? :)
overlookmotel commentedon Jun 15, 2016
@felixfbecker Are there any major ES6 changes still in the works which might interfere with this if I start putting together a PR?
mickhansen commentedon Jun 15, 2016
That's a we're introducing co routines before 4.0.0 launches.
See other issues, we'll likely use function *() { in classes and then just a post processor to rebind and wrap.
overlookmotel commentedon Jun 15, 2016
My opinion is that we're not ready for coroutines quite yet.
Please see my comment here #5898 (comment) about coroutines being responsible for loss of CLS context in some cases.
My feeling is that ensuring CLS support is 100% robust is really important because it's used for running queries in the right transactions - so a bug threatens to undermine consistency, which I think is a pretty big deal.
I'm going for it fast as I can in trying to iron out the CLS/bluebird issues. But it's really complex (see test suite I'm building and issue on CLS), so it may take a while and might not be ready for Sequelize v4 if that's imminent.
Of course, I don't have the final say in these things! But in my opinion, keeping CLS support bug-free is critical.
overlookmotel commentedon Jun 15, 2016
By the way, @mickhansen if the post-processor you mention is https://github.com/danielstjules/async-class, that looks like a nice clean way to co-wrap the methods of a class.
felixfbecker commentedon Jun 15, 2016
@overlookmotel No, not really. Only the tests and template strings.
Personally I'm -1 on coroutines because
BUT I'm totally 100% for always either throwing sync or async, but never both. It releases Zalgo. So go ahead with this "always throw async" thing, and maybe we can do the important stuff like cleaner API for ES6 users, connection-independent models etc. first and later use coroutines after 4.0 is released.
mickhansen commentedon Jun 16, 2016
@felixfbecker We will need to evaluate the performance of coroutines via our benchmark suite in any case, but having used
async/awaita lot in professional projects it's something me and @janmeier think would increase contributor productivity greatly.janmeier commentedon Jun 16, 2016
Definitely agree that we should wrap everything in promise.method to be consistent with regards to sync / async errors.
It does however have the same problems as wrapping in a coroutine does - The class is no longer static so V8 might de-opt it. Of course we could wrap the body of each method instead, but pretty it is not.
felixfbecker commentedon Jun 16, 2016
@janmeier
Promise.method()is not needed, simply replace all occurences ofthrow new Whatever()withreturn Promise.reject(new Whatever())(of course only outside of promise handlers). Or start every method withreturn Promise.try(), as it is already used in the codebase in quite a few places.janmeier commentedon Jun 16, 2016
Regarding CLS, we might also consider if the extra 🐒 patching work is worth it, compared to a switch to domains. Yes I know domains are deprecated, but at least they are officially supported by bluebird.
I'm not saying its a good solution, but I don't think there are any good solutions
felixfbecker commentedon Jun 16, 2016
We could also pass the contexts explicitely via options. I always prefer explicit over implicit and really dislike CLS in general because it is so magic.
overlookmotel commentedon Jun 16, 2016
@felixfbecker Contexts are already passed explicitly with options inside Sequelize. The CLS support is for 2 things: (1) so that if the user wants to use CLS in their own code, calling a Sequelize method doesn't foul it up, and (2) transaction can be passed via CLS.
@janmeier Personally I'm quite invested in CLS so I'm going to do the work of working out the monkey-patching anyway. It's just a question of how long it'll take.
The replacement for both domains and CLS is under development in node core. Async Wrap is actually already in node, but undocumented, and it probably won't be too long until it's officially released.
So I don't think now is a particularly good time to switch to domains.
felixfbecker commentedon Jun 18, 2016
@overlookmotel If we need to patch global Bluebird, the recently added
Promise.getNewLibraryCopy()might be helpfuloverlookmotel commentedon Jun 18, 2016
@felixfbecker Yes, that's why I made the PR on
bluebirdto introduce it! 😄30 remaining items
overlookmotel commentedon Jun 30, 2017
At present that would screw up CLS support - native promises don't support CLS. But once async_hooks stabilises, CLS will use that underlying mechanism instead and that particular problem will go away. Give it a couple of months I'd say.
janmeier commentedon Jun 30, 2017
Really looking forward to async_hooks - Alone for the fact that we will then have something that's stable, and officially supported by core (as opposed to CLS and domains)
felixfbecker commentedon Jun 30, 2017
We will have to wait until Node 4 is out of LTS anyway.
Personally I would never use CLS (or domains), the perf impact is just way to high.
overlookmotel commentedon Jun 30, 2017
@janmeier Yes me too!
@felixfbecker I imagine CLS will be adapted so it uses the old methods on old versions of Node and async_hooks on recent node versions. The performance of async_hooks is meant to be very good, but I haven't benchmarked it yet.
stale commentedon Aug 29, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
overlookmotel commentedon Aug 29, 2017
I think this issue should remain open.
papb commentedon Jul 26, 2019
@overlookmotel What is the status/ideas on this? After two years, anything has changed? New ideas?
sushantdhiman commentedon May 2, 2020
Superseded by #11859, Closing this issue