ClickHouse/src/Storages/UVLoop.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
1.9 KiB
C++
Raw Normal View History

2022-05-08 12:12:15 +00:00
#pragma once
#include <memory>
#include <uv.h>
2022-05-12 19:19:11 +00:00
#include <boost/noncopyable.hpp>
2022-05-08 12:12:15 +00:00
#include <Common/Exception.h>
2023-01-04 11:54:11 +00:00
#include <Common/logger_useful.h>
2022-05-08 12:12:15 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int SYSTEM_ERROR;
}
/// RAII wrapper around uv event loop
class UVLoop : public boost::noncopyable
{
public:
2022-05-12 19:19:11 +00:00
UVLoop() : loop_ptr(new uv_loop_t())
2022-05-08 12:12:15 +00:00
{
int res = uv_loop_init(loop_ptr.get());
if (res != 0)
throw Exception(ErrorCodes::SYSTEM_ERROR, "UVLoop could not initialize");
2022-05-08 12:12:15 +00:00
}
~UVLoop()
{
if (loop_ptr)
2023-01-04 11:54:11 +00:00
{
auto res = uv_loop_close(loop_ptr.get());
if (res == UV_EBUSY)
{
LOG_DEBUG(log, "Closing pending handles");
2023-01-04 12:17:40 +00:00
uv_walk(loop_ptr.get(), onUVWalkClosingCallback, nullptr);
2023-01-04 11:54:11 +00:00
/// Run the loop until there are no pending callbacks.
2023-01-04 12:43:33 +00:00
while ((res = uv_run(loop_ptr.get(), UV_RUN_ONCE)) != 0)
2023-01-04 11:54:11 +00:00
{
2023-01-04 12:43:33 +00:00
LOG_DEBUG(log, "Waiting for pending callbacks to finish ({})", res);
2023-01-04 11:54:11 +00:00
}
res = uv_loop_close(loop_ptr.get());
if (res == UV_EBUSY)
{
LOG_ERROR(
log, "Failed to close libuv loop (active requests/handles in the loop: {})",
uv_loop_alive(loop_ptr.get()));
chassert(false);
}
}
}
2022-05-08 12:12:15 +00:00
}
inline uv_loop_t * getLoop() { return loop_ptr.get(); }
inline const uv_loop_t * getLoop() const { return loop_ptr.get(); }
private:
std::unique_ptr<uv_loop_t> loop_ptr;
2023-01-04 11:54:11 +00:00
Poco::Logger * log = &Poco::Logger::get("UVLoop");
2023-01-04 12:17:40 +00:00
static void onUVWalkClosingCallback(uv_handle_t * handle, void *)
{
if (!uv_is_closing(handle))
uv_close(handle, onUVCloseCallback);
}
static void onUVCloseCallback(uv_handle_t *) {}
2022-05-08 12:12:15 +00:00
};
}