Forked from zhongwencool/gen_server_call_timeout.erl
Last active
November 12, 2016 08:24
-
-
Save redink/932dbba64883501d424d06fa6fb2c889 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(gen_server_call_timeout). | |
%% API | |
-behaviour(gen_server). | |
%% API | |
-export([start/0]). | |
-export([run/2]). | |
-export([multi_run/0]). | |
-export([i/0]). | |
%% gen_server callbacks | |
-export([ | |
init/1, | |
handle_call/3, | |
handle_cast/2, | |
handle_info/2, | |
code_change/3, | |
terminate/2 | |
]). | |
-record(state, {count = 0}). | |
%% =================================================================== | |
%% API functions | |
%% =================================================================== | |
start() -> | |
gen_server:start({local, ?MODULE}, ?MODULE, [], []). | |
i() -> | |
gen_server:call(?MODULE, i). | |
run(ExecuteTime, Timeout) -> | |
try | |
gen_server:call(?MODULE, {run, ExecuteTime}, Timeout) | |
catch | |
exit:{timeout, _CallInfo} -> | |
timer:sleep(ExecuteTime - Timeout + 100), | |
receive X -> | |
io:format("~p after timeout(~p) receive:~p~n", [self(), Timeout, X]) | |
end | |
end. | |
multi_run() -> | |
spawn(fun() -> | |
io:format("~p start run(executetime, timeout)(~p, ~p)~n", [self(), 2000, 1000]), | |
run(2000, 1000) end), | |
spawn(fun() -> | |
io:format("~p start run(execetetime, timeout)(~p, ~p)~n", [self(), 1000, 10]), | |
run(1000, 10) end), | |
spawn(fun() -> | |
io:format("~p start withnot try catch run(execetetime, timeout)(~p, ~p)~n", [self(), 1000, 10]), | |
gen_server:call(?MODULE, {run, 1000}, 10) | |
end), | |
ok. | |
%% =================================================================== | |
%% gen_server callbacks | |
%% =================================================================== | |
init([]) -> | |
erlang:process_flag(trap_exit, true), | |
{ok, #state{}}. | |
handle_call(i, _From, State) -> | |
{reply, State, State}; | |
handle_call({run, Time}, From, State) -> | |
{monitored_by, MonitoredBy} = erlang:process_info(erlang:self(), monitored_by), | |
case lists:member(From, MonitoredBy) of | |
true -> | |
timer:sleep(Time), | |
Result = lists:flatten(io_lib:format("i'm execete ~B ms", [Time])), | |
{reply, Result, State#state{count= State#state.count + 1}}; | |
_ -> | |
{noreply, State} | |
end; | |
handle_call(Msg, From, State) -> | |
{stop, {shutdown, {unexpeted_call, Msg, From}}, State}. | |
handle_cast(Msg, State) -> | |
{stop, {shutdown, {unexpeted_cast, Msg}}, State}. | |
handle_info(Msg, State) -> | |
{stop, {shutdown, {unexpeted_info, Msg}}, State}. | |
terminate(Reason, _State) -> | |
io:format("~p: terminate: Reason:~p", [?MODULE, Reason]), | |
ok. | |
code_change(_OldVsn, State, _Extra) -> | |
{ok, State}. | |
%% =================================================================== | |
%% Local Functions | |
%% =================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment