2010-02-09 reactor pattern & event driven programming

65
Reactor Pattern & Event-Driven Programming A scalable concurrent approach, using EventMachine with Thin as an example Lin Jen-Shin, http://godfat.org /

Upload: lin-jen-shin

Post on 28-Jan-2015

110 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor Pattern&

Event-Driven ProgrammingA scalable concurrent approach,using EventMachine with Thin as an example

Lin Jen-Shin, http://godfat.org/

Page 2: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor Pattern&

Event-Driven Programminghttp://godfat.org/slide/2010-02-29-reactor-pattern-and.pdf

Lin Jen-Shin, http://godfat.org/

Page 3: 2010-02-09 Reactor Pattern & Event Driven Programming

Table of Contents

Page 4: 2010-02-09 Reactor Pattern & Event Driven Programming

Table of Contents

•concurrency, why and how in network

Page 5: 2010-02-09 Reactor Pattern & Event Driven Programming

Table of Contents

•concurrency, why and how in network

•Event-Driven Programming explained in Flash with Ruby syntax

Page 6: 2010-02-09 Reactor Pattern & Event Driven Programming

Table of Contents

•concurrency, why and how in network

•Event-Driven Programming explained in Flash with Ruby syntax

•Reactor Pattern in EventMachine with Thin

Page 7: 2010-02-09 Reactor Pattern & Event Driven Programming

Table of Contents

•concurrency, why and how in network

•Event-Driven Programming explained in Flash with Ruby syntax

•Reactor Pattern in EventMachine with Thin

•how Thin works

Page 8: 2010-02-09 Reactor Pattern & Event Driven Programming

Table of Contents

•concurrency, why and how in network

•Event-Driven Programming explained in Flash with Ruby syntax

•Reactor Pattern in EventMachine with Thin

•how Thin works

•how EventMachine works

Page 9: 2010-02-09 Reactor Pattern & Event Driven Programming

Table of Contents

•concurrency, why and how in network

•Event-Driven Programming explained in Flash with Ruby syntax

•Reactor Pattern in EventMachine with Thin

•how Thin works

•how EventMachine works

Page 10: 2010-02-09 Reactor Pattern & Event Driven Programming

concurrency, whyand how in network

Page 11: 2010-02-09 Reactor Pattern & Event Driven Programming

concurrency, whyand how in network

• [network I/O] is slow, we shouldn't wait for [network I/O] while make [CPU] idle.

Page 12: 2010-02-09 Reactor Pattern & Event Driven Programming

concurrency, whyand how in network

• [network I/O] is slow, we shouldn't wait for [network I/O] while make [CPU] idle.

•you can replace [network I/O] and [CPU] with all other resources like [disc I/O], [memory I/O], etc.

Page 13: 2010-02-09 Reactor Pattern & Event Driven Programming

concurrency, whyand how in network

• [network I/O] is slow, we shouldn't wait for [network I/O] while make [CPU] idle.

•you can replace [network I/O] and [CPU] with all other resources like [disc I/O], [memory I/O], etc.

•each kernel process/thread for each client using a blocking I/O is easy to write but not scalable at all

Page 14: 2010-02-09 Reactor Pattern & Event Driven Programming

Table of Contents

•concurrency, why and how in network

•Event-Driven Programming explained in Flash with Ruby syntax

•Reactor Pattern in EventMachine with Thin

•how Thin works

•how EventMachine works

Page 15: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programmingto the rescue

Page 16: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programming

•only one process/thread

Page 17: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programming

•only one process/thread

• inversion of control

Page 18: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programming

•only one process/thread

• inversion of control

•consists of an event loop and variousevent handlers

Page 19: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programming

• inversion of control

Page 20: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programmingloop{ # you control the flow do_something}

• inversion of control

Page 21: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programmingloop{ # you control the flow do_something}

• inversion of control

register method(:do_something)loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event}

Page 22: 2010-02-09 Reactor Pattern & Event Driven Programming

•consists of an event loop and variousevent handlers

Event-Driven Programmingregister method(:do_something)loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event}

Page 23: 2010-02-09 Reactor Pattern & Event Driven Programming

•consists of an event loop and variousevent handlers

Event-Driven Programmingregister method(:do_something)loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event}

Page 24: 2010-02-09 Reactor Pattern & Event Driven Programming

•consists of an event loop and variousevent handlers

Event-Driven Programmingregister method(:do_something)loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event}

Page 25: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programmingin Flash with Ruby syntax

Page 26: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programmingin Flash with Ruby syntax•game loop, an example of event loop

Page 27: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programmingin Flash with Ruby syntax•game loop, an example of event loop

•Flash ActionScript, onEnterFrame

Page 28: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programmingin Flash with Ruby syntax•game loop, an example of event loop

•Flash ActionScript, onEnterFrame

• frame by frame

Page 29: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programmingin Flash with Ruby syntax

# in each sprite thread30.times{ application.draw sprite sprite.x += 1}

Page 30: 2010-02-09 Reactor Pattern & Event Driven Programming

# in each sprite thread30.times{ application.draw sprite sprite.x += 1}

sprite.onEnterFrame = lambda{ sprite.x += 1}

Page 31: 2010-02-09 Reactor Pattern & Event Driven Programming

