An example of using an unregistered, dynamically created gen_server's?

The tutorials are enough to work with gen_servers, whose names are specified in the OTP application. However, I could not find a good example of dynamically spawning servers that are not registered (not named). Can anyone point out a good, simple example? Not ejabberd, for example, where there is a lot to confuse the main idea I'm trying to get to.

Thanks.

+4
source share
3 answers

I have a dynamic spawning of workers in a surveillance tree that happens at http://github.com/noss/iserve . The iserve application has a registered process that I call iserve_master , it is monitored along with a simple mode manager for one.

iserve_master designed to request the start of the http server. It can have several servers if they are tied to unique addresses. Running servers build a name for themselves, but to simplify debugging, it generates an atom using the port name.

A server is an โ€œeternalโ€ loop waiting for iserve_socket call back after accepting a connection. It runs the first in init.

A running socket is not monitored because it cannot be restarted without losing the connection to the http connection. And I see this as part of a function that doesn't care. Fire and forget.

This is far from ideal, but an architecture that I like.

+1
source

The simplest example:
leave the first argument {local, name} in gen_server: start: i.e. assuming your module is called mod :

 start(ArgX) -> gen_server:start(mod, [ArgX], []). 

then you do:

 > {ok, Pid} = gen_server:start(mod, [66], []). 

and gen_server is up and running.
each call to this function starts a new process without the name gen_server. note that this example uses start (rather than start_link ) for offline use (outside the context of a supervisor).

+2
source

the rabbitmq-shovel plugin seems to have an example of this - see rabbit_shovel_sup.erl and rabbit_shovel_worker.erl. It spawns gen_server workflows based on the settings in the configuration file.

0
source

Source: https://habr.com/ru/post/1301153/


All Articles