43
Mongrel2 an introduction Paolo Negri - @hungryblank Berlin Ruby User Group 7th Oct 2010

Mongrel2, a short introduction

Embed Size (px)

DESCRIPTION

Short introduction to Mongrel2, it illustrates some basics, refer to the Mongrel2 doc for more in depth information.

Citation preview

Page 1: Mongrel2, a short introduction

Mongrel2an introduction

Paolo Negri - @hungryblankBerlin Ruby User Group 7th Oct 2010

Page 2: Mongrel2, a short introduction

1st somethingabout

Mongrel1

Page 3: Mongrel2, a short introduction

Mongrel1

“Mongrel is a small library that provides a very fast HTTP 1.1 server for Ruby web applications.”

(Mongrel README)

Page 4: Mongrel2, a short introduction

Mongrel1

“Unicorn is copyright 2009 by all contributors.

It is based on Mongrel”

(Unicorn README)

Page 5: Mongrel2, a short introduction

Mongrel1

“Thin is a Ruby web server that glues together 3 of the best Ruby libraries in web history:

• the Mongrel parser...”

(Thin homepage)

Page 6: Mongrel2, a short introduction

Mongrel1

“Back then I wrote a little Ruby web server called Ebb that was meant to be a faster Mongrel.

That code was the starting point for Node”

(Ryan Dahl, node.js author)

Page 7: Mongrel2, a short introduction

Mongrel2 and Mongrel1share

• HTTP parser

• author - @zedshaw

Page 8: Mongrel2, a short introduction

Mongrel2is a web server

• application

• language

• network architecture } agnostic

Page 10: Mongrel2, a short introduction

Mongrel2MVC configuration

SQLite3Database

MongrelProcess

m2sh

M V C

Page 11: Mongrel2, a short introduction

Mongrel2MVC configuration

SQLite3Database

MongrelProcess

m2sh

M V C

Configuration is stored in SQLite3 database

Page 12: Mongrel2, a short introduction

Mongrel2MVC configuration

SQLite3Database

MongrelProcess

m2sh

M V C

m2sh writes/reads configuration

Page 13: Mongrel2, a short introduction

Mongrel2MVC configuration

SQLite3Database

MongrelProcess

m2sh

M V C

Mongrel reads config from SQLite3 and runs accordingly

Page 14: Mongrel2, a short introduction

Mongrel2m2sh - query

$ m2sh servers -db tests/config.sqlite

SERVERS:------ test localhost AC1F8236-5919-4696-9D40-0F38DE9E5861

$ m2sh hosts -db tests/config.sqlite -server test

HOSTS in test:----- 1 localhost

Page 15: Mongrel2, a short introduction

Mongrel2m2sh - launch/manage

$ m2sh start -db tests/config.sqlite \ -host localhost -sudo

$ m2sh reload -db tests/config.sqlite \  -host localhost 

Page 16: Mongrel2, a short introduction

Mongrel2Configuration

Load a configuration file with m2sh

$ m2sh load -config \ examples/config/sample.conf

Page 17: Mongrel2, a short introduction

Mongrel2Configuration

main = Server(     uuid="f400bf85-4538-4f7a-8908-67e313d515c2",     access_log="/logs/access.log",     error_log="/logs/error.log",     chroot="./",     default_host="localhost",     name="test",     pid_file="/run/mongrel2.pid",     port=6767,     hosts = [         Host(name="localhost", routes={             '/tests/': Dir(base='tests/', index_file='index.html',                              default_ctype='text/plain')         })     ] ) 

Page 18: Mongrel2, a short introduction

Mongrel2Configuration

main = Server(     uuid="f400bf85-4538-4f7a-8908-67e313d515c2",     access_log="/logs/access.log",     error_log="/logs/error.log",     chroot="./",     default_host="localhost",     name="test",     pid_file="/run/mongrel2.pid",     port=6767,     hosts = [         Host(name="localhost", routes={             '/tests/': Dir(base='tests/', index_file='index.html',                              default_ctype='text/plain')         })     ] ) 

Unique server identifier

Page 19: Mongrel2, a short introduction

Mongrel2Configuration

