André Werlang
@awerlang
The Developer's Conference 2017 - Porto Alegre
The async_hooks module provides an API to register callbacks tracking the lifetime of asynchronous resources created inside a Node.js application
Stack trace: ProvisionedThroughputExceededException: The level of configured
provisioned throughput for the table was exceeded. Consider increasing your
provisioning level with the UpdateTable API.
at Request.extractError (node_modules/aws-sdk/lib/protocol/json.js:43:27)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (node_modules/aws-sdk/lib/request.js:668:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.anonymous (node_modules/aws-sdk/lib/request.js:670:12)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:115:18)
at Request.emit (node_modules/aws-sdk/lib/request.js:668:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
let currentRequest;
new http.Server(async (req, res) => {
currentRequest = req // <---- save for later
const delay = +req.url.substr(1)
await timeout(delay)
respond(res)
}).listen(8080)
function respond(res) {
const req = currentRequest // <---- retrieve
res.write('Response for ' + req.url)
res.end()
}
GET /8000 < Response for 3000
GET /3000 < Response for 3000
const asyncHooks = require('async_hooks')
function init(id, type, triggerAsyncId, resource) {
}
function before(id) {
}
function after(id) {
}
function destroy(id) {
}
const hook = asyncHooks.createHook({ init, before, after, destroy })
hook.enable()
requests.track() // <---- enable tracking
new http.Server(async (req, res) => {
requests.setRequest(req); // <---- save for later
const delay = +req.url.substr(1)
await timeout(delay)
respond(res)
}).listen(8080)
function respond(res) {
const req = requests.current() // <---- retrieve
res.write('Response for ' + req.url)
res.end()
}
const initState = new Map(), prevState = new Map()
function init(id, type, triggerAsyncId, resource) {
initState.set(id, currentRequest)
}
function before(id) {
prevState.set(id, currentRequest)
currentRequest = initState.get(id)
}
function after(id) {
currentRequest = prevState.get(id)
}
function destroy(id) {
prevState.delete(id)
initState.delete(id)
}