How to call (and sleep) on gen_tcp: receive and process system messages at the same time?

I'm still new to the erlang / otp world, so I think this is a pretty simple question. However, I would like to know what is the right way to do the following.

I currently have an application with a senior manager. The latter will control workers who call gen_tcp: accept (sleep on it) and then spawn a process for each received connection. Note. To this question, it does not matter where the listen () function is executed.

My question is about the right way to get these workers (those sleeping on gen_tcp: accept) to respect otp design principles so that they can handle system messages (handle shutdowns, snooping, etc.), as per what I read here: http://www.erlang.org/doc/design_principles/spec_proc.html

So,

  • Is it possible to use one of the available types of behavior, for example, gen_fsm or gen_server? My guess would be wrong, due to the blocking call to gen_tcp: accept / 1. Can this be done by specifying a timeout for reception? If so, where should I put the accept () call?
  • Or do I need to encode it from scratch (i.e. do not use existing behavior), like the examples in the link above? In this case, I was thinking of the main loop that calls gen_tcp: accept / 2 instead of gen_tcp: accept / 1 (i.e.: timeout) and immediately after that the code receives a block, so I can process system messages. Is this correct / acceptable?

Thanks in advance:)

+3
source share
3 answers

I really found the answer in another question: Non-blocking TCP server using OTP principles and here http://20bits.com/article/erlang-a-generalized-tcp-server

EDIT . The specific answer that was useful to me was: fooobar.com/questions/756491 / ...

0
source

Erlang , , accept/{1,2}.

, gen_server , . - (gen_tcp:accept/2), ( ), .

, , . , , , . , , . , supervisor:terminate_child/2 , . supervisor:restart_child/2 - + .

, , cowboy . http, , .

+1

gen_server : https://github.com/alinpopa/qerl/blob/master/src/qerl_conn_listener.erl.

, tcp (, stop (Pid) → gen_server: cast (Pid, {close}).)

, Alin

0