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

doc, async_hooks: improve and add migration hints #45369

Merged
merged 15 commits into from Nov 10, 2022
43 changes: 23 additions & 20 deletions doc/api/async_hooks.md
Expand Up @@ -6,6 +6,14 @@

<!-- source_link=lib/async_hooks.js -->

This module will most likely never reach stable state. Please migrate to
other APIs.
Flarna marked this conversation as resolved.
Show resolved Hide resolved

* [`AsyncLocalStorage`][] is stable and designed to cover one of the
main usecases to track async context
* [`process.getActiveResourcesInfo()`][] might fit better to track active
resources
Flarna marked this conversation as resolved.
Show resolved Hide resolved

The `node:async_hooks` module provides an API to track asynchronous resources.
It can be accessed using:

Expand Down Expand Up @@ -327,20 +335,11 @@ current Node.js instance.

The `type` is a string identifying the type of resource that caused
`init` to be called. Generally, it will correspond to the name of the
resource's constructor.

Valid values are:

```text
FSEVENTWRAP, FSREQCALLBACK, GETADDRINFOREQWRAP, GETNAMEINFOREQWRAP, HTTPINCOMINGMESSAGE,
HTTPCLIENTREQUEST, JSSTREAM, PIPECONNECTWRAP, PIPEWRAP, PROCESSWRAP, QUERYWRAP,
SHUTDOWNWRAP, SIGNALWRAP, STATWATCHER, TCPCONNECTWRAP, TCPSERVERWRAP, TCPWRAP,
TTYWRAP, UDPSENDWRAP, UDPWRAP, WRITEWRAP, ZLIB, SSLCONNECTION, PBKDF2REQUEST,
RANDOMBYTESREQUEST, TLSWRAP, Microtask, Timeout, Immediate, TickObject
```
Qard marked this conversation as resolved.
Show resolved Hide resolved
resource's constructor. The `type` of resources created by Node.js itself
can change in any Node.js release.

These values can change in any Node.js release. Furthermore users of [`AsyncResource`][]
likely provide other values.
Furthermore users of [`AsyncResource`][] create async resources independent
of Node.js itself.

There is also the `PROMISE` resource type, which is used to track `Promise`
instances and asynchronous work scheduled by them.
Expand Down Expand Up @@ -414,19 +413,19 @@ of propagating what resource is responsible for the new resource's existence.
##### `resource`

`resource` is an object that represents the actual async resource that has
been initialized. This can contain useful information that can vary based on
the value of `type`. For instance, for the `GETADDRINFOREQWRAP` resource type,
`resource` provides the host name used when looking up the IP address for the
host in `net.Server.listen()`. The API for accessing this information is
not supported, but using the Embedder API, users can provide
and document their own resource objects. For example, such a resource object
could contain the SQL query being executed.
been initialized. The API to access the object may be specified by the
creator of the resource. Resources created by Node.js itself are internal
and may change at any time therefore no API is specified for these.
Trott marked this conversation as resolved.
Show resolved Hide resolved

In some cases the resource object is reused for performance reasons, it is
thus not safe to use it as a key in a `WeakMap` or add properties to it.

##### Asynchronous context example

The context tracking use case is covered by the stable API [`AsyncLocalStorage`][].
This example only illustrates async hooks operation but [`AsyncLocalStorage`][]
fits better to this use case.

The following is an example with additional information about the calls to
`init` between the `before` and `after` calls, specifically what the
callback to `listen()` will look like. The output formatting is slightly more
Expand Down Expand Up @@ -568,6 +567,9 @@ made to the `resource` object passed to `init` it is possible that `destroy`
will never be called, causing a memory leak in the application. If the resource
does not depend on garbage collection, then this will not be an issue.

Using the destroy hook results in additional overhead because it enables
tracking of `Promise` instances via garbage collector.
Trott marked this conversation as resolved.
Show resolved Hide resolved

#### `promiseResolve(asyncId)`

<!-- YAML
Expand Down Expand Up @@ -869,6 +871,7 @@ The documentation for this class has moved [`AsyncLocalStorage`][].
[`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage
[`AsyncResource`]: async_context.md#class-asyncresource
[`Worker`]: worker_threads.md#class-worker
[`process.getActiveResourcesInfo()`]: process.md#processgetActiveResourcesInfo
[`after` callback]: #afterasyncid
[`before` callback]: #beforeasyncid
[`destroy` callback]: #destroyasyncid
Expand Down