#pragma once #include #include #include #include #include #include #include namespace DB { namespace Loop { static const UInt8 RUN = 1; static const UInt8 STOP = 2; } using SubscriptionPtr = std::unique_ptr; using LockPtr = std::unique_ptr>; class NATSHandler { public: NATSHandler(uv_loop_t * loop_, Poco::Logger * log_); ~NATSHandler(); /// Loop for background thread worker. void startLoop(); /// Loop to wait for small tasks in a non-blocking mode. /// Adds synchronization with main background loop. void iterateLoop(); LockPtr setThreadLocalLoop(); void stopLoop(); bool loopRunning() const { return loop_running.load(); } void updateLoopState(UInt8 state) { loop_state.store(state); } UInt8 getLoopState() { return loop_state.load(); } natsOptions * getOptions() { return opts; } private: uv_loop_t * loop; natsOptions * opts = nullptr; Poco::Logger * log; std::atomic loop_running; std::atomic loop_state; std::mutex startup_mutex; }; }