Add getTotalConnectTimeout method
authorSubodh Iyengar <subodh@fb.com>
Sun, 5 Mar 2017 01:46:28 +0000 (17:46 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sun, 5 Mar 2017 01:51:01 +0000 (17:51 -0800)
Summary:
AsyncSSLSocket can be connected using connect with
total timeouts.

This adds a method to get the total timeout that
was set.

Reviewed By: ngoyal, yfeldblum

Differential Revision: D4656331

fbshipit-source-id: a55ad9f081449d358b8133e9598a2063f625a2e6

folly/io/async/AsyncSSLSocket.cpp
folly/io/async/AsyncSSLSocket.h
folly/io/async/test/AsyncSSLSocketTest.cpp
folly/io/async/test/BlockingSocket.h

index 7c72a58462be4671a0481b97989debd83561ce19..7b4e16bbcdf5c022794dd19fb78547b5400c246e 100644 (file)
@@ -666,6 +666,7 @@ void AsyncSSLSocket::connect(
   assert(state_ == StateEnum::UNINIT);
   assert(sslState_ == STATE_UNINIT);
   noTransparentTls_ = true;
+  totalConnectTimeout_ = totalConnectTimeout;
   AsyncSSLSocketConnector* connector =
       new AsyncSSLSocketConnector(this, callback, totalConnectTimeout.count());
   AsyncSocket::connect(
index 4edbec58548f0bfcf0c0a7c867ec08e409911f63..99728dddcee12302547fcf4834bf3a5f431b2b97 100644 (file)
@@ -693,6 +693,16 @@ class AsyncSSLSocket : public virtual AsyncSocket {
     return sessionResumptionAttempted_;
   }
 
+  /**
+   * If the SSL socket was used to connect as well
+   * as establish an SSL connection, this gives the total
+   * timeout for the connect + SSL connection that was
+   * set.
+   */
+  std::chrono::milliseconds getTotalConnectTimeout() const {
+    return totalConnectTimeout_;
+  }
+
  private:
 
   void init();
@@ -845,6 +855,7 @@ class AsyncSSLSocket : public virtual AsyncSocket {
   std::chrono::steady_clock::time_point handshakeEndTime_;
   std::chrono::milliseconds handshakeConnectTimeout_{0};
   bool sessionResumptionAttempted_{false};
+  std::chrono::milliseconds totalConnectTimeout_{0};
 
   std::unique_ptr<IOBuf> preReceivedData_;
 };
index b5c6430ac268d3f18f7f56cb2154efde08b45641..41284b6ee26d13484a67556fb24c4b4607eaf08c 100644 (file)
@@ -152,7 +152,7 @@ TEST(AsyncSSLSocketTest, ConnectWriteReadClose) {
   // connect
   auto socket = std::make_shared<BlockingSocket>(server.getAddress(),
                                                  sslContext);
-  socket->open();
+  socket->open(std::chrono::milliseconds(10000));
 
   // write()
   uint8_t buf[128];
@@ -169,6 +169,7 @@ TEST(AsyncSSLSocketTest, ConnectWriteReadClose) {
   socket->close();
 
   cerr << "ConnectWriteReadClose test completed" << endl;
+  EXPECT_EQ(socket->getSSLSocket()->getTotalConnectTimeout().count(), 10000);
 }
 
 /**
index 7f72ab24c669b490d07a581139d8d074dbe6b93c..0aeb4d0e49065816243e51c904d54a44eb3ff17b 100644 (file)
@@ -56,6 +56,7 @@ class BlockingSocket : public folly::AsyncSocket::ConnectCallback,
       throw err_.value();
     }
   }
+
   void close() {
     sock_->close();
   }
@@ -86,6 +87,14 @@ class BlockingSocket : public folly::AsyncSocket::ConnectCallback,
     return sock_->getFd();
   }
 
+  folly::AsyncSocket* getSocket() {
+    return sock_.get();
+  }
+
+  folly::AsyncSSLSocket* getSSLSocket() {
+    return dynamic_cast<folly::AsyncSSLSocket*>(sock_.get());
+  }
+
  private:
   folly::EventBase eventBase_;
   folly::AsyncSocket::UniquePtr sock_;