socketstcperlangclient-servergen-fsm

Pass control from one gen_fsm to another


I'm creating a generic Erlang server that should be able to handle hundreds of client connections concurrently. For simplicity, let's suppose that the server performs for every client some basic computation, e.g., addition or subtraction of every two values which the client provides.

As a starting point, I'm using this tutorial for basic TCP client-server interaction. An excerpt that represents the supervision tree:

                +----------------+
                | tcp_server_app |
                +--------+-------+
                         | (one_for_one)
        +----------------+---------+
        |                          |
+-------+------+           +-------+--------+
| tcp_listener |           + tcp_client_sup |
+--------------+           +-------+--------+
                                   | (simple_one_for_one)
                             +-----|---------+
                           +-------|--------+|
                          +--------+-------+|+
                          |  tcp_echo_fsm  |+
                          +----------------+

I would like to extend this code and allow tcp_echo_fsm to pass the control over the socket to one out of two modules: tcp_echo_addition (to compute the addition of every two client values), or tcp_echo_subtraction (to compute the subtraction between every two client values).

The tcp_echo_fsm would choose which module to handle a socket based on the first message from the client, e.g., if the client sends <<start_addition>>, then it would pass control to tcp_echo_addition.

The previous diagram becomes:

                +----------------+
                | tcp_server_app |
                +--------+-------+
                         | (one_for_one)
        +----------------+---------+
        |                          |
+-------+------+           +-------+--------+
| tcp_listener |           + tcp_client_sup |
+--------------+           +-------+--------+
                                   | (simple_one_for_one)
                             +-----|---------+
                           +-------|--------+|
                          +--------+-------+|+
                          |  tcp_echo_fsm  |+
                          +----------------+
                                  |
                                  |
                 +----------------+---------+
         +-------+-----------+      +-------+--------------+
         | tcp_echo_addition |      + tcp_echo_subtraction |
         +-------------------+      +-------+--------------+

My questions are:

  1. Am I on the right path? Is the tutorial which I'm using a good starting point for a scalable TCP server design?

  2. How can I pass control from one gen_fsm (namely, tcp_echo_fsm) to another gen_fsm (either tcp_echo_addition or tcp_echo_subtraction)? Or better yet: is this a correct/clean way to design the server? This related question suggests that passing control between a gen_fsm and another module is not trivial and there might be something wrong with this approach.


Solution

  • For 2, you can use gen_tcp:controlling_process/2 to pass control of the tcp connection: http://erlang.org/doc/man/gen_tcp.html#controlling_process-2.

    For 1, I am not sure of the value of spawning a new module as opposed to handling the subtraction and addition logic as part of the defined states in your finite state machine. Doing so creates code which is now running outside of your supervision tree, so it's harder to handle errors and restarts. Why not define addition and subtraction as different states within your state machines handle that logic within those two states?

    You can create tcp_echo_fsm:subtraction_state/2,3 and tcp_echo_fsm:addition_state/2,3 to handle this logic and use your first message to transition to the appropriate state rather than adding complexity to your application.