mochiweb 不一定每个请求是一个新的process
文章分类:综合技术 关键字: mochiweb
今天试了一下mochiweb,这个东东的文档实在是少的可怜,推荐一个不错的tutorial:
http://erlang-china.org/start/mochiweb_intro.html
做了一个简单的comet测试,在firefox下浏览时发现每个http请求处理的process id固定在2个,而且进程字典的数据也可以跨请求共享,找了下mochiweb的源码,发现其在http请求结束时,支持tcp连接复用(http1.1),代码如下:
mochiweb_http.erl
- after_response(Body, Req) ->
- Socket = Req:get(socket),
- case Req:should_close() of
- true ->
- gen_tcp:close(Socket),
- exit(normal);
- false ->
- Req:cleanup(),
- ?MODULE:loop(Socket, Body)
- end.
mochiweb_request.erl
- %% @spec should_close() -> bool()
- %% @doc Return true if the connection must be closed. If false, using
- %% Keep-Alive should be safe.
- should_close() ->
- ForceClose = erlang:get(mochiweb_request_force_close) =/= undefined,
- DidNotRecv = erlang:get(mochiweb_request_recv) =:= undefined,
- ForceClose orelse Version < {1, 0}
- %% Connection: close
- orelse get_header_value("connection") =:= "close"
- %% HTTP 1.0 requires Connection: Keep-Alive
- orelse (Version =:= {1, 0}
- andalso get_header_value("connection") =/= "Keep-Alive")
- %% unread data left on the socket, can't safely continue
- orelse (DidNotRecv
- andalso get_header_value("content-length") =/= undefined
- andalso list_to_integer(get_header_value("content-length")) > 0)
- orelse (DidNotRecv
- andalso get_header_value("transfer-encoding") =:= "chunked").
So, it's clear now.
没有评论:
发表评论