Search code examples
httperlangkeep-alive

Persistent/Keepalive HTTP client implementation of Erlang


I am confused about the keep-alive feature of Erlang inets and also the ibrowse http client(latest version). According to the RFC:

8.1.2.2 Pipelining   
   A client that supports persistent connections MAY "pipeline" its
   requests (i.e., send multiple requests without waiting for each
   response). A server MUST send its responses to those requests in the
   same order that the requests were received.

   Clients which assume persistent connections and pipeline immediately
   after connection establishment SHOULD be prepared to retry their
   connection if the first pipelined attempt fails. If a client does
   such a retry, it MUST NOT pipeline before it knows the connection is
   persistent. Clients MUST also be prepared to resend their requests if
   the server closes the connection before sending all of the
   corresponding responses.

   Clients SHOULD NOT pipeline requests using non-idempotent methods or
   non-idempotent sequences of methods (see section 9.1.2). Otherwise, a
   premature termination of the transport connection could lead to
   indeterminate results. A client wishing to send a non-idempotent
   request SHOULD wait to send that request until it has received the
   response status for the previous request.

There are two modes, 'pipeline' and 'keep-alive', based on the persistent connections, and the difference between them is that requests could be sent on the same connection without waiting for the response of earlier one when 'pipeline' is used. In other words, we should wait for each response before sending other requests on the same connection when use 'keep-alive'.

As a result, I thought the 'keep-alive' implementation should be like:

%% each httpc_handler(gen_server) maintains a persistent connection 
%% with type of keep-alive
httpc_handler:handle_request(Request, keepalive) ->
        %% check if there is request not finished yet
        case is_there_old_request()  of
            true -> queue:in(RQueue, Request);
            %% then send the request
            false -> gen_tcp:send(Request)
        end.

httpc_handler:handle_response(Response) ->
    send_back(Response),
    case queue:out(RQueue) of
        undefined -> ...;
        %% send the next request
        Request -> gen_tcp:send(Request), ...
    end.

But in fact, the implementation of inets and ibrowse is like:

 httpc_handler:handle_request(Request, keepalive) ->
            %% send without check
            gen_tcp:send(Request),
            case is_there_old_request()  of
                true -> queue:in(RQueue, Request);
                false -> ...
            end.
    
 httpc_handler:handle_response(Response) ->
        send_back(Response),
        case queue:out(RQueue) of
            undefined -> ...;
            Request -> receive_next_response
        end.

which I think it's actually the 'pipeline' mode without idempotent limit.

So, I've missed something in the code, or just misunderstood the RFC?


Solution

  • The problem has already been fixed by the commit inets httpc: Corrected separation of pipelining and .., in Erlang/OTP R16.