I’m trying out the new(ish) gen_{server,statem}:send_request
, which looks very nice. It looked like I could replace a bunch of boiler plate that I usually write to manage concurrent request/replies within some gen_*
services.
My try out code (in a gen_server), is calling another process (a gen_statem in this case) that is doing some difficult work (2 + 2) which might take a while. An ideal candidate for an async send_request
, {timeout, idle}
followed by a receive_response
.
handle_call(do_work, From, #{requests := Requests} = Data) ->
{noreply,
Data#{requests := gen_statem:send_request(
worker,
{add, 2, 2},
From,
Requests)}};
The worker is a gen_statem
doing the heavy lifting which looks like:
handle_event({call, From}, {add, X, Y}, _, _) ->
{keep_state_and_data, {reply, From, X + Y}}.
At a convenient future point I would call receive_response
and get the asynchronous result. Instead I get a crash:
=WARNING REPORT==== 16-Jul-2022::15:47:56.846636 === <0.590.0> gen_server:try_dispatch/4:1127
** Undefined handle_info in client_gen_server
** Unhandled message: {[alias|#Ref<0.2932551881.3440967681.24357>],4}
The reply is coming back as a vanilla info
message (via gen:reply/2).
reply({_To, [alias|Alias] = Tag}, Reply) when is_reference(Alias) ->
Alias ! {Tag, Reply}, ok;
It doesn’t look like I can use send_request
and receive_response
within a gen_*
process?
Is this expected behaviour, or am I doing something daft?
Erlang/OTP v25.0.2.
Thanks!
Peter.