示例#1
0
Variant sockopen_impl(const HostURL &hosturl, VRefParam errnum,
                      VRefParam errstr, double timeout, bool persistent) {
  errnum = 0;
  errstr = empty_string();
  std::string key;
  if (persistent) {
    key = hosturl.getHostURL() + ":" +
          folly::to<std::string>(hosturl.getPort());
    auto sockItr = s_sockets.find(key);
    if (sockItr != s_sockets.end()) {
      auto sock = makeSmartPtr<Socket>(sockItr->second);

      if (sock->getError() == 0 && sock->checkLiveness()) {
        return Variant(sock);
      }

      // socket had an error earlier, we need to close it, remove it from
      // persistent storage, and create a new one (in that order)
      sock->close();
      s_sockets.erase(sockItr);
    }
  }

  if (timeout < 0) {
    timeout = ThreadInfo::s_threadInfo.getNoCheck()->
      m_reqInjectionData.getSocketDefaultTimeout();
  }

  auto socket = new_socket_connect(hosturl, timeout, errnum, errstr);
  if (!socket.isResource()) {
    return false;
  }

  if (persistent) {
    assert(!key.empty());
    s_sockets[key] = cast<Socket>(socket)->getData();
    assert(s_sockets[key]);
  }

  return socket;
}
示例#2
0
Variant sockopen_impl(const HostURL &hosturl, VRefParam errnum,
                      VRefParam errstr, double timeout, bool persistent) {
  errnum = 0;
  errstr = empty_string();
  std::string key;
  if (persistent) {
    key = hosturl.getHostURL() + ":" +
          folly::to<std::string>(hosturl.getPort());
    Socket *sock =
      dynamic_cast<Socket*>(g_persistentResources->get("socket", key.c_str()));
    if (sock) {
      if (sock->getError() == 0 && sock->checkLiveness()) {
        return Resource(sock);
      }

      // socket had an error earlier, we need to close it, remove it from
      // persistent storage, and create a new one (in that order)
      sock->close();
      g_persistentResources->remove("socket", key.c_str());
    }
  }

  if (timeout < 0) {
    timeout = ThreadInfo::s_threadInfo.getNoCheck()->
      m_reqInjectionData.getSocketDefaultTimeout();
  }

  auto socket = new_socket_connect(hosturl, timeout, errnum, errstr);
  if (!socket.isResource()) {
    return false;
  }

  Resource ret = socket.toResource();

  if (persistent) {
    assert(!key.empty());
    g_persistentResources->set("socket", key.c_str(), ret.getTyped<Socket>());
  }

  return ret;
}