Example #1
0
HttpServer::HttpServer(void *sslCTX /* = NULL */)
  : m_stopped(false), m_stopReason(nullptr), m_sslCTX(sslCTX),
    m_watchDog(this, &HttpServer::watchDog) {

  // enabling mutex profiling, but it's not turned on
  LockProfiler::s_pfunc_profile = server_stats_log_mutex;

  int startingThreadCount = RuntimeOption::ServerThreadCount;
  uint32_t additionalThreads = 0;
  if (RuntimeOption::ServerWarmupThrottleRequestCount > 0 &&
      RuntimeOption::ServerThreadCount > kNumProcessors) {
    startingThreadCount = kNumProcessors;
    additionalThreads = RuntimeOption::ServerThreadCount - startingThreadCount;
  }

  auto serverFactory = ServerFactoryRegistry::getInstance()->getFactory
      (RuntimeOption::ServerType);
  ServerOptions options
    (RuntimeOption::ServerIP, RuntimeOption::ServerPort,
     startingThreadCount);
  options.m_serverFD = RuntimeOption::ServerPortFd;
  options.m_sslFD = RuntimeOption::SSLPortFd;
  options.m_takeoverFilename = RuntimeOption::TakeoverFilename;
  m_pageServer = serverFactory->createServer(options);
  m_pageServer->addTakeoverListener(this);

  if (additionalThreads) {
    auto handlerFactory = boost::make_shared<WarmupRequestHandlerFactory>(
        m_pageServer, additionalThreads,
        RuntimeOption::ServerWarmupThrottleRequestCount,
        RuntimeOption::RequestTimeoutSeconds);
    m_pageServer->setRequestHandlerFactory([handlerFactory] {
      return handlerFactory->createHandler();
    });
  } else {
    m_pageServer->setRequestHandlerFactory<HttpRequestHandler>(
      RuntimeOption::RequestTimeoutSeconds);
  }

  if (RuntimeOption::EnableSSL && m_sslCTX) {
    assert(SSLInit::IsInited());
    m_pageServer->enableSSL(m_sslCTX, RuntimeOption::SSLPort);
  }

  m_adminServer = ServerFactoryRegistry::createServer
    (RuntimeOption::ServerType,
     RuntimeOption::ServerIP, RuntimeOption::AdminServerPort,
     RuntimeOption::AdminThreadCount);
  m_adminServer->setRequestHandlerFactory<AdminRequestHandler>(
    RuntimeOption::RequestTimeoutSeconds);

  for (unsigned int i = 0; i < RuntimeOption::SatelliteServerInfos.size();
       i++) {
    SatelliteServerInfoPtr info = RuntimeOption::SatelliteServerInfos[i];
    SatelliteServerPtr satellite = SatelliteServer::Create(info);
    if (satellite) {
      if (info->getType() == SatelliteServer::Type::KindOfDanglingPageServer) {
        m_danglings.push_back(satellite);
      } else {
        m_satellites.push_back(satellite);
      }
    }
  }

  if (RuntimeOption::XboxServerPort != 0) {
    SatelliteServerInfoPtr xboxInfo(new XboxServerInfo());
    SatelliteServerPtr satellite = SatelliteServer::Create(xboxInfo);
    if (satellite) {
      m_satellites.push_back(satellite);
    }
  }

  if (RuntimeOption::EnableStaticContentCache) {
    StaticContentCache::TheCache.load();
  }

  hphp_process_init();

  Server::InstallStopSignalHandlers(m_pageServer);
  Server::InstallStopSignalHandlers(m_adminServer);

  if (!RuntimeOption::StartupDocument.empty()) {
    Hdf hdf;
    hdf["cmd"] = static_cast<int>(Transport::Method::GET);
    hdf["url"] = RuntimeOption::StartupDocument;
    hdf["remote_host"] = RuntimeOption::ServerIP;

    ReplayTransport rt;
    rt.replayInput(hdf);
    HttpRequestHandler handler(0);
    handler.handleRequest(&rt);
    int code = rt.getResponseCode();
    if (code == 200) {
      Logger::Info("StartupDocument %s returned 200 OK: %s",
                   RuntimeOption::StartupDocument.c_str(),
                   rt.getResponse().c_str());
    } else {
      Logger::Error("StartupDocument %s failed %d: %s",
                    RuntimeOption::StartupDocument.c_str(),
                    code, rt.getResponse().data());
      return;
    }
  }

  for (unsigned int i = 0; i < RuntimeOption::ThreadDocuments.size(); i++) {
    ServiceThreadPtr thread
      (new ServiceThread(RuntimeOption::ThreadDocuments[i]));
    m_serviceThreads.push_back(thread);
  }

  for (unsigned int i = 0; i < RuntimeOption::ThreadLoopDocuments.size(); i++) {
    ServiceThreadPtr thread
      (new ServiceThread(RuntimeOption::ThreadLoopDocuments[i], true));
    m_serviceThreads.push_back(thread);
  }
}
Example #2
0
HttpServer::HttpServer(void *sslCTX /* = NULL */)
  : m_stopped(false), m_sslCTX(sslCTX),
    m_loggerThread(this, &HttpServer::flushLog),
    m_watchDog(this, &HttpServer::watchDog) {

  // enabling mutex profiling, but it's not turned on
  LockProfiler::s_pfunc_profile = server_stats_log_mutex;

  if (RuntimeOption::TakeoverFilename.empty()) {
    m_pageServer = ServerPtr
      (new TypedServer<LibEventServer, HttpRequestHandler>
       (RuntimeOption::ServerIP, RuntimeOption::ServerPort,
        RuntimeOption::ServerThreadCount,
        RuntimeOption::RequestTimeoutSeconds));
  } else {
    LibEventServerWithTakeover* server =
      (new TypedServer<LibEventServerWithTakeover, HttpRequestHandler>
       (RuntimeOption::ServerIP, RuntimeOption::ServerPort,
        RuntimeOption::ServerThreadCount,
        RuntimeOption::RequestTimeoutSeconds));
    server->setTransferFilename(RuntimeOption::TakeoverFilename);
    server->addTakeoverListener(this);
    m_pageServer = ServerPtr(server);
  }

  if (RuntimeOption::EnableSSL && m_sslCTX) {
    SSLInit::Init();
    m_pageServer->enableSSL(m_sslCTX, RuntimeOption::SSLPort);
  }

  m_adminServer = ServerPtr
    (new TypedServer<LibEventServer, AdminRequestHandler>
     (RuntimeOption::ServerIP, RuntimeOption::AdminServerPort,
      RuntimeOption::AdminThreadCount,
      RuntimeOption::RequestTimeoutSeconds));

  for (unsigned int i = 0; i < RuntimeOption::SatelliteServerInfos.size();
       i++) {
    SatelliteServerInfoPtr info = RuntimeOption::SatelliteServerInfos[i];
    SatelliteServerPtr satellite = SatelliteServer::Create(info);
    if (satellite) {
      if (info->getType() == SatelliteServer::KindOfDanglingPageServer) {
        m_danglings.push_back(satellite);
      } else {
        m_satellites.push_back(satellite);
      }
    }
  }

  if (RuntimeOption::XboxServerPort != 0) {
    SatelliteServerInfoPtr xboxInfo(new XboxServerInfo());
    SatelliteServerPtr satellite = SatelliteServer::Create(xboxInfo);
    if (satellite) {
      m_satellites.push_back(satellite);
    }
  }

  if (RuntimeOption::EnableStaticContentCache) {
    StaticContentCache::TheCache.load();
  }
  SourceInfo::TheSourceInfo.load();
  RTTIInfo::TheRTTIInfo.init(true);

  hphp_process_init();

  Server::InstallStopSignalHandlers(m_pageServer);
  Server::InstallStopSignalHandlers(m_adminServer);

  if (!RuntimeOption::StartupDocument.empty()) {
    Hdf hdf;
    hdf["cmd"] = Transport::GET;
    hdf["url"] = RuntimeOption::StartupDocument;
    hdf["remote_host"] = RuntimeOption::ServerIP;

    ReplayTransport rt;
    rt.replayInput(hdf);
    HttpRequestHandler handler;
    handler.handleRequest(&rt);
    int code = rt.getResponseCode();
    if (code == 200) {
      Logger::Info("StartupDocument %s returned 200 OK: %s",
                   RuntimeOption::StartupDocument.c_str(),
                   rt.getResponse().c_str());
    } else {
      Logger::Error("StartupDocument %s failed %d: %s",
                    RuntimeOption::StartupDocument.c_str(),
                    code, rt.getResponse().data());
      return;
    }
  }

  for (unsigned int i = 0; i < RuntimeOption::ThreadDocuments.size(); i++) {
    ServiceThreadPtr thread
      (new ServiceThread(RuntimeOption::ThreadDocuments[i]));
    m_serviceThreads.push_back(thread);
  }

  for (unsigned int i = 0; i < RuntimeOption::ThreadLoopDocuments.size(); i++) {
    ServiceThreadPtr thread
      (new ServiceThread(RuntimeOption::ThreadLoopDocuments[i], true));
    m_serviceThreads.push_back(thread);
  }
}