From 82d6726dcb4e05a603e971f46956c142f9403968 Mon Sep 17 00:00:00 2001 From: Andrey Pechkurov Date: Fri, 10 Apr 2020 11:07:40 +0300 Subject: [PATCH] doc: improve AsyncLocalStorage sample MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/32757 Reviewed-By: Michaël Zasso Reviewed-By: Chengzhong Wu Reviewed-By: Colin Ihrig Reviewed-By: Gerhard Stöbich Reviewed-By: Luigi Pinca --- doc/api/async_hooks.md | 49 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 5e5c328229a7d8..5463c39bdc6c93 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -876,41 +876,40 @@ chains. It allows storing data throughout the lifetime of a web request or any other asynchronous duration. It is similar to thread-local storage in other languages. -The following example builds a logger that will always know the current HTTP -request and uses it to display enhanced logs without needing to explicitly -provide the current HTTP request to it. +The following example uses `AsyncLocalStorage` to build a simple logger +that assigns IDs to incoming HTTP requests and includes them in messages +logged within each request. ```js -const { AsyncLocalStorage } = require('async_hooks'); const http = require('http'); +const { AsyncLocalStorage } = require('async_hooks'); -const kReq = 'CURRENT_REQUEST'; const asyncLocalStorage = new AsyncLocalStorage(); -function log(...args) { - const store = asyncLocalStorage.getStore(); - // Make sure the store exists and it contains a request. - if (store && store.has(kReq)) { - const req = store.get(kReq); - // Prints `GET /items ERR could not do something - console.log(req.method, req.url, ...args); - } else { - console.log(...args); - } +function logWithId(msg) { + const id = asyncLocalStorage.getStore(); + console.log(`${id !== undefined ? id : '-'}:`, msg); } -http.createServer((request, response) => { - asyncLocalStorage.run(new Map(), () => { - const store = asyncLocalStorage.getStore(); - store.set(kReq, request); - someAsyncOperation((err, result) => { - if (err) { - log('ERR', err.message); - } +let idSeq = 0; +http.createServer((req, res) => { + asyncLocalStorage.run(idSeq++, () => { + logWithId('start'); + // Imagine any chain of async operations here + setImmediate(() => { + logWithId('finish'); + res.end(); }); }); -}) -.listen(8080); +}).listen(8080); + +http.get('http://localhost:8080'); +http.get('http://localhost:8080'); +// Prints: +// 0: start +// 1: start +// 0: finish +// 1: finish ``` When having multiple instances of `AsyncLocalStorage`, they are independent