tgockel / zookeeper-cpp

A ZooKeeper client for C++.
http://tgockel.github.io/zookeeper-cpp/
Apache License 2.0
148 stars 40 forks source link

Connection loss management #125

Open GuillaumeDua opened 3 years ago

GuillaumeDua commented 3 years ago

Idea : zk::connection should provide a way to reconnect after a connection is loss,
as well as an atomic state

Considering the following example :

zk::connection con = zk::connection::connection("zk://addr:port/?opts");

// somewhere else ...

switch (con->state())
{  // handle some connection loss cases
   case zk::state::closed: [[fallthrough]];
   case zk::state::expired_session:
   {
    reset_connection(); // smthg like `con = zk::connection::connection("zk://addr:port/?opts");`
    break;
   }
   default:
    break;
}

In the sample above, the issue is the switch/case is error-prone, as erasing con content may result in invalid con->state() read in a concurrent context.
This force the user to create some blocking operation using memory barriers and perhaps condition_variales, to ensure no wrong states are evaluated during connection reset.