ClickHouse/src/Storages/RabbitMQ/RabbitMQHandler.cpp

76 lines
1.6 KiB
C++
Raw Normal View History

#include <common/logger_useful.h>
#include <Storages/RabbitMQ/RabbitMQHandler.h>
namespace DB
{
2020-06-04 06:22:53 +00:00
enum
{
2020-06-07 11:14:05 +00:00
Lock_timeout = 50,
Loop_stop_timeout = 200
2020-06-04 06:22:53 +00:00
};
RabbitMQHandler::RabbitMQHandler(event_base * evbase_, Poco::Logger * log_) :
LibEventHandler(evbase_),
evbase(evbase_),
log(log_)
{
2020-06-07 11:14:05 +00:00
tv.tv_sec = 0;
tv.tv_usec = Loop_stop_timeout;
}
2020-06-05 14:27:56 +00:00
void RabbitMQHandler::onError(AMQP::TcpConnection * connection, const char * message)
{
2020-05-26 20:43:20 +00:00
LOG_ERROR(log, "Library error report: {}", message);
2020-06-01 20:48:24 +00:00
2020-06-04 06:22:53 +00:00
if (!connection->usable() || !connection->ready())
2020-05-31 09:34:57 +00:00
{
2020-06-04 06:22:53 +00:00
LOG_ERROR(log, "Connection lost completely");
2020-05-31 09:34:57 +00:00
}
stop();
}
2020-06-09 21:52:06 +00:00
void RabbitMQHandler::startConsumerLoop(std::atomic<bool> & loop_started)
{
2020-06-04 06:22:53 +00:00
/* The object of this class is shared between concurrent consumers (who share the same connection == share the same
2020-06-11 20:05:35 +00:00
* event loop and handler). But the loop should not be attempted to start if it is already running.
*/
2020-06-04 06:22:53 +00:00
if (mutex_before_event_loop.try_lock_for(std::chrono::milliseconds(Lock_timeout)))
{
loop_started.store(true);
stop_scheduled = false;
2020-06-09 21:52:06 +00:00
event_base_loop(evbase, EVLOOP_NONBLOCK);
2020-06-04 06:22:53 +00:00
mutex_before_event_loop.unlock();
}
}
2020-06-07 11:14:05 +00:00
void RabbitMQHandler::startProducerLoop()
{
event_base_loop(evbase, EVLOOP_NONBLOCK);
}
void RabbitMQHandler::stop()
{
2020-06-11 20:05:35 +00:00
if (mutex_before_loop_stop.try_lock())
2020-06-04 06:22:53 +00:00
{
event_base_loopbreak(evbase);
mutex_before_loop_stop.unlock();
}
}
2020-06-07 11:14:05 +00:00
void RabbitMQHandler::stopWithTimeout()
{
stop_scheduled = true;
event_base_loopexit(evbase, &tv);
2020-06-07 11:14:05 +00:00
}
}