Фатальный недостаток
Node.js
Алексей Охрименко - IPONWEB
Алексей Охрименко
twitter: @Ai_boy
99.9%
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello Worldn');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
});
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
});
async / await
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
});
async / await promise
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
});
async / await promise замыкания
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
});
async / await promise замыкания
stacktraces
https://www.future-processing.pl/blog/on-problems-with-threads-in-node-js/
https://www.dynatrace.com/blog/all-you-need-to-know-to-really-understand-the-node-js-event-loop-and-its-metrics/
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
});
???
После uncaughtException
• Immediate Shutdown

• Graceful Shutdown
Immediate Shutdown (убиться сразу)
Immediate Shutdown (убиться сразу)
Immediate Shutdown (убиться сразу)
500 500 500
Immediate Shutdown (убиться сразу)
500 200 200
10 секунд подождать
Graceful Shutdown (убиться красиво)
Graceful Shutdown (убиться красиво)
Graceful Shutdown (убиться красиво)
5 лет спустя
Graceful Shutdown (убиться красиво)
200 200
Graceful Shutdown (убиться красиво)
200 200
Graceful Shutdown (убиться красиво)
200 200
10 секунд уже потрачено
500
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
});
???
Сделай сам!
я-ж программист!
https://github.com/mattinsler/longjohn
Uncaught Error: foo
at f (index.html:24:23)
----------------------------------------
at setTimeout
at onload (index.html:28:40)
Uncaught Error: foo
at USER_NAME_MASHA (index.html:24:23)
----------------------------------------
at setTimeout
at onload (index.html:28:40)
Uncaught Error: foo
at USER_NAME_MASHA (index.html:24:23)
----------------------------------------
at setTimeout
at onload (index.html:28:40)
rename function 1000 callbacks 10 callbacks
Clusters
Clusters
Clusters
Clusters
Clusters
Domains
Domains
Async-Listeners
Node.js 0.11.x
Async-Wrap / Async-Hooks
Experimental
Для Async-Hooks - все
ресурс (объект)
setTimeout(() => {})
setTimeout(() => {})
init
setTimeout(() => {})
init
before
setTimeout(() => {})
init
before
after
setTimeout(() => {})
init
before
after
destroy
new Promise((res, rej) => {})
init
before
after
destroy
async_hooks.createHook({ init, before, after, destroy })
setTimeout(() => {})
AsyncID: 0
setTimeout(() => {})
AsyncID: 1
setTimeout(() => {})
AsyncID: 2
setTimeout(() => {
setTimeout(() => {})
})
AsyncID: 0
AsyncID: 0
setTimeout(() => {
setTimeout(() => {})
})
AsyncID: 1
AsyncID: 0
setTimeout(() => {
setTimeout(() => {})
})
AsyncID: 1
AsyncID: 1
setTimeout(() => {
setTimeout(() => {})
})
AsyncID: 1
AsyncID: 2
setTimeout(() => {
setTimeout(() => {})
})
AsyncID: 1
AsyncID: 2 TriggerID: 1
setTimeout(() => {
setTimeout(() => {})
})
AsyncID: 1
AsyncID: 2 TriggerID: 1
TCPWRAP(2): trigger: 1 execution: 1
TickObject(3): trigger: 2 execution: 1
before: 3
Timeout(4): trigger: 3 execution: 3
TIMERWRAP(5): trigger: 3 execution: 3
after: 3
destroy: 3
before: 5
before: 4
TTYWRAP(6): trigger: 4 execution: 4
SIGNALWRAP(7): trigger: 4 execution: 4
TTYWRAP(8): trigger: 4 execution: 4
>>> 4
TickObject(9): trigger: 4 execution: 4
after: 4
after: 5
before: 9
after: 9
destroy: 4
destroy: 9
destroy: 5
API очень низкоуровневое
LISP (time travel debugger)
Zone (stage 0)
https://github.com/domenic/zones
Zone
Zone.current.fork({}).run(function () {
Zone.current.inTheZone = true;
setTimeout(someCallback, 0);
});
function someCallback() {
console.log(Zone.current.inTheZone);
}
setTimeout(someCallback, 0);
Zone
Zone.current.fork({}).run(function () {
Zone.current.inTheZone = true;
setTimeout(someCallback, 0);
});
function someCallback() {
console.log(Zone.current.inTheZone);
}
setTimeout(someCallback, 0);
Zone
Zone.current.fork({}).run(function () {
Zone.current.inTheZone = true;
setTimeout(someCallback, 0);
});
function someCallback() {
console.log(Zone.current.inTheZone);
}
setTimeout(someCallback, 0);
Zone
Zone.current.fork({}).run(function () {
Zone.current.inTheZone = true;
setTimeout(someCallback, 0);
});
function someCallback() {
console.log(Zone.current.inTheZone);TRUE
}
setTimeout(someCallback, 0);
Zone
Zone.current.fork({}).run(function () {
Zone.current.inTheZone = true;
setTimeout(someCallback, 0);
});
function someCallback() {
console.log(Zone.current.inTheZone);
}
setTimeout(someCallback, 0);
Zone
Zone.current.fork({}).run(function () {
Zone.current.inTheZone = true;
setTimeout(someCallback, 0);
});
function someCallback() {
console.log(Zone.current.inTheZone);FALSE
}
setTimeout(someCallback, 0);
А что делать простым
смертным?
Алексей Охрименко
twitter: @Ai_boy
http://bit.ly/2vZKN6k

Фатальный недостаток Node.js