Hi everybody, Iam new to Cowboy Server I have read enough code to understand how it handle a simple request but I can’t understand how it implements the HTTP Protocol :
After the ranch_conns_sup receive the Accepted Socket it will Spawn a new Process to be the cowboy_http Handler and after this last send the Response to the Client it should close the connection Immediately but what I see is that it will loop again (loop(info(State, StreamID, Msg))) so why ?
the cowboy_http Process in its #state have a variable last_streamid=maps:get(max_keepalive, Opts, 1000)} and that means that this Process may receive up to 1000 requests (streams) : should not this Process receive just 1 request, reply to, close the connection and terminate normally ?
Thank you @asabil I know that but if you look we have in all cases loop(info(State, StreamID, Msg)) and that means that the Process will always receive other requests
@AstonJ I try just to understand how the Erlang HTTP Server work exactly, after that I want to customize it to meet my goals as a part of a Generic Erlang Server
Okey, I have read the code Carefully Again and I think I find something in the connection function
1-If the Server is configured to handle just One Request and after that it should Close the Connection (last_streamid=1), then it will Send <<"connection : close" >> in its Reply Header and Logically the Client will not use this Socket again (Regardless of handler Module specification of <<"connection">> Value in headers parameter in its init function, for example this : headers here is the second Parameter in the function cowboy_req:reply and in this case there is no headers)
2-If the Server is configured to handle Many Requests per Socket and Keep-Alive the Connection and the handler Module want to Close the connection Immediately After Sending the Reply (have <<"connection : close">> in headers Parameter in its init function), then the Server will set (last_streamid=StreamID) and the Client Logically will not use this Socket again.
Okey that’s fine, in the two cases the HTTP Process will loop again with always this match
Okey it will Never receive Data from this Socket and even if something happens Abnormally and the Client Send Data via this Socket Again, the Process will Ignore it and Loop Again so :
Why the Server Keeps this Process Alive ? it’s a Waste of Resources especially with a lot of Connections(Even if the Process will do Nothing it still Keep Memory Space) so Why don’t just Terminate this Process ?