Bercovici Adrian
Bercovici Adrian

Reputation: 9360

Why is my gen_server not respecting pattern matching?

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.

Upvotes: 1

Views: 99

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170723

Because you have wrong parameter order: it should be Module:handle_call(Request, From, State). So the first pattern would only match when From is state.

Upvotes: 3

Related Questions