| 1 |
[[flow_diagram]] |
| 2 |
== Flow diagram |
| 3 |
|
| 4 |
Cowboy is a lightweight HTTP server with support for HTTP/1.1, |
| 5 |
HTTP/2 and Websocket. |
| 6 |
|
| 7 |
It is built on top of Ranch. Please see the Ranch guide for more |
| 8 |
information about how the network connections are handled. |
| 9 |
|
| 10 |
=== Overview |
| 11 |
|
| 12 |
image::http_req_resp.png[HTTP request/response flowchart] |
| 13 |
|
| 14 |
As you can see on the diagram, the client |
| 15 |
begins by connecting to the server. This step is handled |
| 16 |
by a Ranch acceptor, which is a process dedicated to |
| 17 |
accepting new connections. |
| 18 |
|
| 19 |
After Ranch accepts a new connection, whether it is an |
| 20 |
HTTP/1.1 or HTTP/2 connection, Cowboy starts receiving |
| 21 |
requests and handling them. |
| 22 |
|
| 23 |
In HTTP/1.1 all requests come sequentially. In HTTP/2 |
| 24 |
the requests may arrive and be processed concurrently. |
| 25 |
|
| 26 |
When a request comes in, Cowboy creates a stream, which |
| 27 |
is a set of request/response and all the events associated |
| 28 |
with them. The protocol code in Cowboy defers the handling |
| 29 |
of these streams to stream handler modules. When you |
| 30 |
configure Cowboy you may define one or more module that |
| 31 |
will receive all events associated with a stream, including |
| 32 |
the request, response, bodies, Erlang messages and more. |
| 33 |
|
| 34 |
By default, Cowboy comes configured with a stream handler |
| 35 |
called `cowboy_stream_h`. This stream handler will create |
| 36 |
a new process for every request coming in, and then |
| 37 |
communicate with this process to read the body or send |
| 38 |
a response back. The request process executes middlewares. |
| 39 |
By default, the request process executes the router and then |
| 40 |
the handlers. Like stream handlers, middlewares may also be |
| 41 |
customized. |
| 42 |
|
| 43 |
A response may be sent at almost any point in this |
| 44 |
diagram. If the response must be sent before the stream |
| 45 |
is initialized (because an error occurred early, for |
| 46 |
example) then stream handlers receive a special event |
| 47 |
indicating this error. |
| 48 |
|
| 49 |
=== Protocol-specific headers |
| 50 |
|
| 51 |
Cowboy takes care of protocol-specific headers and prevents |
| 52 |
you from sending them manually. For HTTP/1.1 this includes |
| 53 |
the `transfer-encoding` and `connection` headers. For HTTP/2 |
| 54 |
this includes the colon headers like `:status`. |
| 55 |
|
| 56 |
Cowboy will also remove protocol-specific headers from |
| 57 |
requests before passing them to stream handlers. Cowboy |
| 58 |
tries to hide the implementation details of all protocols |
| 59 |
as well as possible. |
| 60 |
|
| 61 |
=== Number of processes per connection |
| 62 |
|
| 63 |
By default, Cowboy will use one process per connection, |
| 64 |
plus one process per set of request/response (called a |
| 65 |
stream, internally). |
| 66 |
|
| 67 |
The reason it creates a new process for every request is due |
| 68 |
to the requirements of HTTP/2 where requests are executed |
| 69 |
concurrently and independently from the connection. The |
| 70 |
frames from the different requests end up interleaved on |
| 71 |
the single TCP connection. |
| 72 |
|
| 73 |
The request processes are never reused. There is therefore |
| 74 |
no need to perform any cleanup after the response has been |
| 75 |
sent. The process will terminate and Erlang/OTP will reclaim |
| 76 |
all memory at once. |
| 77 |
|
| 78 |
Cowboy ultimately does not require more than one process |
| 79 |
per connection. It is possible to interact with the connection |
| 80 |
directly from a stream handler, a low level interface to Cowboy. |
| 81 |
They are executed from within the connection process, and can |
| 82 |
handle the incoming requests and send responses. This is however |
| 83 |
not recommended in normal circumstances, as a stream handler |
| 84 |
taking too long to execute could have a negative impact on |
| 85 |
concurrent requests or the state of the connection itself. |
| 86 |
|
| 87 |
=== Date header |
| 88 |
|
| 89 |
Because querying for the current date and time can be expensive, |
| 90 |
Cowboy generates one 'Date' header value every second, shares it |
| 91 |
to all other processes, which then simply copy it in the response. |
| 92 |
This allows compliance with HTTP/1.1 with no actual performance loss. |
| 93 |
|
| 94 |
=== Binaries |
| 95 |
|
| 96 |
Cowboy makes extensive use of binaries. |
| 97 |
|
| 98 |
Binaries are more efficient than lists for representing |
| 99 |
strings because they take less memory space. Processing |
| 100 |
performance can vary depending on the operation. Binaries |
| 101 |
are known for generally getting a great boost if the code |
| 102 |
is compiled natively. Please see the HiPE documentation |
| 103 |
for more details. |
| 104 |
|
| 105 |
Binaries may end up being shared between processes. This |
| 106 |
can lead to some large memory usage when one process keeps |
| 107 |
the binary data around forever without freeing it. If you |
| 108 |
see some weird memory usage in your application, this might |
| 109 |
be the cause. |