erpc-io / eRPC

Efficient RPCs for datacenter networks
https://erpc.io/
Other
835 stars 137 forks source link

Multiple erpc::Rpc object in one thread #109

Closed Stuart0l closed 6 months ago

Stuart0l commented 6 months ago

I'm trying to connect to multiple erpc servers from one client (single thread). I used two methods, one is to use a separate erpc::Rpc object for each connection like this:

    string client_uri = "10.10.1.3:31851";
    auto *nexus = new erpc::Nexus(client_uri);

    auto *rpc1 = new erpc::Rpc<erpc::CTransport>(nexus, &complete1, 1, cli_sm_handler, 1);
    int ses_num1 = rpc1->create_session("10.10.1.3:31850", 0);

    while (!rpc1->is_connected(ses_num1)) {
        rpc1->run_event_loop_once();
    }

    auto *rpc2 = new erpc::Rpc<erpc::CTransport>(nexus, &complete2, 3, cli_sm_handler, 1);
    int ses_num2 = rpc2->create_session("10.10.1.3:31850", 2);

    while (!rpc2->is_connected(ses_num2)) {
        rpc2->run_event_loop_once();
    }

but this does not work. The rpc request can't be delivered to the server. The other way is to use the same erpc::Rpc object for both connections like this:

    string client_uri = "10.10.1.3:31851";
    auto *nexus = new erpc::Nexus(client_uri);

    auto *rpc1 = new erpc::Rpc<erpc::CTransport>(nexus, &complete1, 1, cli_sm_handler, 1);
    int ses_num1 = rpc1->create_session("10.10.1.3:31850", 0);

    while (!rpc1->is_connected(ses_num1)) {
        rpc1->run_event_loop_once();
    }

    int ses_num2 = rpc1->create_session("10.10.1.3:31850", 2);

    while (!rpc1->is_connected(ses_num2)) {
        rpc1->run_event_loop_once();
    }

This way it works and rpc requests can be delivered to servers. So is the second way the correct way to use and why the first way fails to work?

Thanks!

anujkaliaiitd commented 6 months ago

The second way is correct and recommended.

There might be a bug in eRPC that prevents the first way from working, caused by the use of thread-local variables for tracking (https://github.com/erpc-io/eRPC/blob/615aa9373e74cc270adae51267e0a1912848bbd0/src/util/tls_registry.h#L10C19-L10C28).

Stuart0l commented 6 months ago

Hi Anuj, thanks for your answer!