SlideShare uma empresa Scribd logo
1 de 76
Baixar para ler offline
Фатальный недостаток
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

Mais conteúdo relacionado

Mais procurados

Linklist through struct
Linklist through structLinklist through struct
Linklist through struct
IBRAR GUL
 
Java Thread Cronometro
Java Thread CronometroJava Thread Cronometro
Java Thread Cronometro
jubacalo
 

Mais procurados (20)

Rubyslava2102
Rubyslava2102Rubyslava2102
Rubyslava2102
 
Dsa 1
Dsa 1Dsa 1
Dsa 1
 
JAVA Program in NetBeans
JAVA Program in NetBeansJAVA Program in NetBeans
JAVA Program in NetBeans
 
Sockets java
Sockets javaSockets java
Sockets java
 
Base datos
Base datosBase datos
Base datos
 
Week 7 unit3 (chapter 10-11)
Week 7   unit3 (chapter 10-11)Week 7   unit3 (chapter 10-11)
Week 7 unit3 (chapter 10-11)
 
Java AWT Calculadora
Java AWT CalculadoraJava AWT Calculadora
Java AWT Calculadora
 
Gaztea Tech 2015: 4. GT Drawbot Control
Gaztea Tech 2015: 4. GT Drawbot ControlGaztea Tech 2015: 4. GT Drawbot Control
Gaztea Tech 2015: 4. GT Drawbot Control
 
JavaScript Assíncrono
JavaScript AssíncronoJavaScript Assíncrono
JavaScript Assíncrono
 
Linklist through struct
Linklist through structLinklist through struct
Linklist through struct
 
Caculadora pacho (1)
Caculadora pacho (1)Caculadora pacho (1)
Caculadora pacho (1)
 
aggregation and indexing with suitable example using MongoDB.
aggregation and indexing with suitable example using MongoDB.aggregation and indexing with suitable example using MongoDB.
aggregation and indexing with suitable example using MongoDB.
 
商派信息安全解决方案
商派信息安全解决方案商派信息安全解决方案
商派信息安全解决方案
 
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tipsOpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
 
Dasar c
Dasar cDasar c
Dasar c
 
Java Thread Cronometro
Java Thread CronometroJava Thread Cronometro
Java Thread Cronometro
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговлеТененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
 
L’enfer des callbacks
L’enfer des callbacksL’enfer des callbacks
L’enfer des callbacks
 
I will be callback/JS同步與非同步
I will be callback/JS同步與非同步I will be callback/JS同步與非同步
I will be callback/JS同步與非同步
 

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