application.register sprite30.times{ # event loop, also called game loop events = application.pop_event_queue events.each{ |event| application.dispatch event } # model/view separation application.draw application.sprites}# in each sprite thread30.times{ application.draw sprite sprite.x += 1}

sprite.onEnterFrame = lambda{ sprite.x += 1}

Page 32: 2010-02-09 Reactor Pattern & Event Driven Programming

Table of Contents

•concurrency, why and how in network

•Event-Driven Programming explained in Flash with Ruby syntax

•Reactor Pattern in EventMachine with Thin

•how Thin works

•how EventMachine works

Page 33: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor Pattern

Page 34: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor Patternloop{ data = read handle data}

Page 35: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor Patternloop{ data = read handle data}

register method(:handle)loop{ data = partial_read event = process data dispatch event if event}

Page 36: 2010-02-09 Reactor Pattern & Event Driven Programming

Event-Driven Programmingloop{ # you control the flow do_something}

register method(:do_something)loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event}

Page 37: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor Patternloop{ data = read handle data}

register method(:handle)loop{ data = partial_read event = process data dispatch event if event}

Page 38: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor Pattern

by wikipedia

Page 39: 2010-02-09 Reactor Pattern & Event Driven Programming

• resources # e.g. network I/O

Reactor Pattern

by wikipedia

Page 40: 2010-02-09 Reactor Pattern & Event Driven Programming

• resources # e.g. network I/O

• synchronous event demultiplexer# i.e. the blocking event loop

Reactor Pattern

by wikipedia

Page 41: 2010-02-09 Reactor Pattern & Event Driven Programming

• resources # e.g. network I/O

• synchronous event demultiplexer# i.e. the blocking event loop

•dispatcher# i.e. handler manager and event dispatcher

Reactor Pattern

by wikipedia

Page 42: 2010-02-09 Reactor Pattern & Event Driven Programming

• resources # e.g. network I/O

• synchronous event demultiplexer# i.e. the blocking event loop

•dispatcher# i.e. handler manager and event dispatcher

• request handler # e.g. thin handler

Reactor Pattern

by wikipedia

Page 43: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor PatternRequest

(resource)

Page 44: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor PatternRequest

(resource)EventMachine (demultiplexer + dispatcher)

Page 45: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor PatternRequest

(resource)Thin (or AMQP)(request handler)

EventMachine (demultiplexer + dispatcher)

Page 46: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor PatternRequest

(resource)Thin (or AMQP)(request handler)

Rack Thin handler

EventMachine (demultiplexer + dispatcher)

Page 47: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor PatternRequest

(resource)Thin (or AMQP)(request handler)

Rack Thin handler

EventMachine (demultiplexer + dispatcher)

Rack Rails adapter rack env

Page 48: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor PatternRequest

(resource)Thin (or AMQP)(request handler)

Rack Thin handler

EventMachine (demultiplexer + dispatcher)

Rails Rack Rails adapter rack env

Page 49: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor PatternRequest

(resource)Thin (or AMQP)(request handler)

Rack Thin handler

EventMachine (demultiplexer + dispatcher)

Rails

your rails application

Rack Rails adapter rack env

Page 50: 2010-02-09 Reactor Pattern & Event Driven Programming

Reactor PatternEventMachine is a generic network I/O server/clientlibrary due to I/O and request handler separation in

Reactor Pattern

Page 51: 2010-02-09 Reactor Pattern & Event Driven Programming

•EventMachine (Ruby)

Reactor Pattern

Page 52: 2010-02-09 Reactor Pattern & Event Driven Programming

•EventMachine (Ruby)

•Twisted (Python)

Reactor Pattern

Page 53: 2010-02-09 Reactor Pattern & Event Driven Programming

•EventMachine (Ruby)

•Twisted (Python)

•nodejs (JavaScript in V8)

Reactor Pattern

Page 54: 2010-02-09 Reactor Pattern & Event Driven Programming

•EventMachine (Ruby)

•Twisted (Python)

•nodejs (JavaScript in V8)

• libevent and libev (C)

Reactor Pattern

Page 55: 2010-02-09 Reactor Pattern & Event Driven Programming

• select (POSIX)

Reactor Pattern

Page 56: 2010-02-09 Reactor Pattern & Event Driven Programming

• select (POSIX)

•poll (POSIX)

Reactor Pattern

Page 57: 2010-02-09 Reactor Pattern & Event Driven Programming

• select (POSIX)

•poll (POSIX)

•epoll (Linux)

Reactor Pattern

Page 58: 2010-02-09 Reactor Pattern & Event Driven Programming

• select (POSIX)

•poll (POSIX)

•epoll (Linux)

•kqueue (BSD, Mac OS X (Darwin))

Reactor Pattern

Page 59: 2010-02-09 Reactor Pattern & Event Driven Programming

Table of Contents

•concurrency, why and how in network

•Event-Driven Programming explained in Flash with Ruby syntax

•Reactor Pattern in EventMachine with Thin

•how Thin works

•how EventMachine works

Page 60: 2010-02-09 Reactor Pattern & Event Driven Programming

how Thin works•Thin::Server

Page 61: 2010-02-09 Reactor Pattern & Event Driven Programming

how Thin works•Thin::Server

•Thin::Backends::TcpServer# communicate with EventMachine

Page 62: 2010-02-09 Reactor Pattern & Event Driven Programming

how Thin works•Thin::Server

•Thin::Backends::TcpServer# communicate with EventMachine

•Thin::Connection# EventMachine event handler

Page 63: 2010-02-09 Reactor Pattern & Event Driven Programming

how Thin works•Thin::Server

•Thin::Backends::TcpServer# communicate with EventMachine

•Thin::Connection# EventMachine event handler

•Thin::Request# partial HTTP request parsing# Rack env builder

Page 64: 2010-02-09 Reactor Pattern & Event Driven Programming

how Thin works

Sorry! To be continued......

Page 65: 2010-02-09 Reactor Pattern & Event Driven Programming

how Thin works

Sorry! To be continued......

?