Commit 2a4ad2c8 authored by Dan Melnic's avatar Dan Melnic Committed by Facebook Github Bot

Return if we handle any error messages to avoid unnecessarily calling recv/send

Summary: Return if we handle any error messages to avoid unnecessarily calling recv/send

Reviewed By: yfeldblum

Differential Revision: D6677314

fbshipit-source-id: 21f724bb2c92b954888ba97bf7820d72decd2775
parent ee1a988d
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2017-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <folly/io/async/AsyncSocket.h> #include <folly/io/async/AsyncSocket.h>
#include <folly/ExceptionWrapper.h> #include <folly/ExceptionWrapper.h>
...@@ -1672,7 +1671,11 @@ void AsyncSocket::ioReady(uint16_t events) noexcept { ...@@ -1672,7 +1671,11 @@ void AsyncSocket::ioReady(uint16_t events) noexcept {
// EventHandler::WRITE is set. Any of these flags can // EventHandler::WRITE is set. Any of these flags can
// indicate that there are messages available in the socket // indicate that there are messages available in the socket
// error message queue. // error message queue.
handleErrMessages(); // Return if we handle any error messages - this is to avoid
// unnecessary read/write calls
if (handleErrMessages()) {
return;
}
// Return now if handleErrMessages() detached us from our EventBase // Return now if handleErrMessages() detached us from our EventBase
if (eventBase_ != originalEventBase) { if (eventBase_ != originalEventBase) {
...@@ -1746,7 +1749,7 @@ void AsyncSocket::prepareReadBuffer(void** buf, size_t* buflen) { ...@@ -1746,7 +1749,7 @@ void AsyncSocket::prepareReadBuffer(void** buf, size_t* buflen) {
readCallback_->getReadBuffer(buf, buflen); readCallback_->getReadBuffer(buf, buflen);
} }
void AsyncSocket::handleErrMessages() noexcept { size_t AsyncSocket::handleErrMessages() noexcept {
// This method has non-empty implementation only for platforms // This method has non-empty implementation only for platforms
// supporting per-socket error queues. // supporting per-socket error queues.
VLOG(5) << "AsyncSocket::handleErrMessages() this=" << this << ", fd=" << fd_ VLOG(5) << "AsyncSocket::handleErrMessages() this=" << this << ", fd=" << fd_
...@@ -1754,7 +1757,7 @@ void AsyncSocket::handleErrMessages() noexcept { ...@@ -1754,7 +1757,7 @@ void AsyncSocket::handleErrMessages() noexcept {
if (errMessageCallback_ == nullptr && idZeroCopyBufPtrMap_.empty()) { if (errMessageCallback_ == nullptr && idZeroCopyBufPtrMap_.empty()) {
VLOG(7) << "AsyncSocket::handleErrMessages(): " VLOG(7) << "AsyncSocket::handleErrMessages(): "
<< "no callback installed - exiting."; << "no callback installed - exiting.";
return; return 0;
} }
#ifdef FOLLY_HAVE_MSG_ERRQUEUE #ifdef FOLLY_HAVE_MSG_ERRQUEUE
...@@ -1774,6 +1777,7 @@ void AsyncSocket::handleErrMessages() noexcept { ...@@ -1774,6 +1777,7 @@ void AsyncSocket::handleErrMessages() noexcept {
msg.msg_flags = 0; msg.msg_flags = 0;
int ret; int ret;
size_t num = 0;
while (true) { while (true) {
ret = recvmsg(fd_, &msg, MSG_ERRQUEUE); ret = recvmsg(fd_, &msg, MSG_ERRQUEUE);
VLOG(5) << "AsyncSocket::handleErrMessages(): recvmsg returned " << ret; VLOG(5) << "AsyncSocket::handleErrMessages(): recvmsg returned " << ret;
...@@ -1789,12 +1793,14 @@ void AsyncSocket::handleErrMessages() noexcept { ...@@ -1789,12 +1793,14 @@ void AsyncSocket::handleErrMessages() noexcept {
errnoCopy); errnoCopy);
failErrMessageRead(__func__, ex); failErrMessageRead(__func__, ex);
} }
return;
return num;
} }
for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
cmsg != nullptr && cmsg->cmsg_len != 0; cmsg != nullptr && cmsg->cmsg_len != 0;
cmsg = CMSG_NXTHDR(&msg, cmsg)) { cmsg = CMSG_NXTHDR(&msg, cmsg)) {
++num;
if (isZeroCopyMsg(*cmsg)) { if (isZeroCopyMsg(*cmsg)) {
processZeroCopyMsg(*cmsg); processZeroCopyMsg(*cmsg);
} else { } else {
...@@ -1804,6 +1810,8 @@ void AsyncSocket::handleErrMessages() noexcept { ...@@ -1804,6 +1810,8 @@ void AsyncSocket::handleErrMessages() noexcept {
} }
} }
} }
#else
return 0;
#endif // FOLLY_HAVE_MSG_ERRQUEUE #endif // FOLLY_HAVE_MSG_ERRQUEUE
} }
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2017-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#pragma once #pragma once
#include <folly/Optional.h> #include <folly/Optional.h>
...@@ -1023,7 +1022,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper { ...@@ -1023,7 +1022,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
virtual void checkForImmediateRead() noexcept; virtual void checkForImmediateRead() noexcept;
virtual void handleInitialReadWrite() noexcept; virtual void handleInitialReadWrite() noexcept;
virtual void prepareReadBuffer(void** buf, size_t* buflen); virtual void prepareReadBuffer(void** buf, size_t* buflen);
virtual void handleErrMessages() noexcept; virtual size_t handleErrMessages() noexcept;
virtual void handleRead() noexcept; virtual void handleRead() noexcept;
virtual void handleWrite() noexcept; virtual void handleWrite() noexcept;
virtual void handleConnect() noexcept; virtual void handleConnect() noexcept;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment