Skip to content

Domains support #709

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

Merged
merged 9 commits into from
Jan 17, 2014
Merged

Domains support #709

merged 9 commits into from
Jan 17, 2014

Conversation

sidorares
Copy link
Member

This change is very similar to sidorares/node-mysql2#75

Usually connection/pool is created in top level domain (or no domain at all) and you would expect query(cb) callback to be called in the context of query() domain (often http request level domain). Instead, connection EventEmitter captures connect time domain and it is used later in all callbacks. This PR wraps all api callbacks with bindToCurrentDomain function. There should be no performance impact when domains are not used. See test and sidorares/node-mysql2#73 for code examples

@felixge
Copy link
Collaborator

felixge commented Jan 16, 2014

LGTM. I've never really worked with domains as they don't solve the problem of callback safety, but I'm all for it if people find them useful.

@sidorares
Copy link
Member Author

@dresende @dougwilson - wdyt?

@dougwilson
Copy link
Member

It looks good to me. Do we think this should also be added to the callbacks in Pool?

@sidorares
Copy link
Member Author

You are right. For some reason I was thinking that this is unnecessary. because already happens in underlying query call. I'll add this to pool callbacks

@dougwilson
Copy link
Member

@sidorares yea, I thought so too. It may, but at the very least Pool.prototype.query won't, since the query is wrapped in a call to Pool.prototype.getConnection which will loose the domain. This would also loose the domain for users wrapping their query in this, so we probably should :)

@sidorares
Copy link
Member Author

@dougwilson - added test, but can't make it to fail, pooled connection seems to use correct domain.

@dougwilson
Copy link
Member

I think to see the pool fail, you have to have an idle connection in the pool, then calling getConnection will loose the domain. If getConnection actually creates a brand new connection then the domain stuff is covered by Connection methods.


From: Andrey Sidorovmailto:[email protected]
Sent: ‎1/‎16/‎2014 17:44
To: felixge/node-mysqlmailto:[email protected]
Cc: Douglas Christopher Wilsonmailto:[email protected]
Subject: Re: [node-mysql] Domains support (#709)

@dougwilson - added test, but can't make it to fail, pooled connection seems to use correct domain.


Reply to this email directly or view it on GitHub:
#709 (comment)

@sidorares
Copy link
Member Author

still not failing...

  // make idle connection first
  pool.getConnection(function(err, conn) {
    conn.release();
    d3.run(function() {
      pool.query('SELECT 2', function(err, _rows, _fields) {
        if (err) throw err;
        throw new Error('inside domain 3');
      });
    });
  });

@dougwilson
Copy link
Member

Yea, sorry, after I sent that I realized I said it wrong. I actually meant the domain will vanish when getConnection has to wait for a connection to become available. I'll make a test, haha. I can't articulate well right now it seems :)

@dougwilson
Copy link
Member

See if this fails, plz :)

var pool = common.createPool({connectionLimit: 1});

pool.getConnection(function(err, conn) {
  d3.run(function() {
    pool.query('SELECT 2', function(err, _rows, _fields) {
      if (err) throw err;
      throw new Error('inside domain 3');
    });
  });
  process.nextTick(function () {
    conn.release();
  });
});

@dougwilson
Copy link
Member

If the above does fail, I think it would fix it by binding the domain to cb at https://github.com/felixge/node-mysql/blob/master/lib/Pool.js#L66

If it does not fail, then I guess we won't worry about it unless I can come with with something that does fail :)

@sidorares
Copy link
Member Author

Finally - test fails without pool.js change and passes with. @dougwilson - could you please review the test?

@dougwilson
Copy link
Member

Sweet. I think we are good to go. I can't think of any other holes there would be.

@sidorares
Copy link
Member Author

thanks for help @dougwilson - very much appreciated! merging...

sidorares added a commit that referenced this pull request Jan 17, 2014
@sidorares sidorares merged commit f71022d into master Jan 17, 2014
@sidorares sidorares deleted the domains branch January 17, 2014 01:22
@felixge
Copy link
Collaborator

felixge commented Jan 17, 2014

💖 thx guys

@sidorares
Copy link
Member Author

see also: #308 #489

dveeden pushed a commit to dveeden/mysql that referenced this pull request Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants