Skip to content

Commit

Permalink
nghttpx: Log error code from getsockopt(SO_ERROR) on first write event
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Aug 24, 2016
1 parent bd0c1ed commit cf7f87c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/shrpx_http2_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1806,9 +1806,11 @@ int Http2Session::read_noop(const uint8_t *data, size_t datalen) { return 0; }
int Http2Session::write_noop() { return 0; }

int Http2Session::connected() {
if (!util::check_socket_connected(conn_.fd)) {
auto sock_error = util::get_socket_error(conn_.fd);
if (sock_error != 0) {
SSLOG(WARN, this) << "Backend connect failed; addr="
<< util::to_numeric_addr(&addr_->addr);
<< util::to_numeric_addr(&addr_->addr)
<< ": errno=" << sock_error;

downstream_failure(addr_);

Expand Down
6 changes: 4 additions & 2 deletions src/shrpx_http_downstream_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1143,11 +1143,13 @@ int HttpDownstreamConnection::process_input(const uint8_t *data,
int HttpDownstreamConnection::connected() {
auto &connect_blocker = addr_->connect_blocker;

if (!util::check_socket_connected(conn_.fd)) {
auto sock_error = util::get_socket_error(conn_.fd);
if (sock_error != 0) {
conn_.wlimit.stopw();

DCLOG(WARN, this) << "Backend connect failed; addr="
<< util::to_numeric_addr(&addr_->addr);
<< util::to_numeric_addr(&addr_->addr)
<< ": errno=" << sock_error;

downstream_failure(addr_);

Expand Down
6 changes: 4 additions & 2 deletions src/shrpx_live_check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,12 @@ int LiveCheck::initiate_connection() {
}

int LiveCheck::connected() {
if (!util::check_socket_connected(conn_.fd)) {
auto sock_error = util::get_socket_error(conn_.fd);
if (sock_error != 0) {
if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Backend connect failed; addr="
<< util::to_numeric_addr(&addr_->addr);
<< util::to_numeric_addr(&addr_->addr)
<< ": errno=" << sock_error;
}

return -1;
Expand Down
11 changes: 6 additions & 5 deletions src/shrpx_memcached_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,16 @@ int MemcachedConnection::initiate_connection() {
}

int MemcachedConnection::connected() {
if (!util::check_socket_connected(conn_.fd)) {
auto sock_error = util::get_socket_error(conn_.fd);
if (sock_error != 0) {
MCLOG(WARN, this) << "memcached connect failed; addr="
<< util::to_numeric_addr(addr_)
<< ": errno=" << sock_error;

connect_blocker_.on_failure();

conn_.wlimit.stopw();

if (LOG_ENABLED(INFO)) {
MCLOG(INFO, this) << "memcached connect failed";
}

return -1;
}

Expand Down
10 changes: 10 additions & 0 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,16 @@ bool check_socket_connected(int fd) {
return error == 0;
}

int get_socket_error(int fd) {
int error;
socklen_t len = sizeof(error);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0) {
return -1;
}

return error;
}

bool ipv6_numeric_addr(const char *host) {
uint8_t dst[16];
return inet_pton(AF_INET6, host, dst) == 1;
Expand Down
5 changes: 5 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ int create_nonblock_socket(int family);

bool check_socket_connected(int fd);

// Returns the error code (errno) by inspecting SO_ERROR of given
// |fd|. This function returns the error code if it succeeds, or -1.
// Returning 0 means no error.
int get_socket_error(int fd);

// Returns true if |host| is IPv6 numeric address (e.g., ::1)
bool ipv6_numeric_addr(const char *host);

Expand Down

0 comments on commit cf7f87c

Please sign in to comment.