Anda di halaman 1dari 61

mod_spdy: SPDY in Apache

Matthew Steele April 2012

Why make mod_spdy?


SPDY makes the web faster Apache is the most popular webserver by market share. To help increase SPDY adoption, we want a dead-easy-to-use, drop-in SPDY module for Apache.

History of mod_spdy
Work began January 2010. We implemented framing, and serial multiplexing. The project was placed on indefinite hold a few months later, pending better SPDY experimental results. Resumed the project November 2011. We now have a complete SPDY implementation.

Critical SPDY features


Next Protocol Negotiation (NPN) Framing and header compression Stream multiplexing Server push

How Apache works


The MPM accepts connections and assigns each one to a child process/thread. For each connection, most of the work is done by handlers and filters.
A handler takes control of completely processing a connection or an HTTP request. A filter transforms the input or output data passing between the handler and the socket.

How Apache works


Apache's behavior can be extended by modules, which register hooks. Hook functions are called at various interesting events, and give the module a chance to do something in response. For example, a module could register a hook to insert a filter for each request that arrives.

Critical SPDY features


Next Protocol Negotiation (NPN) Framing and header compression Stream multiplexing Server push

Next Protocol Negotiation (NPN)

server

client

Next Protocol Negotiation (NPN)

SSL handshake

server
Hello!

client

Next Protocol Negotiation (NPN)


I support: - spdy/3 - spdy/2 - foo/1.1

SSL handshake

server

client

Next Protocol Negotiation (NPN)


I support: - spdy/3 - spdy/2 - foo/1.1

SSL handshake

server
I choose: spdy/2

client

Next Protocol Negotiation (NPN) Our patched version of mod_ssl provides two NPN-related hooks.

Next Protocol Negotiation (NPN) Our patched version of mod_ssl provides two NPN-related hooks. The first hook lets us add protocols to be advertised. The second hook informs us which protocol was chosen by the client.

Next Protocol Negotiation (NPN) Our patched version of mod_ssl provides two NPN-related hooks. The first hook lets us add protocols to be advertised. The second hook informs us which protocol was chosen by the client. Other modules could use these hooks too.

Next Protocol Negotiation (NPN)


Apache server

mod_spdy

mod_ssl

client

mod_foo

Next Protocol Negotiation (NPN)


Apache server

mod_spdy

mod_ssl

SSL handshake

client

Hello! mod_foo

Next Protocol Negotiation (NPN)


Apache server What do we
support? mod_spdy

NPN advertise hook

mod_ssl

SSL handshake

client

Hello! mod_foo

Next Protocol Negotiation (NPN)


Apache server spdy/3
spdy/2 mod_spdy

NPN advertise hook

mod_ssl

SSL handshake

client

Hello! mod_foo foo/1.1

Next Protocol Negotiation (NPN)


Apache server
I support: - spdy/3 - spdy/2 - foo/1.1

mod_spdy

mod_ssl

SSL handshake

client

mod_foo

Next Protocol Negotiation (NPN)


Apache server
I support: - spdy/3 - spdy/2 - foo/1.1

mod_spdy

mod_ssl

SSL handshake

client

I choose: spdy/2 mod_foo

Next Protocol Negotiation (NPN)


Apache server The client
chose spdy/2 mod_spdy

NPN done hook

mod_ssl

client

mod_foo

Next Protocol Negotiation (NPN)


Apache server The client
chose spdy/2 mod_spdy

Hooray!

mod_ssl

client

mod_foo

Aww...

Critical SPDY features


Next Protocol Negotiation (NPN) Framing and header compression Stream multiplexing Server push

Framing and header compression We insert filters to translate from SPDY frames to equivalent HTTP data, and back. Do this at the connection (not request) level, to maintain header compression state. This approach lets the site owner use the same, HTTP-specific Apache modules to process requests as without mod_spdy.

Critical SPDY features


Next Protocol Negotiation (NPN) Framing and header compression Stream multiplexing Server push

How Apache works

How Apache works

MPM

server socket

How Apache works


client socket

MPM
client socket

server socket

client socket

How Apache works


connection object
client socket

MPM

connection object

client socket

server socket

connection object

client socket

How Apache works


