mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-17 21:24:28 +00:00
414f470c79
* Refactoring: part 1 * Refactoring: part 2 * Handle request using ReadBuffer interface * Struggles with ReadBuffer's * Fix URI parsing * Implement parsing of multipart/form-data * Check HTTP_LENGTH_REQUIRED before eof() or will hang * Fix HTTPChunkedReadBuffer * Fix build and style * Fix test * Resist double-eof * Fix arcadian build
87 lines
2.7 KiB
C++
87 lines
2.7 KiB
C++
#include <Poco/Net/NetException.h>
|
|
|
|
#include <IO/ReadBufferFromPocoSocket.h>
|
|
#include <Common/Exception.h>
|
|
#include <Common/NetException.h>
|
|
#include <Common/Stopwatch.h>
|
|
|
|
|
|
namespace ProfileEvents
|
|
{
|
|
extern const Event NetworkReceiveElapsedMicroseconds;
|
|
}
|
|
|
|
|
|
namespace DB
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
{
|
|
extern const int NETWORK_ERROR;
|
|
extern const int SOCKET_TIMEOUT;
|
|
extern const int CANNOT_READ_FROM_SOCKET;
|
|
}
|
|
|
|
|
|
bool ReadBufferFromPocoSocket::nextImpl()
|
|
{
|
|
ssize_t bytes_read = 0;
|
|
Stopwatch watch;
|
|
|
|
int flags = 0;
|
|
if (async_callback)
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
/// Add more details to exceptions.
|
|
try
|
|
{
|
|
bytes_read = socket.impl()->receiveBytes(internal_buffer.begin(), internal_buffer.size(), flags);
|
|
|
|
/// If async_callback is specified, and read is blocking, run async_callback and try again later.
|
|
/// It is expected that file descriptor may be polled externally.
|
|
/// Note that receive timeout is not checked here. External code should check it while polling.
|
|
while (bytes_read < 0 && async_callback && errno == EAGAIN)
|
|
{
|
|
async_callback(socket);
|
|
bytes_read = socket.impl()->receiveBytes(internal_buffer.begin(), internal_buffer.size(), flags);
|
|
}
|
|
}
|
|
catch (const Poco::Net::NetException & e)
|
|
{
|
|
throw NetException(e.displayText() + ", while reading from socket (" + peer_address.toString() + ")", ErrorCodes::NETWORK_ERROR);
|
|
}
|
|
catch (const Poco::TimeoutException &)
|
|
{
|
|
throw NetException("Timeout exceeded while reading from socket (" + peer_address.toString() + ")", ErrorCodes::SOCKET_TIMEOUT);
|
|
}
|
|
catch (const Poco::IOException & e)
|
|
{
|
|
throw NetException(e.displayText() + ", while reading from socket (" + peer_address.toString() + ")", ErrorCodes::NETWORK_ERROR);
|
|
}
|
|
|
|
if (bytes_read < 0)
|
|
throw NetException("Cannot read from socket (" + peer_address.toString() + ")", ErrorCodes::CANNOT_READ_FROM_SOCKET);
|
|
|
|
/// NOTE: it is quite inaccurate on high loads since the thread could be replaced by another one
|
|
ProfileEvents::increment(ProfileEvents::NetworkReceiveElapsedMicroseconds, watch.elapsedMicroseconds());
|
|
|
|
if (bytes_read)
|
|
working_buffer.resize(bytes_read);
|
|
else
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
ReadBufferFromPocoSocket::ReadBufferFromPocoSocket(Poco::Net::Socket & socket_, size_t buf_size)
|
|
: BufferWithOwnMemory<ReadBuffer>(buf_size), socket(socket_), peer_address(socket.peerAddress())
|
|
{
|
|
}
|
|
|
|
bool ReadBufferFromPocoSocket::poll(size_t timeout_microseconds) const
|
|
{
|
|
return available() || socket.poll(timeout_microseconds, Poco::Net::Socket::SELECT_READ | Poco::Net::Socket::SELECT_ERROR);
|
|
}
|
|
|
|
}
|