Esempio n. 1
0
Future<http::Response> MetricsProcess::_snapshot(
    const http::Request& request,
    const Option<string>& /* principal */)
{
  // Parse the 'timeout' parameter.
  Option<Duration> timeout;

  if (request.url.query.contains("timeout")) {
    string parameter = request.url.query.get("timeout").get();

    Try<Duration> duration = Duration::parse(parameter);

    if (duration.isError()) {
      return http::BadRequest(
          "Invalid timeout '" + parameter + "': " + duration.error() + ".\n");
    }

    timeout = duration.get();
  }

  Future<Nothing> acquire = Nothing();

  if (limiter.isSome()) {
    acquire = limiter.get()->acquire();
  }

  return acquire.then(defer(self(), &Self::snapshot, timeout))
      .then([request](const hashmap<string, double>& metrics)
            -> http::Response {
        return http::OK(jsonify(metrics), request.url.query.get("jsonp"));
      });
}
Esempio n. 2
0
Future<void> ServerPlugin::execute(const std::string &iUriPath, std::shared_ptr<ServerConnection> iTransaction) {
  using CallbacksIntType = std::list<std::shared_ptr<CallbackDataBase>>;
  using CallbacksMapIntType = std::map<size_t, CallbacksIntType>;
  CallbacksMapIntType callbacksMap;
  const std::string uriPath = iTransaction->getRequest().url().path();
  // Plugins
  for (std::shared_ptr<PluginData> plugin : _plugins) {
    size_t length;
    if (plugin->matchUri(iUriPath, length)) {
      callbacksMap[length].push_back(plugin);
    }
  }
  // callbacks use
  getCallbacksByMethod("", iUriPath, callbacksMap);
  // callbacks by method
  getCallbacksByMethod(iTransaction->getRequest().getMethodStr(), iUriPath, callbacksMap);
  // execute
  Future<void> result = Promise<void>::Resolve(_loop);
  bool isFirst = true;
  for (CallbacksMapIntType::value_type &callbacksIt : callbacksMap) {
    std::string newUriPath = iUriPath.substr(callbacksIt.first);
    for (std::shared_ptr<CallbackDataBase> callback : callbacksIt.second) {
      std::weak_ptr<CallbackDataBase> callbackWeak = callback;

      if (!callback) {
        continue;
      }

      callback->uri.extract(iTransaction->getRequest().getUriTemplateValues(), iUriPath, false);

      if (!isFirst) {
        result = result.then([callbackWeak, iTransaction, newUriPath]() -> Future<void> {
          if (iTransaction->getResponse().isSent()) {
            return Promise<void>::Resolve(iTransaction->_server->_loop);
          }
          return callbackWeak.lock()->execute(newUriPath, iTransaction);
        });
      } else {
        isFirst = false;
        result = callback->execute(iUriPath, iTransaction);
      }
    }
  }

  return result;
}
Esempio n. 3
0
 Future<string> func2(const Future<int>& f)
 {
   return f.then(defer(self(), &Self::_func2));
 }
Esempio n. 4
0
 Future<string> func1(const Future<int>& f)
 {
   return f.then(defer(self(), &Self::_func1, lambda::_1));
 }