ClickHouse/src/Storages/RabbitMQ/RabbitMQHandler.h

62 lines
1.4 KiB
C++
Raw Normal View History

#pragma once
2020-06-02 13:15:53 +00:00
#include <thread>
#include <memory>
2020-06-02 13:15:53 +00:00
#include <mutex>
#include <amqpcpp.h>
#include <amqpcpp/linux_tcp.h>
#include <common/types.h>
#include <amqpcpp/libuv.h>
namespace DB
{
2020-08-08 16:45:52 +00:00
namespace Loop
{
static const UInt8 RUN = 1;
static const UInt8 STOP = 2;
}
2021-09-10 10:28:09 +00:00
using ChannelPtr = std::unique_ptr<AMQP::TcpChannel>;
2021-05-01 18:00:43 +00:00
class RabbitMQHandler : public AMQP::LibUvHandler
{
public:
2020-06-30 01:48:11 +00:00
RabbitMQHandler(uv_loop_t * loop_, Poco::Logger * log_);
2020-08-28 08:52:02 +00:00
void onError(AMQP::TcpConnection * connection, const char * message) override;
void onReady(AMQP::TcpConnection * connection) override;
2020-06-29 15:41:17 +00:00
2021-05-04 16:26:47 +00:00
/// Loop for background thread worker.
void startLoop();
2021-05-04 16:26:47 +00:00
/// Loop to wait for small tasks in a non-blocking mode.
2021-05-04 19:57:45 +00:00
/// Adds synchronization with main background loop.
2020-07-02 16:44:04 +00:00
void iterateLoop();
2020-08-28 08:52:02 +00:00
2021-05-04 16:26:47 +00:00
/// Loop to wait for small tasks in a blocking mode.
2021-05-04 19:57:45 +00:00
/// No synchronization is done with the main loop thread.
2021-05-05 07:52:21 +00:00
void startBlockingLoop();
2021-05-04 16:26:47 +00:00
void stopLoop();
2021-09-10 10:28:09 +00:00
bool connectionRunning() { return connection_running.load(); }
2020-08-28 08:52:02 +00:00
bool loopRunning() { return loop_running.load(); }
2020-08-08 16:45:52 +00:00
void updateLoopState(UInt8 state) { loop_state.store(state); }
UInt8 getLoopState() { return loop_state.load(); }
private:
uv_loop_t * loop;
Poco::Logger * log;
2020-08-28 08:52:02 +00:00
std::atomic<bool> connection_running, loop_running;
2020-08-08 16:45:52 +00:00
std::atomic<UInt8> loop_state;
2020-07-02 16:44:04 +00:00
std::mutex startup_mutex;
};
2021-09-10 10:28:09 +00:00
using RabbitMQHandlerPtr = std::shared_ptr<RabbitMQHandler>;
}