Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use getsockopt to check connection state after poll indicate writable #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ int Connection::connect() {
if (server_addrinfo) {
freeaddrinfo(server_addrinfo);
}
log_warn("[I: %p] %s getaddrinfo err", this, m_name);
return -1;
}

Expand Down Expand Up @@ -134,23 +135,44 @@ int Connection::connectPoll(int fd, struct addrinfo* ai_ptr) {
pollfd_t pollfds[n_fds];
pollfds[0].fd = fd;
pollfds[0].events = POLLOUT;
// clean errno
errno = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tclh123 https://github.com/douban/libmc/blob/master/include/Common.h#L61 log will use errno,if not clean here。such as EINPROGRESS EALREADY above will show in the log,it's noisy。

int max_timeout = 6;
while (--max_timeout) {
int rv = poll(pollfds, n_fds, m_connectTimeout);
rv = poll(pollfds, n_fds, m_connectTimeout);
if (rv == 1) {
if (pollfds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
log_warn("[I: %p] %s conn poll err", this, m_name);
return -1;
} else {
int err = 0;
socklen_t errlen = sizeof(err);

if (::getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
log_warn("[I: %p] %s getsockopt err", this, m_name);
return -1;
}

if (err) {
errno = err;
log_warn("[I: %p] %s getsockopt indicate connect err", this, m_name);
return -1;
}
return 0;
}
} else if (rv == -1) {
log_warn("[I: %p] %s poll err", this, m_name);
return -1;
}
}
log_warn("[I: %p] %s connect timeout after poll retry", this, m_name);
return -1;
}
default:
return -1;
{
log_warn("[I: %p] %s connect err", this, m_name);
return -1;
}
}
}

Expand Down