summaryrefslogtreecommitdiff
path: root/src/buffering_proxy.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/buffering_proxy.erl')
-rw-r--r--src/buffering_proxy.erl20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/buffering_proxy.erl b/src/buffering_proxy.erl
index d2505701..fcb7b412 100644
--- a/src/buffering_proxy.erl
+++ b/src/buffering_proxy.erl
@@ -32,6 +32,8 @@
-export([mainloop/4, drain/2]).
-export([proxy_loop/3]).
+-define(HIBERNATE_AFTER, 5000).
+
%%----------------------------------------------------------------------------
start_link(M, A) ->
@@ -40,7 +42,8 @@ start_link(M, A) ->
ProxyPid = self(),
Ref = make_ref(),
Pid = spawn_link(
- fun () -> mainloop(ProxyPid, Ref, M,
+ fun () -> ProxyPid ! Ref,
+ mainloop(ProxyPid, Ref, M,
M:init(ProxyPid, A)) end),
proxy_loop(Ref, Pid, empty)
end).
@@ -48,14 +51,19 @@ start_link(M, A) ->
%%----------------------------------------------------------------------------
mainloop(ProxyPid, Ref, M, State) ->
- ProxyPid ! Ref,
NewState =
receive
{Ref, Messages} ->
- lists:foldl(fun (Msg, S) ->
- drain(M, M:handle_message(Msg, S))
- end, State, lists:reverse(Messages));
+ NewSt =
+ lists:foldl(fun (Msg, S) ->
+ drain(M, M:handle_message(Msg, S))
+ end, State, lists:reverse(Messages)),
+ ProxyPid ! Ref,
+ NewSt;
Msg -> M:handle_message(Msg, State)
+ after ?HIBERNATE_AFTER ->
+ erlang:hibernate(?MODULE, mainloop,
+ [ProxyPid, Ref, M, State])
end,
?MODULE:mainloop(ProxyPid, Ref, M, NewState).
@@ -89,4 +97,6 @@ proxy_loop(Ref, Pid, State) ->
waiting -> Pid ! {Ref, [Msg]}, empty;
Messages -> [Msg | Messages]
end)
+ after ?HIBERNATE_AFTER ->
+ erlang:hibernate(?MODULE, proxy_loop, [Ref, Pid, State])
end.