Example #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());
}
Example #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));
  });
}
Example #3
0
void ConnectionManagerTest::testAddDuringCloseWhenIdle(bool deactivate) {
  auto extraConn = MockConnection::makeUnique(this);
  InSequence enforceOrder;

  // All conns will get closeWhenIdle
  for (const auto& conn: conns_) {
    conn->setIdle(true);
    EXPECT_CALL(*conn, closeWhenIdle());
  }
  cm_->initiateGracefulShutdown(std::chrono::milliseconds(0));

  // Add the extra conn in this state
  extraConn->setIdle(true);
  conns_.insert(conns_.begin(), std::move(extraConn));
  cm_->addConnection(conns_.front().get());
  // Shouldn't be deleted yet, call is delayed
  ASSERT_TRUE(conns_.front().get() != nullptr);

  // Mark the connection as active
  conns_.front()->setIdle(false);
  if (deactivate) {
    // mark it idle and move to the end of the list.  The regular
    // drainAllConnections code will find it and call closeWhenIdle.  The
    // second loop callback won't find the conn and be a no-op
    cm_->onDeactivated(*conns_.front());
    conns_.front()->setIdle(true);
  }
  EXPECT_CALL(*conns_.front(), closeWhenIdle());
  eventBase_.loop();
  if (!deactivate) {
    // drainAllConnections didn't find it, closeWhenIdle was invoked by the
    // second loop callback.
    cm_->onDeactivated(*conns_.front());
    conns_.front()->setIdle(true);
  }
  ASSERT_TRUE(conns_.front().get() == nullptr);
}
Example #4
0
void serverLoop(size_t threadId, folly::EventBase& evb,
                AsyncMcServerWorker& worker) {
  worker.setOnRequest(MockMcOnRequest());
  evb.loop();
}