flow_diagram.asciidoc 4.2 KB

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