EventLoop & libuv

Nairi

Harutyunyan

nairi.haroutiounian@gmail.com

@nairihar

#nodejsarmenia 🇦🇲

 

#nodeGS

Reactor Pattern

I/O request

Event Loop

Event

Demultiplexer

Event

Queue

Hardware

I/O

Enqueue Event

Execute Event Handler

> fs.readFile('./file.txt', cb);

> uv__fs_read(req)

> cb(null, data);

JavaScript Code

V8

Operating System

C++ Bindings

Core Modules

libuv

c-ares,
zlib,
etc...

Node.js architecture

require('main.node')

node-gyp

Macrotasks

setTimeout / setInterval

I/O

setImmediate

close events

process.exit

index.js

Event Loop

refs++

refs--

refs==0

refs=0

refs>0

EventLoop queues

Microtasks

setTimeout & setInterval

I/O

setImmediate

close events

process.exit

index.js

Event Loop

EventLoop queues

process.nextTick

Promise

Promise

nextTick

Timers

I/O

JavaScript queues

Timers

I/O

Immediate

close events

process.exit

Event Loop

index.js

queues

Promise

nextTick

queues

timers

I/O Poll

pending I/O callbacks

idle handlers

prepare handlers

Check handlers

Close handlers

Run expired timers

Run completed I/O handlers

Wait for I/O to complete

Some checking after polling

Run close handlers

libuv

JavaScript

Finished I/O work or I/O Errors

setTimeout
setInterval

 

setImmediate

.on('close'... handlers

epoll

kqueue

event ports

Thread Pool

linux

BSD

SunOS

IOCP

Windows

Network I/O

File I/O

DNS

...

libuv

Non-Blocking I/O

Blocking I/O

Threads

UV_THREADPOOL_SIZE=8 (SIZE<=128)

Thread pool

Blocking I/O

CPU intensive

NOW: Up to 1024 threads (after libuv v1.30.0 or node v12.5.0)

Thank You!!!

nairi.haroutiounian@gmail.com

@nairihar

#nodeGS

Made with Slides.com