コード例 #1
0
ファイル: AsyncMcClientImpl.cpp プロジェクト: jq/mcrouter
void AsyncMcClientImpl::attemptConnection() {
  assert(connectionState_ == ConnectionState::DOWN);

  connectionState_ = ConnectionState::CONNECTING;

  if (connectionOptions_.noNetwork) {
    socket_.reset(new MockMcClientTransport(eventBase_));
    connectSuccess();
    return;
  }

  if (connectionOptions_.sslContextProvider) {
    auto sslContext = connectionOptions_.sslContextProvider();
    if (!sslContext) {
      LOG_FAILURE("AsyncMcClient", failure::Category::kBadEnvironment,
        "SSLContext provider returned nullptr, check SSL certificates. Any "
        "further request to {} will fail.",
        connectionOptions_.accessPoint.toHostPortString());
      connectErr(folly::AsyncSocketException(
                   folly::AsyncSocketException::SSL_ERROR, ""));
      return;
    }
    socket_.reset(new folly::AsyncSSLSocket(
      sslContext, &eventBase_));
  } else {
    socket_.reset(new folly::AsyncSocket(&eventBase_));
  }

  auto& socket = dynamic_cast<folly::AsyncSocket&>(*socket_);

  folly::SocketAddress address;
  try {
    address = folly::SocketAddress(
      connectionOptions_.accessPoint.getHost(),
      connectionOptions_.accessPoint.getPort(),
      /* allowNameLookup */ true);
  } catch (const std::system_error& e) {
    LOG_FAILURE("AsyncMcClient", failure::Category::kBadEnvironment,
                "{}", e.what());
    connectErr(folly::AsyncSocketException(
                   folly::AsyncSocketException::NOT_OPEN, ""));
    return;
  }

  auto socketOptions = createSocketOptions(address, connectionOptions_);

  socket.setSendTimeout(connectionOptions_.writeTimeout.count());
  socket.connect(this, address, connectionOptions_.writeTimeout.count(),
                 socketOptions);

  // If AsyncSocket::connect() fails, socket_ may have been reset
  if (socket_ && connectionOptions_.enableQoS) {
    checkWhetherQoSIsApplied(address, socket.getFd(), connectionOptions_);
  }
}
コード例 #2
0
ファイル: ThriftTransport.cpp プロジェクト: huayl/mcrouter
apache::thrift::async::TAsyncTransport::UniquePtr
ThriftTransportBase::getConnectingSocket() {
  return folly::fibers::runInMainContext([this] {
    // TODO(@aap): Replace with createSocket() once Thrift works with
    // AsyncTransportWrapper.
    apache::thrift::async::TAsyncTransport::UniquePtr socket(
        new apache::thrift::async::TAsyncSocket(&eventBase_));

    socket->setSendTimeout(connectionOptions_.writeTimeout.count());

    auto sockAddressExpected = getSocketAddress(connectionOptions_);
    if (sockAddressExpected.hasError()) {
      const auto& ex = sockAddressExpected.error();
      LOG_FAILURE(
          "ThriftTransport",
          failure::Category::kBadEnvironment,
          "{}",
          ex.what());
      return apache::thrift::async::TAsyncTransport::UniquePtr{};
    }
    folly::SocketAddress address = std::move(sockAddressExpected).value();

    auto socketOptions = createSocketOptions(address, connectionOptions_);
    const auto mech = connectionOptions_.accessPoint->getSecurityMech();
    connectionState_ = ConnectionState::Connecting;
    if (mech == SecurityMech::TLS13_FIZZ) {
      auto fizzClient = socket->getUnderlyingTransport<McFizzClient>();
      fizzClient->connect(
          this,
          address,
          connectionOptions_.connectTimeout.count(),
          socketOptions);
    } else {
      auto asyncSock = socket->getUnderlyingTransport<folly::AsyncSocket>();
      asyncSock->connect(
          this,
          address,
          connectionOptions_.connectTimeout.count(),
          socketOptions);
    }
    return socket;
  });
}