Upload
paolo-negri
View
4.980
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Short introduction to Mongrel2, it illustrates some basics, refer to the Mongrel2 doc for more in depth information.
Citation preview
Mongrel2an introduction
Paolo Negri - @hungryblankBerlin Ruby User Group 7th Oct 2010
1st somethingabout
Mongrel1
Mongrel1
“Mongrel is a small library that provides a very fast HTTP 1.1 server for Ruby web applications.”
(Mongrel README)
Mongrel1
“Unicorn is copyright 2009 by all contributors.
It is based on Mongrel”
(Unicorn README)
Mongrel1
“Thin is a Ruby web server that glues together 3 of the best Ruby libraries in web history:
• the Mongrel parser...”
(Thin homepage)
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)
Mongrel2 and Mongrel1share
• HTTP parser
• author - @zedshaw
Mongrel2is a web server
• application
• language
• network architecture } agnostic
• Focus on HTTP
• Not for a specific language
• Use it with Ruby, Python, C++, PHP, Haskell, Common Lisp, Perl, .NET, Clojure, Lua ...
Mongrel2Language Agnostic
Mongrel2MVC configuration
SQLite3Database
MongrelProcess
m2sh
M V C
Mongrel2MVC configuration
SQLite3Database
MongrelProcess
m2sh
M V C
Configuration is stored in SQLite3 database
Mongrel2MVC configuration
SQLite3Database
MongrelProcess
m2sh
M V C
m2sh writes/reads configuration
Mongrel2MVC configuration
SQLite3Database
MongrelProcess
m2sh
M V C
Mongrel reads config from SQLite3 and runs accordingly
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
Mongrel2m2sh - launch/manage
$ m2sh start -db tests/config.sqlite \ -host localhost -sudo
$ m2sh reload -db tests/config.sqlite \ -host localhost
Mongrel2Configuration
Load a configuration file with m2sh
$ m2sh load -config \ examples/config/sample.conf
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') }) ] )
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
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...
Mongrel2Configuration
• Server
• Host
• Route
• Dir
• Proxy
• Handler
• Server
• Host
• Route
• Dir
• Proxy
• Handler
Mongrel2Configuration
Is the root element of a configuration
• Server
• Host
• Route
• Dir
• Proxy
• Handler
Mongrel2Configuration
DNS hostname to answer for
A server can contain multiple hosts
• Server
• Host
• Route
• Dir
• Proxy
• Handler
Mongrel2Configuration
URL paths and patterns to match
example ‘/images/’
• Server
• Host
• Route
• Dir
• Proxy
• Handler
Mongrel2Configuration
Serves files out of a directory
(full 304 and ETag support)
• Server
• Host
• Route
• Dir
• Proxy
• Handler
Mongrel2Configuration
Requests matching the route are sent to another HTTP server
• Server
• Host
• Route
• Dir
• Proxy
• Handler
Mongrel2Configuration
“A Handler is the best part of Mongrel2”
Mongrel2Handler
mongrel2
handler
browser
Mongrel2Handler
mongrel2
handler
browser
Mongrel2Handler
mongrel2
handler
browser
Mongrel2Handler
mongrel2
handler
browser
• Handlers receive asynchronous requests from Mongrel2
• Handlers and Mongrel2 communicate via ØMQ sockets
Mongrel2Handler
Mongrel2Handlermongrel2
ØMQ sockets
handlerhandler
handler
Mongrel2Handlermongrel2
handlerhandler
handler
Push/Pull Pub/Sub
Requests Responses
Mongrel2Handlermongrel2
handlerhandler
handler
Push/Pull Pub/Sub
Requests Responses
Round robindistribution
Mongrel2Handlermongrel2
handlerhandler
handler
Push/Pull Pub/Sub
Requests Responses
Target up to128 clients
Mongrel2Handler
handlerhandler
handler
Push/Pull Pub/Sub
Requests Responses
UUID ID PATH SIZE:HEADERS,SIZE:BODY
Mongrel2Handler
handlerhandler
handler
Push/Pull Pub/Sub
Requests Responses
UUID SIZE:ID ID ID, BODY
• One or many handlers
• Increase/decrease # of handlers while the server is running, no reload/restart needed
• UUID are used to route messages
Mongrel2Handler
• 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
• http://github.com/perplexes/m2r
• JRuby or ruby 1.9.2
• Simple handler or Rack enabled
Mongrel2Ruby Handler
Mongrel2Ruby Handler
• Run any Rack app using mongrel2, now!
Mongrel2Questions
?
Thanks!
Paolo Negri@hungryblank