Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions gloo/transport/tcp/attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ struct attr {
int ai_protocol;
struct sockaddr_storage ai_addr;
int ai_addrlen;

// Pre-bound socket file descriptor. When set to a valid fd (>= 0),
// the Listener will adopt this socket instead of creating a new one
// and binding again. This avoids EADDRINUSE races between the test
// bind in address resolution and the actual listener bind.
int ai_fd = -1;
};

} // namespace tcp
Expand Down
5 changes: 4 additions & 1 deletion gloo/transport/tcp/device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ static void lookupAddrForHostname(struct attr& attr) {
continue;
}

int on = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

bind_rv = bind(fd, rp->ai_addr, rp->ai_addrlen);
if (bind_rv == -1) {
bind_errno = errno;
Expand All @@ -117,7 +120,7 @@ static void lookupAddrForHostname(struct attr& attr) {
attr.ai_protocol = rp->ai_protocol;
memcpy(&attr.ai_addr, rp->ai_addr, rp->ai_addrlen);
attr.ai_addrlen = rp->ai_addrlen;
close(fd);
attr.ai_fd = fd;
break;
}

Expand Down
11 changes: 8 additions & 3 deletions gloo/transport/tcp/listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ namespace tcp {

Listener::Listener(std::shared_ptr<Loop> loop, const attr& attr)
: loop_(std::move(loop)) {
listener_ = Socket::createForFamily(attr.ai_addr.ss_family);
listener_->reuseAddr(true);
listener_->bind(attr.ai_addr);
if (attr.ai_fd >= 0) {
listener_ = std::make_shared<Socket>(attr.ai_fd);
listener_->block(false);
} else {
listener_ = Socket::createForFamily(attr.ai_addr.ss_family);
listener_->reuseAddr(true);
listener_->bind(attr.ai_addr);
}
listener_->listen(kBacklog);
addr_ = listener_->sockName();
useRankAsSeqNumber_ = useRankAsSeqNumber();
Expand Down
Loading