From 2a4ad2c8ddc1eb1be8b7ffb607de954ccc2b666e Mon Sep 17 00:00:00 2001 From: Dan Melnic Date: Tue, 9 Jan 2018 11:36:55 -0800 Subject: [PATCH] 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 --- folly/io/async/AsyncSocket.cpp | 20 ++++++++++++++------ folly/io/async/AsyncSocket.h | 5 ++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/folly/io/async/AsyncSocket.cpp b/folly/io/async/AsyncSocket.cpp index 883ee1d7..3065c1cd 100644 --- a/folly/io/async/AsyncSocket.cpp +++ b/folly/io/async/AsyncSocket.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2017-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include #include @@ -1672,7 +1671,11 @@ void AsyncSocket::ioReady(uint16_t events) noexcept { // EventHandler::WRITE is set. Any of these flags can // indicate that there are messages available in the socket // 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 if (eventBase_ != originalEventBase) { @@ -1746,7 +1749,7 @@ void AsyncSocket::prepareReadBuffer(void** buf, size_t* buflen) { readCallback_->getReadBuffer(buf, buflen); } -void AsyncSocket::handleErrMessages() noexcept { +size_t AsyncSocket::handleErrMessages() noexcept { // This method has non-empty implementation only for platforms // supporting per-socket error queues. VLOG(5) << "AsyncSocket::handleErrMessages() this=" << this << ", fd=" << fd_ @@ -1754,7 +1757,7 @@ void AsyncSocket::handleErrMessages() noexcept { if (errMessageCallback_ == nullptr && idZeroCopyBufPtrMap_.empty()) { VLOG(7) << "AsyncSocket::handleErrMessages(): " << "no callback installed - exiting."; - return; + return 0; } #ifdef FOLLY_HAVE_MSG_ERRQUEUE @@ -1774,6 +1777,7 @@ void AsyncSocket::handleErrMessages() noexcept { msg.msg_flags = 0; int ret; + size_t num = 0; while (true) { ret = recvmsg(fd_, &msg, MSG_ERRQUEUE); VLOG(5) << "AsyncSocket::handleErrMessages(): recvmsg returned " << ret; @@ -1789,12 +1793,14 @@ void AsyncSocket::handleErrMessages() noexcept { errnoCopy); failErrMessageRead(__func__, ex); } - return; + + return num; } for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr && cmsg->cmsg_len != 0; cmsg = CMSG_NXTHDR(&msg, cmsg)) { + ++num; if (isZeroCopyMsg(*cmsg)) { processZeroCopyMsg(*cmsg); } else { @@ -1804,6 +1810,8 @@ void AsyncSocket::handleErrMessages() noexcept { } } } +#else + return 0; #endif // FOLLY_HAVE_MSG_ERRQUEUE } diff --git a/folly/io/async/AsyncSocket.h b/folly/io/async/AsyncSocket.h index 44ae5968..c85ac6b8 100644 --- a/folly/io/async/AsyncSocket.h +++ b/folly/io/async/AsyncSocket.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2017-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #pragma once #include @@ -1023,7 +1022,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper { virtual void checkForImmediateRead() noexcept; virtual void handleInitialReadWrite() noexcept; virtual void prepareReadBuffer(void** buf, size_t* buflen); - virtual void handleErrMessages() noexcept; + virtual size_t handleErrMessages() noexcept; virtual void handleRead() noexcept; virtual void handleWrite() noexcept; virtual void handleConnect() noexcept; -- 2.34.1