snake server / join subscription background thread in the ConnectionState destructor + attach cobra message subscription id to the connection state instead of having it be a local reference that gets unbound

This commit is contained in:
Benjamin Sergeant 2020-07-27 17:35:03 -07:00
parent 5daa59f9f3
commit af2f31045d
3 changed files with 13 additions and 15 deletions

View File

@ -17,6 +17,15 @@ namespace snake
class SnakeConnectionState : public ix::ConnectionState
{
public:
virtual ~SnakeConnectionState()
{
if (subscriptionThread.joinable())
{
subscriptionRedisClient.stop();
subscriptionThread.join();
}
}
std::string getNonce()
{
return _nonce;
@ -52,19 +61,11 @@ namespace snake
return _redisClient;
}
void cleanup()
{
if (subscriptionThread.joinable())
{
subscriptionRedisClient.stop();
subscriptionThread.join();
}
}
// We could make those accessible through methods
std::thread subscriptionThread;
std::string appChannel;
std::string subscriptionId;
uint64_t id;
std::unique_ptr<StreamSql> streamSql;
ix::RedisClient subscriptionRedisClient;
ix::RedisClient::OnRedisSubscribeResponseCallback onRedisSubscribeResponseCallback;

View File

@ -198,9 +198,8 @@ namespace snake
std::string filterStr = pdu["body"]["filter"];
}
state->streamSql = std::make_unique<StreamSql>(filterStr);
int id = 0;
state->onRedisSubscribeCallback = [&ws, &id, state](const std::string& messageStr) {
state->id = 0;
state->onRedisSubscribeCallback = [&ws, state](const std::string& messageStr) {
auto msg = nlohmann::json::parse(messageStr);
msg = msg["body"]["message"];
@ -212,7 +211,7 @@ namespace snake
nlohmann::json response = {
{"action", "rtm/subscription/data"},
{"id", id++},
{"id", state->id++},
{"body",
{{"subscription_id", state->subscriptionId}, {"position", "0-0"}, {"messages", {msg}}}}};

View File

@ -99,8 +99,6 @@ namespace snake
ss << "Closed connection"
<< " code " << msg->closeInfo.code << " reason "
<< msg->closeInfo.reason << std::endl;
state->cleanup();
}
else if (msg->type == ix::WebSocketMessageType::Error)
{