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:
		| @@ -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; | ||||
|   | ||||
| @@ -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}}}}}; | ||||
|  | ||||
|   | ||||
| @@ -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) | ||||
|                 { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user