ClickHouse/src/Client/PacketReceiver.h

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

88 lines
2.7 KiB
C++
Raw Normal View History

2021-02-17 17:34:52 +00:00
#pragma once
#if defined(OS_LINUX)
2021-02-21 14:03:24 +00:00
#include <variant>
2021-02-17 17:34:52 +00:00
#include <Client/IConnections.h>
#include <Common/FiberStack.h>
#include <Common/Fiber.h>
2021-02-21 14:03:24 +00:00
#include <Common/Epoll.h>
#include <Common/TimerDescriptor.h>
2023-03-03 19:30:43 +00:00
#include <Common/AsyncTaskExecutor.h>
2021-02-17 17:34:52 +00:00
namespace DB
{
/// Class for nonblocking packet receiving. It runs connection->receivePacket
/// in fiber and sets special read callback which is called when
/// reading from socket blocks. When read callback is called,
/// socket and receive timeout are added in epoll and execution returns to the main program.
/// So, you can poll this epoll file descriptor to determine when to resume
2021-02-21 14:03:24 +00:00
/// packet receiving.
2023-03-03 19:30:43 +00:00
class PacketReceiver : public AsyncTaskExecutor
2021-02-17 17:34:52 +00:00
{
public:
2023-03-03 19:30:43 +00:00
explicit PacketReceiver(Connection * connection_);
2021-02-21 14:03:24 +00:00
2023-03-17 13:02:20 +00:00
bool isPacketReady() const { return !is_read_in_process && !is_timeout_expired && !exception; }
2023-03-03 19:30:43 +00:00
Packet getPacket() { return std::move(packet); }
2021-02-17 17:34:52 +00:00
2023-03-03 19:30:43 +00:00
bool hasException() const { return exception.operator bool(); }
std::exception_ptr getException() const { return exception; }
bool isTimeoutExpired() const { return is_timeout_expired; }
Poco::Timespan getTimeout() const { return timeout; }
2021-02-17 17:34:52 +00:00
2023-03-03 19:30:43 +00:00
void setTimeout(const Poco::Timespan & timeout_)
2021-02-17 17:34:52 +00:00
{
2023-03-03 19:30:43 +00:00
timeout_descriptor.setRelative(timeout_);
timeout = timeout_;
2021-02-17 17:34:52 +00:00
}
int getFileDescriptor() const { return epoll.getFileDescriptor(); }
private:
2023-03-03 19:30:43 +00:00
bool checkBeforeTaskResume() override;
void afterTaskResume() override {}
2021-02-17 17:34:52 +00:00
2023-03-03 19:30:43 +00:00
void processAsyncEvent(int fd, Poco::Timespan socket_timeout, AsyncEventTimeoutType, const std::string &, uint32_t) override;
void clearAsyncEvent() override;
2023-03-17 13:02:20 +00:00
void processException(std::exception_ptr e) override { exception = e; }
2023-03-03 19:30:43 +00:00
struct Task : public AsyncTask
2021-02-17 17:34:52 +00:00
{
2023-03-03 19:30:43 +00:00
Task(PacketReceiver & receiver_) : receiver(receiver_) {}
2021-02-17 17:34:52 +00:00
PacketReceiver & receiver;
2023-05-22 18:22:05 +00:00
void run(AsyncCallback async_callback, SuspendCallback suspend_callback) override;
2021-02-17 17:34:52 +00:00
};
2023-03-03 19:30:43 +00:00
/// When epoll file descriptor is ready, check if it's an expired timeout.
/// Return false if receive timeout expired and socket is not ready, return true otherwise.
bool checkTimeout();
2021-02-17 17:34:52 +00:00
Connection * connection;
2023-03-03 19:30:43 +00:00
int socket_fd = -1;
2021-02-21 14:03:24 +00:00
Packet packet;
2023-03-03 19:30:43 +00:00
/// We use timer descriptor for checking socket timeouts.
TimerDescriptor timeout_descriptor;
Poco::Timespan timeout;
bool is_timeout_expired = false;
2021-02-21 14:03:24 +00:00
/// In read callback we add socket file descriptor and timer descriptor with receive timeout
/// in epoll, so we can return epoll file descriptor outside for polling.
Epoll epoll;
/// If and exception occurred in fiber resume, we save it and rethrow.
2021-02-17 17:34:52 +00:00
std::exception_ptr exception;
2021-02-21 14:03:24 +00:00
bool is_read_in_process = false;
2021-02-17 17:34:52 +00:00
};
}
#endif