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

Can this be modeled on top of async hooks #41

Open
jkrems opened this issue Feb 13, 2018 · 2 comments
Open

Can this be modeled on top of async hooks #41

jkrems opened this issue Feb 13, 2018 · 2 comments

Comments

@jkrems
Copy link

jkrems commented Feb 13, 2018

Things like asynchronous operations at the very least could be expressed as (virtual) async resources. An interesting question would be if this is sufficient to express something like a "diagnostics channel" by filtering for specific async_hooks resources.

@MSLaguana
Copy link
Member

I believe that async_hooks is not enough by itself, because of issues with userspace queueing. Essentially, if I have code like the following:

// Async context 1
function foo() {
  doAsyncThing("data", function () { console.log('Called back eventually')})
}

// Async context 2
function bar() {
  doAsyncThing("more data", function () { console.log('called back after starting in a different context');})
}

function doAsyncThing() {
  //...
  manager.jobs.push({data, callback});
  //...
}

// Elsewhere, asynchronously
manager.onFreeWorker = function () {
  worker.jobs = this.jobs;
  this.jobs = [];
  worker.work().then((completedJobs) => {
    for (job of completedJobs) {
      job.callback();
    }
  }
}

What context are the callbacks invoked in? There are at least 3 different asynchronous contexts here (suppose the first 2 are different HTTP requests coming in, and the worker is communicating the jobs with an out-of-process worker like a database).

My understanding of async hooks would let you associate a single stack of calls together, for example the promises in onFreeWorker might be associated with the context that invoked onFreeWorker, but that isn't either of the 2 http contexts. And each callback invoked at the end is invoked synchronously, within a single async context, so how would they be distinguished and assigned to the correct original context?

@jkrems
Copy link
Author

jkrems commented Feb 13, 2018

I wasn't even talking about the async tracking, more about the data being emitted / the "diagnostics event bus". E.g. creating an AsyncResource that represents an individual mongodb interaction.

The userland callback queue can be implemented using the embedder API of async hooks so the context tracking shouldn't be an issue for properly instrumented libraries in node 8+.

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