1
0
Fork 0

make SimpleHttpClient's SSL connections abortable too (#4023)

This commit is contained in:
Jan 2017-12-13 14:03:33 +01:00 committed by GitHub
parent 9c76613e63
commit 9bcf3c7978
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 246 additions and 422 deletions

View File

@ -30,20 +30,6 @@
#include <sys/types.h>
#ifdef _WIN32
#define STR_ERROR() \
windowsErrorBuf; \
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, \
windowsErrorBuf, sizeof(windowsErrorBuf), NULL); \
errno = GetLastError();
#else
#define STR_ERROR() strerror(errno)
#endif
#ifdef TRI_HAVE_POLL_H
#include <poll.h>
#endif
using namespace arangodb;
using namespace arangodb::basics;
using namespace arangodb::httpclient;
@ -55,17 +41,13 @@ using namespace arangodb::httpclient;
ClientConnection::ClientConnection(Endpoint* endpoint, double requestTimeout,
double connectTimeout, size_t connectRetries)
: GeneralClientConnection(endpoint, requestTimeout, connectTimeout,
connectRetries) {
TRI_invalidatesocket(&_socket);
}
connectRetries) {}
ClientConnection::ClientConnection(std::unique_ptr<Endpoint>& endpoint,
double requestTimeout, double connectTimeout,
size_t connectRetries)
: GeneralClientConnection(endpoint, requestTimeout, connectTimeout,
connectRetries) {
TRI_invalidatesocket(&_socket);
}
connectRetries) {}
////////////////////////////////////////////////////////////////////////////////
/// @brief destroys a client connection
@ -73,35 +55,6 @@ ClientConnection::ClientConnection(std::unique_ptr<Endpoint>& endpoint,
ClientConnection::~ClientConnection() { disconnect(); }
////////////////////////////////////////////////////////////////////////////////
/// @brief check whether the socket is still alive
////////////////////////////////////////////////////////////////////////////////
bool ClientConnection::checkSocket() {
int so_error = -1;
socklen_t len = sizeof so_error;
TRI_ASSERT(TRI_isvalidsocket(_socket));
int res =
TRI_getsockopt(_socket, SOL_SOCKET, SO_ERROR, (void*)&so_error, &len);
if (res != TRI_ERROR_NO_ERROR) {
TRI_set_errno(errno);
disconnect();
return false;
}
if (so_error == 0) {
return true;
}
TRI_set_errno(so_error);
disconnect();
return false;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief connect
////////////////////////////////////////////////////////////////////////////////
@ -114,6 +67,7 @@ bool ClientConnection::connectSocket() {
_isConnected = false;
}
_errorDetails.clear();
_socket = _endpoint->connect(_connectTimeout, _requestTimeout);
if (!TRI_isvalidsocket(_socket)) {
@ -144,174 +98,6 @@ void ClientConnection::disconnectSocket() {
TRI_invalidatesocket(&_socket);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief prepare connection for read/write I/O
////////////////////////////////////////////////////////////////////////////////
bool ClientConnection::prepare(double timeout, bool isWrite) const {
if (!TRI_isvalidsocket(_socket)) {
_errorDetails = std::string("not a valid socket");
return false;
}
// wait for at most 0.5 seconds for poll/select to complete
// if it takes longer, break each poll/select into smaller chunks so we can
// interrupt the whole process if it takes too long in total
static double const POLL_DURATION = 0.5;
auto const fd = TRI_get_fd_or_handle_of_socket(_socket);
double start = TRI_microtime();
int res;
#ifdef TRI_HAVE_POLL_H
// Here we have poll, on all other platforms we use select
bool nowait = (timeout == 0.0);
int towait;
if (timeout * 1000.0 > static_cast<double>(INT_MAX)) {
towait = INT_MAX;
} else {
towait = static_cast<int>(timeout * 1000.0);
}
struct pollfd poller;
memset(&poller, 0, sizeof(struct pollfd)); // for our old friend Valgrind
poller.fd = fd;
poller.events = (isWrite ? POLLOUT : POLLIN);
while (true) { // will be left by break
res = poll(&poller, 1, towait > static_cast<int>(POLL_DURATION * 1000.0)
? static_cast<int>(POLL_DURATION * 1000.0)
: towait);
if (res == -1 && errno == EINTR) {
if (!nowait) {
double end = TRI_microtime();
towait -= static_cast<int>((end - start) * 1000.0);
start = end;
if (towait <= 0) { // Should not happen, but there might be rounding
// errors, so just to prevent a poll call with
// negative timeout...
res = 0;
break;
}
}
continue;
}
if (res == 0) {
if (isInterrupted()) {
_errorDetails = std::string("command locally aborted");
TRI_set_errno(TRI_ERROR_REQUEST_CANCELED);
return false;
}
double end = TRI_microtime();
towait -= static_cast<int>((end - start) * 1000.0);
if (towait <= 0) {
break;
}
start = end;
continue;
}
break;
}
// Now res can be:
// 1 : if the file descriptor was ready
// 0 : if the timeout happened
// -1: if an error happened, EINTR within the timeout is already caught
#else
// All versions use select:
// An fd_set is a fixed size buffer.
// Executing FD_CLR() or FD_SET() with a value of fd that is negative or is
// equal to or larger than FD_SETSIZE
// will result in undefined behavior. Moreover, POSIX requires fd to be a
// valid file descriptor.
if (fd < 0 || fd >= FD_SETSIZE) {
// invalid or too high file descriptor value...
// if we call FD_ZERO() or FD_SET() with it, the program behavior will be
// undefined
_errorDetails = std::string("file descriptor value too high");
return false;
}
// handle interrupt
do {
retry:
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
fd_set* readFds = nullptr;
fd_set* writeFds = nullptr;
if (isWrite) {
writeFds = &fdset;
} else {
readFds = &fdset;
}
int sockn = (int)(fd + 1);
double waitTimeout = timeout;
if (waitTimeout > POLL_DURATION) {
waitTimeout = POLL_DURATION;
}
struct timeval t;
t.tv_sec = (long)waitTimeout;
t.tv_usec = (long)((waitTimeout - (double)t.tv_sec) * 1000000.0);
res = select(sockn, readFds, writeFds, nullptr, &t);
if ((res == -1 && errno == EINTR)) {
int myerrno = errno;
double end = TRI_microtime();
errno = myerrno;
timeout = timeout - (end - start);
start = end;
} else if (res == 0) {
if (isInterrupted()) {
_errorDetails = std::string("command locally aborted");
TRI_set_errno(TRI_ERROR_REQUEST_CANCELED);
return false;
}
double end = TRI_microtime();
timeout = timeout - (end - start);
if (timeout <= 0.0) {
break;
}
start = end;
goto retry;
}
} while (res == -1 && errno == EINTR && timeout > 0.0);
#endif
if (res > 0) {
return true;
}
if (res == 0) {
if (isWrite) {
_errorDetails = std::string("timeout during write");
TRI_set_errno(TRI_SIMPLE_CLIENT_COULD_NOT_WRITE);
} else {
_errorDetails = std::string("timeout during read");
TRI_set_errno(TRI_SIMPLE_CLIENT_COULD_NOT_READ);
}
} else { // res < 0
#ifdef _WIN32
char windowsErrorBuf[256];
#endif
char const* pErr = STR_ERROR();
_errorDetails = std::string("during prepare: ") + std::to_string(errno) +
std::string(" - ") + pErr;
TRI_set_errno(errno);
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief write data to the connection
////////////////////////////////////////////////////////////////////////////////
@ -400,7 +186,7 @@ bool ClientConnection::readClientConnection(StringBuffer& stringBuffer,
////////////////////////////////////////////////////////////////////////////////
bool ClientConnection::readable() {
if (prepare(0.0, false)) {
if (prepare(_socket, 0.0, false)) {
return checkSocket();
}

View File

@ -55,12 +55,6 @@ class ClientConnection final : public GeneralClientConnection {
~ClientConnection();
//////////////////////////////////////////////////////////////////////////////
/// @brief check whether the socket is still alive
//////////////////////////////////////////////////////////////////////////////
bool checkSocket();
protected:
//////////////////////////////////////////////////////////////////////////////
/// @brief connect
@ -73,13 +67,7 @@ class ClientConnection final : public GeneralClientConnection {
//////////////////////////////////////////////////////////////////////////////
void disconnectSocket() override;
//////////////////////////////////////////////////////////////////////////////
/// @brief prepare connection for read/write I/O
//////////////////////////////////////////////////////////////////////////////
bool prepare(double, bool) const override;
//////////////////////////////////////////////////////////////////////////////
/// @brief write data to the connection
//////////////////////////////////////////////////////////////////////////////
@ -98,13 +86,6 @@ class ClientConnection final : public GeneralClientConnection {
//////////////////////////////////////////////////////////////////////////////
bool readable() override;
private:
//////////////////////////////////////////////////////////////////////////////
/// @brief the underlying socket
//////////////////////////////////////////////////////////////////////////////
TRI_socket_t _socket;
};
}
}

View File

@ -24,6 +24,28 @@
#include "GeneralClientConnection.h"
#include "SimpleHttpClient/ClientConnection.h"
#include "SimpleHttpClient/SslClientConnection.h"
#include "Basics/socket-utils.h"
#ifdef TRI_HAVE_POLL_H
#include <poll.h>
#endif
#ifdef TRI_HAVE_WINSOCK2_H
#include <WinSock2.h>
#include <WS2tcpip.h>
#endif
#include <sys/types.h>
#ifdef _WIN32
#define STR_ERROR() \
windowsErrorBuf; \
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, \
windowsErrorBuf, sizeof(windowsErrorBuf), NULL); \
errno = GetLastError();
#else
#define STR_ERROR() strerror(errno)
#endif
using namespace arangodb;
using namespace arangodb::basics;
@ -45,6 +67,7 @@ GeneralClientConnection::GeneralClientConnection(Endpoint* endpoint,
_numConnectRetries(0),
_isConnected(false),
_isInterrupted(false) {
TRI_invalidatesocket(&_socket);
}
GeneralClientConnection::GeneralClientConnection(
@ -57,7 +80,9 @@ GeneralClientConnection::GeneralClientConnection(
_connectRetries(connectRetries),
_numConnectRetries(0),
_isConnected(false),
_isInterrupted(false) {}
_isInterrupted(false) {
TRI_invalidatesocket(&_socket);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief destroys a client connection
@ -138,6 +163,203 @@ void GeneralClientConnection::disconnect() {
_numConnectRetries = 0;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief prepare connection for read/write I/O
////////////////////////////////////////////////////////////////////////////////
bool GeneralClientConnection::prepare(TRI_socket_t socket, double timeout, bool isWrite) const {
// wait for at most 0.5 seconds for poll/select to complete
// if it takes longer, break each poll/select into smaller chunks so we can
// interrupt the whole process if it takes too long in total
static constexpr double POLL_DURATION = 0.5;
auto const fd = TRI_get_fd_or_handle_of_socket(socket);
double start = TRI_microtime();
int res;
#ifdef TRI_HAVE_POLL_H
// Here we have poll, on all other platforms we use select
bool nowait = (timeout == 0.0);
int towait;
if (timeout * 1000.0 > static_cast<double>(INT_MAX)) {
towait = INT_MAX;
} else {
towait = static_cast<int>(timeout * 1000.0);
}
struct pollfd poller;
memset(&poller, 0, sizeof(struct pollfd)); // for our old friend Valgrind
poller.fd = fd;
poller.events = (isWrite ? POLLOUT : POLLIN);
while (true) { // will be left by break
res = poll(&poller, 1, towait > static_cast<int>(POLL_DURATION * 1000.0)
? static_cast<int>(POLL_DURATION * 1000.0)
: towait);
if (res == -1 && errno == EINTR) {
if (!nowait) {
double end = TRI_microtime();
towait -= static_cast<int>((end - start) * 1000.0);
start = end;
if (towait <= 0) { // Should not happen, but there might be rounding
// errors, so just to prevent a poll call with
// negative timeout...
res = 0;
break;
}
}
continue;
}
if (res == 0) {
if (isInterrupted()) {
_errorDetails = std::string("command locally aborted");
TRI_set_errno(TRI_ERROR_REQUEST_CANCELED);
return false;
}
double end = TRI_microtime();
towait -= static_cast<int>((end - start) * 1000.0);
if (towait <= 0) {
break;
}
start = end;
continue;
}
break;
}
// Now res can be:
// 1 : if the file descriptor was ready
// 0 : if the timeout happened
// -1: if an error happened, EINTR within the timeout is already caught
#else
// All versions use select:
// An fd_set is a fixed size buffer.
// Executing FD_CLR() or FD_SET() with a value of fd that is negative or is
// equal to or larger than FD_SETSIZE
// will result in undefined behavior. Moreover, POSIX requires fd to be a
// valid file descriptor.
if (fd < 0 || fd >= FD_SETSIZE) {
// invalid or too high file descriptor value...
// if we call FD_ZERO() or FD_SET() with it, the program behavior will be
// undefined
_errorDetails = std::string("file descriptor value too high");
return false;
}
// handle interrupt
do {
retry:
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
fd_set* readFds = nullptr;
fd_set* writeFds = nullptr;
if (isWrite) {
writeFds = &fdset;
} else {
readFds = &fdset;
}
int sockn = (int)(fd + 1);
double waitTimeout = timeout;
if (waitTimeout > POLL_DURATION) {
waitTimeout = POLL_DURATION;
}
struct timeval t;
t.tv_sec = (long)waitTimeout;
t.tv_usec = (long)((waitTimeout - (double)t.tv_sec) * 1000000.0);
res = select(sockn, readFds, writeFds, nullptr, &t);
if ((res == -1 && errno == EINTR)) {
int myerrno = errno;
double end = TRI_microtime();
errno = myerrno;
timeout = timeout - (end - start);
start = end;
} else if (res == 0) {
if (isInterrupted()) {
_errorDetails = std::string("command locally aborted");
TRI_set_errno(TRI_ERROR_REQUEST_CANCELED);
return false;
}
double end = TRI_microtime();
timeout = timeout - (end - start);
if (timeout <= 0.0) {
break;
}
start = end;
goto retry;
}
} while (res == -1 && errno == EINTR && timeout > 0.0);
#endif
if (res > 0) {
if (isInterrupted()) {
_errorDetails = std::string("command locally aborted");
TRI_set_errno(TRI_ERROR_REQUEST_CANCELED);
return false;
}
return true;
}
if (res == 0) {
if (isWrite) {
_errorDetails = std::string("timeout during write");
TRI_set_errno(TRI_SIMPLE_CLIENT_COULD_NOT_WRITE);
} else {
_errorDetails = std::string("timeout during read");
TRI_set_errno(TRI_SIMPLE_CLIENT_COULD_NOT_READ);
}
} else { // res < 0
#ifdef _WIN32
char windowsErrorBuf[256];
#endif
char const* pErr = STR_ERROR();
_errorDetails = std::string("during prepare: ") + std::to_string(errno) +
std::string(" - ") + pErr;
TRI_set_errno(errno);
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief check whether the socket is still alive
////////////////////////////////////////////////////////////////////////////////
bool GeneralClientConnection::checkSocket() {
int so_error = -1;
socklen_t len = sizeof so_error;
TRI_ASSERT(TRI_isvalidsocket(_socket));
int res =
TRI_getsockopt(_socket, SOL_SOCKET, SO_ERROR, (void*)&so_error, &len);
if (res != TRI_ERROR_NO_ERROR) {
TRI_set_errno(errno);
disconnect();
return false;
}
if (so_error == 0) {
return true;
}
TRI_set_errno(so_error);
disconnect();
return false;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief handleWrite
/// Write data to endpoint, this uses select to block until some
@ -157,7 +379,7 @@ bool GeneralClientConnection::handleWrite(double timeout, void const* buffer,
size_t length, size_t* bytesWritten) {
*bytesWritten = 0;
if (prepare(timeout, true)) {
if (prepare(_socket, timeout, true)) {
return this->writeClientConnection(buffer, length, bytesWritten);
}
@ -183,7 +405,7 @@ bool GeneralClientConnection::handleRead(double timeout, StringBuffer& buffer,
bool& connectionClosed) {
connectionClosed = false;
if (prepare(timeout, false)) {
if (prepare(_socket, timeout, false)) {
return this->readClientConnection(buffer, connectionClosed);
}

View File

@ -165,12 +165,18 @@ class GeneralClientConnection {
//////////////////////////////////////////////////////////////////////////////
virtual void disconnectSocket() = 0;
//////////////////////////////////////////////////////////////////////////////
/// @brief prepare connection for read/write I/O
//////////////////////////////////////////////////////////////////////////////
virtual bool prepare(double, bool) const = 0;
bool prepare(TRI_socket_t socket, double timeout, bool isWrite) const;
//////////////////////////////////////////////////////////////////////////////
/// @brief check whether the socket is still alive
//////////////////////////////////////////////////////////////////////////////
bool checkSocket();
//////////////////////////////////////////////////////////////////////////////
/// @brief write data to the connection
@ -192,6 +198,12 @@ class GeneralClientConnection {
virtual bool readable() = 0;
protected:
//////////////////////////////////////////////////////////////////////////////
/// @brief the underlying socket
//////////////////////////////////////////////////////////////////////////////
TRI_socket_t _socket;
//////////////////////////////////////////////////////////////////////////////
/// @brief details to errors
//////////////////////////////////////////////////////////////////////////////

View File

@ -50,10 +50,6 @@
#define STR_ERROR() strerror(errno)
#endif
#ifdef TRI_HAVE_POLL_H
#include <sys/poll.h>
#endif
using namespace arangodb;
using namespace arangodb::basics;
using namespace arangodb::httpclient;
@ -174,7 +170,6 @@ SslClientConnection::SslClientConnection(Endpoint* endpoint,
_ctx(nullptr),
_sslProtocol(sslProtocol) {
TRI_invalidatesocket(&_socket);
init(sslProtocol);
}
@ -189,7 +184,6 @@ SslClientConnection::SslClientConnection(std::unique_ptr<Endpoint>& endpoint,
_ctx(nullptr),
_sslProtocol(sslProtocol) {
TRI_invalidatesocket(&_socket);
init(sslProtocol);
}
@ -412,129 +406,6 @@ void SslClientConnection::disconnectSocket() {
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief prepare connection for read/write I/O
////////////////////////////////////////////////////////////////////////////////
bool SslClientConnection::prepare(double timeout, bool isWrite) const {
auto const fd = TRI_get_fd_or_handle_of_socket(_socket);
double start = TRI_microtime();
int res;
#ifdef TRI_HAVE_POLL_H
// Here we have poll, on all other platforms we use select
bool nowait = (timeout == 0.0);
int towait;
if (timeout * 1000.0 > static_cast<double>(INT_MAX)) {
towait = INT_MAX;
} else {
towait = static_cast<int>(timeout * 1000.0);
}
struct pollfd poller;
memset(&poller, 0, sizeof(struct pollfd)); // for our old friend Valgrind
poller.fd = fd;
poller.events = isWrite ? POLLOUT : POLLIN;
while (true) { // will be left by break
res = poll(&poller, 1, towait);
if (res == -1 && errno == EINTR) {
if (!nowait) {
double end = TRI_microtime();
towait -= static_cast<int>((end - start) * 1000.0);
start = end;
if (towait < 0) { // Should not happen, but there might be rounding
// errors, so just to prevent a poll call with
// negative timeout...
res = 0;
break;
}
}
continue;
}
break;
}
// Now res can be:
// 1 : if the file descriptor was ready
// 0 : if the timeout happened
// -1: if an error happened, EINTR within the timeout is already caught
#else
// All versions other use select:
// An fd_set is a fixed size buffer.
// Executing FD_CLR() or FD_SET() with a value of fd that is negative or is
// equal to or larger than FD_SETSIZE
// will result in undefined behavior. Moreover, POSIX requires fd to be a
// valid file descriptor.
if (fd < 0 || fd >= FD_SETSIZE) {
// invalid or too high file descriptor value...
// if we call FD_ZERO() or FD_SET() with it, the program behavior will be
// undefined
_errorDetails = std::string("file descriptor value too high");
return false;
}
fd_set fdset;
// handle interrupt
do {
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
fd_set* readFds = nullptr;
fd_set* writeFds = nullptr;
if (isWrite) {
writeFds = &fdset;
} else {
readFds = &fdset;
}
int sockn = (int)(fd + 1);
struct timeval t;
t.tv_sec = (long)timeout;
t.tv_usec = (long)((timeout - (double)t.tv_sec) * 1000000.0);
res = select(sockn, readFds, writeFds, nullptr, &t);
if ((res == -1 && errno == EINTR)) {
int myerrno = errno;
double end = TRI_microtime();
errno = myerrno;
timeout = timeout - (end - start);
start = end;
}
} while ((res == -1) && (errno == EINTR) && (timeout > 0.0));
#endif
if (res > 0) {
return true;
}
if (res == 0) {
if (isWrite) {
_errorDetails = std::string("timeout during write");
TRI_set_errno(TRI_SIMPLE_CLIENT_COULD_NOT_WRITE);
} else {
_errorDetails = std::string("timeout during read");
TRI_set_errno(TRI_SIMPLE_CLIENT_COULD_NOT_READ);
}
} else { // res < 0
#ifdef _WIN32
char windowsErrorBuf[256];
#endif
char const* pErr = STR_ERROR();
_errorDetails = std::string("during prepare: ") + std::to_string(errno) +
std::string(" - ") + pErr;
TRI_set_errno(errno);
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief write data to the connection
////////////////////////////////////////////////////////////////////////////////
@ -686,38 +557,9 @@ bool SslClientConnection::readable() {
return true;
}
if (prepare(0.0, false)) {
if (prepare(_socket, 0.0, false)) {
return checkSocket();
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return whether the socket is workable
////////////////////////////////////////////////////////////////////////////////
bool SslClientConnection::checkSocket() {
int so_error = -1;
socklen_t len = sizeof so_error;
TRI_ASSERT(TRI_isvalidsocket(_socket));
int res =
TRI_getsockopt(_socket, SOL_SOCKET, SO_ERROR, (char*)(&so_error), &len);
if (res != TRI_ERROR_NO_ERROR) {
_isConnected = false;
TRI_set_errno(errno);
return false;
}
if (so_error == 0) {
return true;
}
TRI_set_errno(so_error);
_isConnected = false;
return false;
}

View File

@ -80,13 +80,7 @@ class SslClientConnection final : public GeneralClientConnection {
//////////////////////////////////////////////////////////////////////////////
void disconnectSocket() override;
//////////////////////////////////////////////////////////////////////////////
/// @brief prepare connection for read/write I/O
//////////////////////////////////////////////////////////////////////////////
bool prepare(double, bool) const override;
//////////////////////////////////////////////////////////////////////////////
/// @brief write data to the connection
//////////////////////////////////////////////////////////////////////////////
@ -107,19 +101,6 @@ class SslClientConnection final : public GeneralClientConnection {
bool readable() override;
private:
//////////////////////////////////////////////////////////////////////////////
/// @brief return whether the socket is still workable
//////////////////////////////////////////////////////////////////////////////
bool checkSocket();
private:
//////////////////////////////////////////////////////////////////////////////
/// @brief the underlying socket
//////////////////////////////////////////////////////////////////////////////
TRI_socket_t _socket;
//////////////////////////////////////////////////////////////////////////////
/// @brief the underlying session
//////////////////////////////////////////////////////////////////////////////