Erlang Cowboy HTTP request life cycle

Source: Internet
Author: User

Erlang Cowboy HTTP request life cycle

Translated from:

http://ninenines.eu/docs/en/cowboy/1.0/guide/http_req_life/


the life cycle of the request

This chapter explains the steps of the HTTP request before the server response, as well as the details of the cowboy implementation.

Request/response

As you learned, the HTTP client connects to the server, sends a resource request (requests), and the server sends a response (response) that contains the resources that can be obtained.

Before the server sends a resource, it needs some steps to read the request, identify the resource, prepare to send the resource, and include other associated operations, such as write logs.

In cowboy, the request is routed by:

This figure shows the default processing middleware (middlewares), which can be set in different ways. The dark green (double box) identifies the code points that the user can insert (hooks) to implement. Light green is a cowboy code that can be set as needed.

acceptoris part of the server, it accepts the connection, and then creates an Erlang process to handle it.

parser接着开始读取Socket connection, which is processed when the request arrives, until the socket is closed.

The response can be returned in multiple places over the entire period of the request. If Cowboy cannot resolve the request, it will end processing the return error. If the route router cannot find the resource, it returns a not found error. Your own code can return a response at any time.

When the response is sent, you can choose to modify it, or in the onresponse的回调中实现代码 . By default, the response is sent directly to the client.

And then?

The specific behavior relies on the protocol used.

Http/1.0 can only process a single request with one connection, so cowboy closes the connection immediately after the response is sent.

http/1.1 allows the client to request the server and maintain this connection. This mechanism is explained below.

SPDY is used to send multiple asynchronous requests on the same connection, as described below.

Keep-alive (http/1.1)

In the http/1.1 protocol, the connection can always be opened and a continuous request occurs. This mechanism is called keep-alive .

When a client sends a request to the server, it contains a header that identifies whether the connection needs to be kept open. The server is either acceptable or unacceptable, and returns its choice in the head of the response.

The http/1.1 Requests,cowboy automatically includes this header in all responses. You can also close the socket connection if you wish. When Cowboy sees you set the connection: close header, it will not overwrite your settings, and when the response is sent, the connection will be closed.

The following code forces cowboy to close the connection:

{OK, Req2} = cowboy_req:reply ($, [    {<< "Connection" >>, << "Close" >>},], << Closing The socket in 3.. 2.. 1.. " >>, Req).

Cowboy only accepts a certain number of requests on the same connection, the default is 100 requests. When starting an HTTP listener, this number can be changed as follows ( max_keepalive):

Cowboy:start_http (My_http_listener, +, [{port, 8080}], [        {env, [{Dispatch, Dispatch}]},        {max_keepalive, 5}] ).

Cowboy implements the KeepAlive keep-alive by reusing the same process for all requests. This cowboy can save memory. This works because most code has no side-effects on subsequent requests. But it also means that if your code has side effects, you have to do cleanup work, and that's terminate/3 what the function is doing. Pipelining (http/1.1)

HTTP is a sequential protocol in which a client sends a request and then waits for the server to respond. Because of the sockets mode of operation, HTTP does not prevent customers from sending more requests and does not force customers to wait for a response. The server still processes the request sequentially, returning the response in the same order.

This mechanism is called pipelining. When a customer requests more resources within a single time, it can reduce latency, for example, when the browser requests a static file.

This mode is handled automatically by the server.

Asynchronous request requests (SPDY)

At Spdy, the customer can send the request at any time, and the service can also return the response at any point. This means that the client does not have to wait for the request to be completely sent to send another request, which makes it possible for the interleaving to send the request. The same is true for the service side. Responses can also be sent in a different order.

Because the request and response are completely asynchronous, Cowboy creates a new process for each request that is managed by another process that processes the connection. The Spdy service can also decide whether to send a resource to a customer before the request, which is especially useful for sending static files associated with an HTML page because of reduced response latency. However, the current cowboy does not support this mechanism.

Erlang Cowboy HTTP request life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.