PageRenderTime 18ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mochiweb_acceptor.erl

http://github.com/basho/mochiweb
Erlang | 83 lines | 50 code | 10 blank | 23 comment | 0 complexity | 34854b5f17df5f08c71a03118685cbfc MD5 | raw file
Possible License(s): MIT
  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. %% @doc MochiWeb acceptor.
  22. -module(mochiweb_acceptor).
  23. -author('bob@mochimedia.com').
  24. -include("internal.hrl").
  25. -export([start_link/3, start_link/4, init/4]).
  26. -define(EMFILE_SLEEP_MSEC, 100).
  27. start_link(Server, Listen, Loop) ->
  28. start_link(Server, Listen, Loop, []).
  29. start_link(Server, Listen, Loop, Opts) ->
  30. proc_lib:spawn_link(?MODULE, init, [Server, Listen, Loop, Opts]).
  31. do_accept(Server, Listen) ->
  32. T1 = os:timestamp(),
  33. case mochiweb_socket:transport_accept(Listen) of
  34. {ok, Socket} ->
  35. gen_server:cast(Server, {accepted, self(), timer:now_diff(os:timestamp(), T1)}),
  36. mochiweb_socket:finish_accept(Socket);
  37. Other ->
  38. Other
  39. end.
  40. init(Server, Listen, Loop, Opts) ->
  41. case catch do_accept(Server, Listen) of
  42. {ok, Socket} ->
  43. call_loop(Loop, Socket, Opts);
  44. {error, Err} when Err =:= closed orelse
  45. Err =:= esslaccept orelse
  46. Err =:= timeout ->
  47. exit(normal);
  48. Other ->
  49. %% Mitigate out of file descriptor scenario by sleeping for a
  50. %% short time to slow error rate
  51. case Other of
  52. {error, emfile} ->
  53. receive
  54. after ?EMFILE_SLEEP_MSEC ->
  55. ok
  56. end;
  57. _ ->
  58. ok
  59. end,
  60. error_logger:error_report(
  61. [{application, mochiweb},
  62. "Accept failed error",
  63. lists:flatten(io_lib:format("~p", [Other]))]),
  64. exit({error, accept_failed})
  65. end.
  66. call_loop({M, F}, Socket, Opts) ->
  67. M:F(Socket, Opts);
  68. call_loop({M, F, [A1]}, Socket, Opts) ->
  69. M:F(Socket, Opts, A1);
  70. call_loop({M, F, A}, Socket, Opts) ->
  71. erlang:apply(M, F, [Socket, Opts | A]);
  72. call_loop(Loop, Socket, Opts) ->
  73. Loop(Socket, Opts).