Subversion Repositories SE.SVN

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12 7u83 1
%% @author Bob Ippolito <bob@mochimedia.com>
2
%% @copyright 2010 Mochi Media, Inc.
3
%%
4
%% Permission is hereby granted, free of charge, to any person obtaining a
5
%% copy of this software and associated documentation files (the "Software"),
6
%% to deal in the Software without restriction, including without limitation
7
%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
%% and/or sell copies of the Software, and to permit persons to whom the
9
%% Software is furnished to do so, subject to the following conditions:
10
%%
11
%% The above copyright notice and this permission notice shall be included in
12
%% all copies or substantial portions of the Software.
13
%%
14
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
%% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
%% DEALINGS IN THE SOFTWARE.
21
 
22
%% @doc MochiWeb acceptor.
23
 
24
-module(mochiweb_acceptor).
25
 
26
-author('bob@mochimedia.com').
27
 
28
-include("internal.hrl").
29
 
30
-export([init/4, start_link/3, start_link/4]).
31
 
32
-define(EMFILE_SLEEP_MSEC, 100).
33
 
34
start_link(Server, Listen, Loop) ->
35
    start_link(Server, Listen, Loop, []).
36
 
37
start_link(Server, Listen, Loop, Opts) ->
38
    proc_lib:spawn_link(?MODULE, init,
39
			[Server, Listen, Loop, Opts]).
40
 
41
do_accept(Server, Listen) ->
42
    T1 = os:timestamp(),
43
    case mochiweb_socket:transport_accept(Listen) of
44
      {ok, Socket} ->
45
	  gen_server:cast(Server,
46
			  {accepted, self(),
47
			   timer:now_diff(os:timestamp(), T1)}),
48
	  mochiweb_socket:finish_accept(Socket);
49
      Other -> Other
50
    end.
51
 
52
init(Server, Listen, Loop, Opts) ->
53
    case catch do_accept(Server, Listen) of
54
      {ok, Socket} -> call_loop(Loop, Socket, Opts);
55
      {error, Err}
56
	  when Err =:= closed orelse
57
		 Err =:= esslaccept orelse Err =:= timeout ->
58
	  exit(normal);
59
      Other ->
60
	  %% Mitigate out of file descriptor scenario by sleeping for a
61
	  %% short time to slow error rate
62
	  case Other of
63
	    {error, emfile} ->
64
		receive  after ?EMFILE_SLEEP_MSEC -> ok end;
65
	    _ -> ok
66
	  end,
67
	  error_logger:error_report([{application, mochiweb},
68
				     "Accept failed error",
69
				     lists:flatten(io_lib:format("~p",
70
								 [Other]))]),
71
	  exit({error, accept_failed})
72
    end.
73
 
74
call_loop({M, F}, Socket, Opts) when is_atom(M) ->
75
    M:F(Socket, Opts);
76
call_loop({M, F, [A1]}, Socket, Opts) when is_atom(M) ->
77
    M:F(Socket, Opts, A1);
78
call_loop({M, F, A}, Socket, Opts) when is_atom(M) ->
79
    erlang:apply(M, F, [Socket, Opts | A]);
80
call_loop(Loop, Socket, Opts) -> Loop(Socket, Opts).