示例#1
0
proxy_t::Pointer proxy_t::createProxy(McrouterInstance& router,
                                      folly::EventBase& eventBase) {
  /* This hack is needed to make sure proxy_t stays alive
     until at least event base managed to run the callback below */
  auto proxy = std::shared_ptr<proxy_t>(new proxy_t(router));
  proxy->self_ = proxy;

  eventBase.runInEventBaseThread(
    [proxy, &eventBase] () {
      proxy->eventBase_ = &eventBase;
      proxy->messageQueue_->attachEventBase(eventBase);

      dynamic_cast<folly::fibers::EventBaseLoopController&>(
        proxy->fiberManager.loopController()).attachEventBase(eventBase);

      std::chrono::milliseconds connectionResetInterval{
        proxy->router_.opts().reset_inactive_connection_interval
      };

      if (connectionResetInterval.count() > 0) {
        proxy->destinationMap->setResetTimer(connectionResetInterval);
      }

      if (proxy->router_.opts().cpu_cycles) {
        cycles::attachEventBase(eventBase);
        proxy->fiberManager.setObserver(&proxy->cyclesObserver);
      }
    });

  return Pointer(proxy.get());
}
示例#2
0
bool startObservingFile(const std::string& filePath,
                        folly::EventBase& evb,
                        uint32_t pollPeriodMs,
                        uint32_t sleepBeforeUpdateMs,
                        std::function<void(std::string)> onUpdate,
                        std::function<void()> fallbackOnError) {

  std::shared_ptr<FileDataProvider> provider;
  try {
    provider = std::make_shared<FileDataProvider>(filePath);

    onUpdate(provider->load());
  } catch (const std::exception& e) {
    VLOG(0) << "Can not start watching " << filePath <<
               " for modifications: " << e.what();
    checkAndExecuteFallbackOnError(std::move(fallbackOnError));
    return false;
  }

  VLOG(0) << "Watching " << filePath << " for modifications.";
  FileObserverData data(std::move(provider),
                        std::move(onUpdate),
                        std::move(fallbackOnError),
                        pollPeriodMs,
                        sleepBeforeUpdateMs);
  return evb.runInEventBaseThread([&evb, data = std::move(data)]() {
    scheduleObserveFile(evb, std::move(data));
  });
}