main = Server(     uuid="f400bf85-4538-4f7a-8908-67e313d515c2",     access_log="/logs/access.log",     error_log="/logs/error.log",     chroot="./",     default_host="localhost",     name="test",     pid_file="/run/mongrel2.pid",     port=6767,     hosts = [         Host(name="localhost", routes={             '/tests/': Dir(base='tests/', index_file='index.html',                              default_ctype='text/plain')         })     ] ) 

logfiles, port, server name, pid...

Page 20: Mongrel2, a short introduction

Mongrel2Configuration

• Server

• Host

• Route

• Dir

• Proxy

• Handler

Page 21: Mongrel2, a short introduction

• Server

• Host

• Route

• Dir

• Proxy

• Handler

Mongrel2Configuration

Is the root element of a configuration

Page 22: Mongrel2, a short introduction

• Server

• Host

• Route

• Dir

• Proxy

• Handler

Mongrel2Configuration

DNS hostname to answer for

A server can contain multiple hosts

Page 23: Mongrel2, a short introduction

• Server

• Host

• Route

• Dir

• Proxy

• Handler

Mongrel2Configuration

URL paths and patterns to match

example ‘/images/’

Page 24: Mongrel2, a short introduction

• Server

• Host

• Route

• Dir

• Proxy

• Handler

Mongrel2Configuration

Serves files out of a directory

(full 304 and ETag support)

Page 25: Mongrel2, a short introduction

• Server

• Host

• Route

• Dir

• Proxy

• Handler

Mongrel2Configuration

Requests matching the route are sent to another HTTP server

Page 26: Mongrel2, a short introduction

• Server

• Host

• Route

• Dir

• Proxy

• Handler

Mongrel2Configuration

“A Handler is the best part of Mongrel2”

Page 27: Mongrel2, a short introduction

Mongrel2Handler

mongrel2

handler

browser

Page 28: Mongrel2, a short introduction

Mongrel2Handler

mongrel2

handler

browser

Page 29: Mongrel2, a short introduction

Mongrel2Handler

mongrel2

handler

browser

Page 30: Mongrel2, a short introduction

Mongrel2Handler

mongrel2

handler

browser

Page 31: Mongrel2, a short introduction

• Handlers receive asynchronous requests from Mongrel2

• Handlers and Mongrel2 communicate via ØMQ sockets

Mongrel2Handler

Page 32: Mongrel2, a short introduction

Mongrel2Handlermongrel2

ØMQ sockets

handlerhandler

handler

Page 33: Mongrel2, a short introduction

Mongrel2Handlermongrel2

handlerhandler

handler

Push/Pull Pub/Sub

Requests Responses

Page 34: Mongrel2, a short introduction

Mongrel2Handlermongrel2

handlerhandler

handler

Push/Pull Pub/Sub

Requests Responses

Round robindistribution

Page 35: Mongrel2, a short introduction

Mongrel2Handlermongrel2

handlerhandler

handler

Push/Pull Pub/Sub

Requests Responses

Target up to128 clients

Page 36: Mongrel2, a short introduction

Mongrel2Handler

handlerhandler

handler

Push/Pull Pub/Sub

Requests Responses

UUID ID PATH SIZE:HEADERS,SIZE:BODY

Page 37: Mongrel2, a short introduction

Mongrel2Handler

handlerhandler

handler

Push/Pull Pub/Sub

Requests Responses

UUID SIZE:ID ID ID, BODY

Page 38: Mongrel2, a short introduction

• One or many handlers

• Increase/decrease # of handlers while the server is running, no reload/restart needed

• UUID are used to route messages

Mongrel2Handler

Page 39: Mongrel2, a short introduction

• Communication with handler completely async

• ØMQ sockets can work over different transports (tcp, domain sockets...)

• Messages to/from handler built on netstrings (D. J. Bernstein) are easy to parse

Mongrel2Handler

Page 40: Mongrel2, a short introduction

• http://github.com/perplexes/m2r

• JRuby or ruby 1.9.2

• Simple handler or Rack enabled

Mongrel2Ruby Handler

Page 41: Mongrel2, a short introduction

Mongrel2Ruby Handler

• Run any Rack app using mongrel2, now!

Page 42: Mongrel2, a short introduction

Mongrel2Questions

?

Page 43: Mongrel2, a short introduction

Thanks!

Paolo Negri@hungryblank