Пример #1
0
void ServiceThread::threadRun() {
  Logger::Info("Service thread %s started", m_url.c_str());
  s_service_threads.set(this);

  Hdf hdf;
  hdf["get"] = 1;
  hdf["url"] = m_url;
  hdf["remote_host"] = RuntimeOption::ServerIP;

  ReplayTransport rt;
  rt.replayInput(hdf);
  HttpRequestHandler handler;
  handler.disablePathTranslation();
  handler.handleRequest(&rt);

  Logger::Info("Service thread %s stopped", m_url.c_str());
}
Пример #2
0
void HttpServer::playShutdownRequest(const std::string& fileName) {
  if (fileName.empty()) return;
  Logger::Info("playing request upon shutdown %s", fileName.c_str());
  try {
    ReplayTransport rt;
    rt.replayInput(fileName.c_str());
    HttpRequestHandler handler(0);
    handler.run(&rt);
    if (rt.getResponseCode() == 200) {
      Logger::Info("successfully finished request: %s", rt.getUrl());
    } else {
      Logger::Error("request unsuccessful: %s", rt.getUrl());
    }
  } catch (...) {
    Logger::Error("got exception when playing request: %s",
                  fileName.c_str());
  }
}
Пример #3
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);
  }
}
Пример #4
0
HttpServer::HttpServer()
  : m_stopped(false),
    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);
  }

  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::EnableStaticContentCache) {
    StaticContentCache::TheCache.load();
  }
  ClassInfo::Load();
  SourceInfo::TheSourceInfo.load();
  RTTIInfo::TheRTTIInfo.init(true);

  hphp_process_init();
  apc_load(RuntimeOption::ApcLoadThread);

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

  if (!RuntimeOption::StartupDocument.empty()) {
    Hdf hdf;
    hdf["get"] = 1;
    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);
  }
}