Hello I am trying to figure out why my implementation of gen_server
is not respecting pattern matching:
If I run gen_server:call(ServerRef,state)
it gets in the second pattern of handle_call
and i do not understand why , since the the first pattern should get hit.
Is there a problem when sending atoms
?
Module
-module(wk).
-behaviour(gen_server).
-compile(export_all).
-record(state,{
limit,
processed=[],
unknown=[],
counter=0
}).
start_link(Limit)->
gen_server:start_link(?MODULE, Limit, []).
start(Limit)->
gen_server:start(?MODULE,Limit,[]).
init(Limit)->
State=#state{limit=Limit},
{ok,State}.
handle_call(From,state,State)->
{reply,State,State};
handle_call(From,Message,State=#state{processed=P,limit=L,counter=C})->
Reply=if C=:=L;C>L -> exit(consumed);
C<L -> {{processed,self(),os:timestamp()},Message}
end,
{reply,Reply,State#state{counter=C+1,processed=[Message,P]}}.
handle_info(Message,State=#state{unknown=U})->
{noreply,State#state{unknown=[Message|U]}}.
Calling:>gen_server:call(ServerRef,state)
enters into the second pattern too.
1条答案
按热度按时间i1icjdpr1#
因为参数顺序错误:它应该是
Module:handle_call(Request, From, State)
。因此第一个模式只有在From
是state
时才匹配。