| 34 | } |
| 35 | |
| 36 | ssize_t ReadBufferFromPocoSocketBase::socketReceiveBytesImpl(char * ptr, size_t size) |
| 37 | { |
| 38 | ssize_t bytes_read = 0; |
| 39 | Stopwatch watch; |
| 40 | |
| 41 | SCOPE_EXIT({ |
| 42 | /// NOTE: it is quite inaccurate on high loads since the thread could be replaced by another one |
| 43 | ProfileEvents::increment(ProfileEvents::NetworkReceiveElapsedMicroseconds, watch.elapsedMicroseconds()); |
| 44 | ProfileEvents::increment(ProfileEvents::NetworkReceiveBytes, bytes_read); |
| 45 | }); |
| 46 | |
| 47 | CurrentMetrics::Increment metric_increment(CurrentMetrics::NetworkReceive); |
| 48 | |
| 49 | /// Add more details to exceptions. |
| 50 | try |
| 51 | { |
| 52 | /// If async_callback is specified, set socket to non-blocking mode |
| 53 | /// and try to read data from it, if socket is not ready for reading, |
| 54 | /// run async_callback and try again later. |
| 55 | /// It is expected that file descriptor may be polled externally. |
| 56 | /// Note that send timeout is not checked here. External code should check it while polling. |
| 57 | if (async_callback) |
| 58 | { |
| 59 | socket.setBlocking(false); |
| 60 | SCOPE_EXIT_SAFE(socket.setBlocking(true)); |
| 61 | bool secure = socket.secure(); |
| 62 | bytes_read = socket.impl()->receiveBytes(ptr, static_cast<int>(size)); |
| 63 | |
| 64 | /// Check EAGAIN and ERR_SSL_WANT_READ/ERR_SSL_WANT_WRITE for secure socket (reading from secure socket can write too). |
| 65 | while (bytes_read < 0 && (errno == EAGAIN || (secure && (checkSSLWantRead(bytes_read) || checkSSLWantWrite(bytes_read))))) |
| 66 | { |
| 67 | /// In case of ERR_SSL_WANT_WRITE we should wait for socket to be ready for writing, otherwise - for reading. |
| 68 | if (secure && checkSSLWantWrite(bytes_read)) |
| 69 | async_callback(socket.impl()->sockfd(), socket.getSendTimeout(), AsyncEventTimeoutType::SEND, socket_description, AsyncTaskExecutor::Event::WRITE | AsyncTaskExecutor::Event::ERROR); |
| 70 | else |
| 71 | async_callback(socket.impl()->sockfd(), socket.getReceiveTimeout(), AsyncEventTimeoutType::RECEIVE, socket_description, AsyncTaskExecutor::Event::READ | AsyncTaskExecutor::Event::ERROR); |
| 72 | |
| 73 | /// Try to read again. |
| 74 | bytes_read = socket.impl()->receiveBytes(ptr, static_cast<int>(size)); |
| 75 | } |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | bytes_read = socket.impl()->receiveBytes(ptr, static_cast<int>(size)); |
| 80 | } |
| 81 | } |
| 82 | catch (const Poco::Net::NetException & e) |
| 83 | { |
| 84 | throw NetException(ErrorCodes::NETWORK_ERROR, "{}, while reading from socket (peer: {}, local: {})", e.displayText(), peer_address.toString(), socket.address().toString()); |
| 85 | } |
| 86 | catch (const Poco::TimeoutException &) |
| 87 | { |
| 88 | throw NetException(ErrorCodes::SOCKET_TIMEOUT, "Timeout exceeded while reading from socket (peer: {}, local: {}, {} ms)", |
| 89 | peer_address.toString(), socket.address().toString(), |
| 90 | socket.impl()->getReceiveTimeout().totalMilliseconds()); |
| 91 | } |
| 92 | catch (const Poco::IOException & e) |
| 93 | { |
nothing calls this directly
no test coverage detected