thread/process 1 connection object
client socket

thread/process 2 MPM connection object

client socket

thread/process 3
server socket

connection object

client socket

How Apache works


thread/process 1 connection object

client socket

MPM

server socket

How Apache works


thread/process 1 connection object

client socket

network filters

MPM

server socket

How Apache works


thread/process 1 connection object connection filters (e.g. SSL)

client socket

network filters

MPM

server socket

How Apache works


thread/process 1 connection object connection filters (e.g. SSL) connection handler (e.g. HTTP)

client socket

network filters

MPM

server socket

How Apache works


thread/process 1 connection object connection filters (e.g. SSL) connection handler (e.g. HTTP)

client socket

network filters

MPM

request object

server socket

How Apache works


thread/process 1 connection object connection filters (e.g. SSL) request filters (e.g. gzip) connection handler (e.g. HTTP) request handler (e.g. CGI)

client socket

network filters

MPM

request object

server socket

How Apache works


thread/process 1 connection object connection filters (e.g. SSL) connection handler (e.g. HTTP)

client socket

network filters

MPM

server socket

Stream multiplexing
Once a request handler takes over, it does not return control until the request is finished. Apache (and modules) assume one thread per connection; the core data structures are not thread-safe. So we can't be event-driven, and we can't be multithreaded. What to do?

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL)

client socket

network filters

MPM

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM

SPDY stream

SPDY stream server socket SPDY stream

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM

SPDY thread 1
SPDY stream

SPDY thread 2
SPDY stream server socket SPDY stream

SPDY thread 3

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM

SPDY thread 1
SPDY stream

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM connection object

SPDY thread 1
SPDY stream

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM connection object

SPDY thread 1
SPDY stream fake socket

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM connection object

SPDY thread 1
SPDY stream fake socket

SSL filters

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM connection object

SPDY thread 1
SPDY stream fake socket

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM connection object

SPDY thread 1
SPDY stream fake socket

network filters

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM connection object

SPDY thread 1
SPDY stream fake socket

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM connection object

SPDY thread 1
SPDY stream fake socket

connection handler (e.g. HTTP)

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM connection object

SPDY thread 1
SPDY stream fake socket

SPDY/HTTP conversion filters

connection handler (e.g. HTTP)

server socket

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM connection object

SPDY thread 1
SPDY stream fake socket

SPDY/HTTP conversion filters

connection handler (e.g. HTTP)

server socket

request object

How mod_spdy works


thread/process 1 connection object connection filters (e.g. SSL) SPDY connection handler

client socket

network filters

MPM connection object

SPDY thread 1
SPDY stream fake socket

SPDY/HTTP conversion filters request filters (e.g. gzip)

connection handler (e.g. HTTP) request handler (e.g. CGI)

server socket

request object

Critical SPDY features


Next Protocol Negotiation (NPN) Framing and header compression Stream multiplexing Server push

Server push
mod_spdy provides a server-push function to other modules. When called (e.g. from a stream request handler), the main SPDY connection thread creates a new server-push stream. We synthesize a request to imagine that the client sent us, and allow Apache to respond accordingly.

Server push
One wrinkle: by the spec, we must send the pushed SYN_STREAM before the associated stream's FLAG_FIN.

Server push
One wrinkle: by the spec, we must send the pushed SYN_STREAM before the associated stream's FLAG_FIN. But we don't have response headers yet, and don't want to impose a synchronization between stream threads.

Server push
One wrinkle: by the spec, we must send the pushed SYN_STREAM before the associated stream's FLAG_FIN.

But we don't have response headers yet, and don't want to impose a synchronization between stream threads.

Solution: send SYN_STREAM sans headers; follow up with HEADERS frame later.

Critical SPDY features


Next Protocol Negotiation (NPN) Framing and header compression Stream multiplexing Server push

Remaining tasks/challenges

Get SSL changes into Apache trunk.


https://issues.apache.org/bugzilla/show_bug.cgi?id=52210

Support SPDY v3 in addition to v2. Experiment further with server push.

Open source project: http://code.google.com/p/mod-spdy/ Download DEB/RPM packages:


https://developers.google.com/speed/spdy/mod_spdy/

Anda mungkin juga menyukai