I am trying to figure out why my gen_server
crashes with a timeout since i am treating all possible cases:
module(wk).
-behaviour(gen_server).
-compile(export_all).
-record(state,{
limit,
count=0,
toSend
}).
start_link(ToSend,Limit)->
gen_server:start_link(?MODULE, {ToSend,Limit}, []).
init({ToSend,Limit})->
State=#state{toSend=ToSend,limit=Limit},
{ok,State}.
handle_call({process,Message},From,State)->
{reply,{processed,os:timestamp()},State};
handle_call(Message,From,State)->
self() ! {from_call,Message},
{noreply,State}.
handle_cast(Message,State=#state{count=C})->
self() ! {from_cast,Message},
{noreply,State}.
handle_info(Message,State=#state{count=C,limit=L,toSend=T})->
io:format("inside handle_info"),
T! {badrequest,Message},
Ret=if C>L -> {stop,State};
_ ->{noreply,State#state{count=C+1}}
end,
Ret.
As you can see this server can handles a number of limit
unknown messages , and as well as cast
messages.
Now my problem is with the handle_call
:
- If i send a message that fits the first case its ok and it replies back
When i send an unknown message using
gen_server:call(S,xx)
for example, i get a timeout error :exception exit: {timeout,{gen_server,call,[<0.102.0>,33]}} in function gen_server:call/2 (gen_server.erl, line 215)
Why is my server timing out ? I can see that after handle_call
it enters into handle_info
but why does it crash ?
Usage:
{ok,Y}=wk:start_link(self(),3).
gen_server:cast(Y,some_message). % works limit times and then crashes as expected
Y ! some_message % works limit times and then crashes as expected
gen_server:call(Y,some_message) % gets inside handle_info , since i get the io message, then times out