proxygen
folly::futures Namespace Reference

Namespaces

 detail
 
 test
 

Classes

class  Barrier
 

Functions

template<class It , class F , class ItT , class Result >
std::vector< Future< Result > > map (It first, It last, F func)
 
template<class It , class F , class ItT , class Result >
std::vector< Future< Result > > map (Executor &exec, It first, It last, F func)
 
Future< Unitsleep (Duration dur, Timekeeper *tk)
 
template<class Collection , class F >
auto map (Collection &&c, F &&func) -> decltype(map(c.begin(), c.end(), func))
 
template<class Collection , class F >
auto map (Executor &exec, Collection &&c, F &&func) -> decltype(map(exec, c.begin(), c.end(), func))
 
template<class Policy , class FF >
invoke_result_t< FF, size_t > retrying (Policy &&p, FF &&ff)
 
std::function< bool(size_t, const exception_wrapper &)> retryingPolicyBasic (size_t max_tries)
 
template<class Policy , class URNG >
std::function< Future< bool >size_t, const exception_wrapper &)> retryingPolicyCappedJitteredExponentialBackoff (size_t max_tries, Duration backoff_min, Duration backoff_max, double jitter_param, URNG &&rng, Policy &&p)
 
std::function< Future< bool >size_t, const exception_wrapper &)> retryingPolicyCappedJitteredExponentialBackoff (size_t max_tries, Duration backoff_min, Duration backoff_max, double jitter_param)
 

Detailed Description

This namespace is for utility functions that would usually be static members of Future, except they don't make sense there because they don't depend on the template type (rather, on the type of their arguments in some cases). This is the least-bad naming scheme we could think of. Some of the functions herein have really-likely-to-collide names, like "map" and "sleep".

Function Documentation

template<class Collection , class F >
auto folly::futures::map ( Collection &&  c,
F &&  func 
) -> decltype(map(c.begin(), c.end(), func))

Definition at line 78 of file helpers.h.

References c, and map().

78  {
79  return map(c.begin(), c.end(), std::forward<F>(func));
80 }
static Map map(mapCap)
char c
template<class Collection , class F >
auto folly::futures::map ( Executor exec,
Collection &&  c,
F &&  func 
) -> decltype(map(exec, c.begin(), c.end(), func))
template<class It , class F , class ItT , class Result >
std::vector< Future< Result > > folly::futures::map ( It  first,
It  last,
func 
)

Set func as the callback for each input Future and return a vector of Futures containing the results in the input order.

Definition at line 2358 of file Future-inl.h.

References FOLLY_GNU_DISABLE_WARNING, FOLLY_POP_WARNING, FOLLY_PUSH_WARNING, and folly::gen::move.

Referenced by complexBenchmark(), map(), and TEST().

2358  {
2359  std::vector<Future<Result>> results;
2360  results.reserve(std::distance(first, last));
2361  for (auto it = first; it != last; it++) {
2363  FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations")
2364  results.push_back(std::move(*it).then(func));
2366  }
2367  return results;
2368 }
#define FOLLY_GNU_DISABLE_WARNING(warningName)
Definition: Portability.h:180
#define FOLLY_POP_WARNING
Definition: Portability.h:179
#define FOLLY_PUSH_WARNING
Definition: Portability.h:178
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
void BENCHFUN() push_back(size_t iters, size_t arg)
constexpr detail::First first
Definition: Base-inl.h:2553
template<class It , class F , class ItT , class Result >
std::vector< Future< Result > > folly::futures::map ( Executor exec,
It  first,
It  last,
func 
)

Set func as the callback for each input Future and return a vector of Futures containing the results in the input order and completing on exec.

Definition at line 2371 of file Future-inl.h.

References FOLLY_GNU_DISABLE_WARNING, FOLLY_POP_WARNING, FOLLY_PUSH_WARNING, and folly::gen::move.

2371  {
2372  std::vector<Future<Result>> results;
2373  results.reserve(std::distance(first, last));
2374  for (auto it = first; it != last; it++) {
2376  FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations")
2377  results.push_back(std::move(*it).via(&exec).then(func));
2379  }
2380  return results;
2381 }
#define FOLLY_GNU_DISABLE_WARNING(warningName)
Definition: Portability.h:180
#define FOLLY_POP_WARNING
Definition: Portability.h:179
#define FOLLY_PUSH_WARNING
Definition: Portability.h:178
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
void BENCHFUN() push_back(size_t iters, size_t arg)
Future< Unit > via(Executor::KeepAlive<> executor, int8_t priority)
Definition: Future-inl.h:1373
constexpr detail::First first
Definition: Base-inl.h:2553
template<class Policy , class FF >
invoke_result_t< FF, size_t > folly::futures::retrying ( Policy &&  p,
FF &&  ff 
)

retrying

Given a policy and a future-factory, creates futures according to the policy.

The policy must be moveable - retrying will move it a lot - and callable of either of the two forms:

  • Future<bool>(size_t, exception_wrapper)
  • bool(size_t, exception_wrapper) Internally, the latter is transformed into the former in the obvious way. The first parameter is the attempt number of the next prospective attempt; the second parameter is the most recent exception. The policy returns a Future<bool> which, when completed with true, indicates that a retry is desired.

We provide a few generic policies:

  • Basic
  • CappedJitteredexponentialBackoff

Custom policies may use the most recent try number and exception to decide whether to retry and optionally to do something interesting like delay before the retry. Users may pass inline lambda expressions as policies, or may define their own data types meeting the above requirements. Users are responsible for managing the lifetimes of anything pointed to or referred to from inside the policy.

For example, one custom policy may try up to k times, but only if the most recent exception is one of a few types or has one of a few error codes indicating that the failure was transitory.

Cancellation is not supported.

If both FF and Policy inline executes, then it is possible to hit a stack overflow due to the recursive nature of the retry implementation

Definition at line 234 of file Retrying.h.

References folly::futures::detail::retrying().

Referenced by TEST().

234  {
235  using tag = typename detail::retrying_policy_traits<Policy>::tag;
236  return detail::retrying(std::forward<Policy>(p), std::forward<FF>(ff), tag());
237 }
invoke_result_t< FF, size_t > retrying(Policy &&p, FF &&ff, retrying_policy_fut_tag)
Definition: Retrying.h:135
std::function<bool(size_t, const exception_wrapper&)> folly::futures::retryingPolicyBasic ( size_t  max_tries)
inline

Definition at line 240 of file Retrying.h.

Referenced by TEST().

240  {
241  return [=](size_t n, const exception_wrapper&) { return n < max_tries; };
242 }
template<class Policy , class URNG >
std::function<Future<bool>size_t, const exception_wrapper&)> folly::futures::retryingPolicyCappedJitteredExponentialBackoff ( size_t  max_tries,
Duration  backoff_min,
Duration  backoff_max,
double  jitter_param,
URNG &&  rng,
Policy &&  p 
)

Definition at line 246 of file Retrying.h.

References folly::futures::detail::retryingPolicyCappedJitteredExponentialBackoff(), and rng.

252  {
253  using tag = typename detail::retrying_policy_traits<Policy>::tag;
255  max_tries,
256  backoff_min,
257  backoff_max,
258  jitter_param,
259  std::forward<URNG>(rng),
260  std::forward<Policy>(p),
261  tag());
262 }
auto rng
Definition: CollectTest.cpp:31
std::function< Future< bool >size_t, const exception_wrapper &)> retryingPolicyCappedJitteredExponentialBackoff(size_t max_tries, Duration backoff_min, Duration backoff_max, double jitter_param)
Definition: Retrying.h:265
std::function<Future<bool>size_t, const exception_wrapper&)> folly::futures::retryingPolicyCappedJitteredExponentialBackoff ( size_t  max_tries,
Duration  backoff_min,
Duration  backoff_max,
double  jitter_param 
)
inline

Definition at line 265 of file Retrying.h.

References folly::gen::move, and folly::futures::detail::retryingPolicyCappedJitteredExponentialBackoff().

269  {
270  auto p = [](size_t, const exception_wrapper&) { return true; };
272  max_tries,
273  backoff_min,
274  backoff_max,
275  jitter_param,
276  ThreadLocalPRNG(),
277  std::move(p));
278 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::function< Future< bool >size_t, const exception_wrapper &)> retryingPolicyCappedJitteredExponentialBackoff(size_t max_tries, Duration backoff_min, Duration backoff_max, double jitter_param)
Definition: Retrying.h:265
Future< Unit > folly::futures::sleep ( Duration  ,
Timekeeper = nullptr 
)

Returns a Future that will complete after the specified duration. The Duration typedef of a std::chrono duration type indicates the resolution you can expect to be meaningful (milliseconds at the time of writing). Normally you wouldn't need to specify a Timekeeper, we will use the global futures timekeeper (we run a thread whose job it is to keep time for futures timeouts) but we provide the option for power users.

The Timekeeper thread will be lazily created the first time it is needed. If your program never uses any timeouts or other time-based Futures you will pay no Timekeeper thread overhead.

Definition at line 42 of file Future.cpp.

References folly::Timekeeper::after(), folly::detail::getTimekeeperSingleton(), LIKELY, and UNLIKELY.

Referenced by folly::coro::TimedWaitAwaitable< Awaitable >::await_suspend(), BENCHMARK(), folly::SemiFuture< T >::delayed(), folly::Future< T >::delayed(), wangle::ExpiringFilter< Req, Resp >::ExpiringFilter(), keepAliveTest(), RpcService::operator()(), folly::futures::detail::retryingPolicyCappedJitteredExponentialBackoff(), wangle::ExpiringFilter< Req, Resp >::startIdleTimer(), TEST(), and virtualExecutorTest().

42  {
43  std::shared_ptr<Timekeeper> tks;
44  if (LIKELY(!tk)) {
46  tk = tks.get();
47  }
48 
49  if (UNLIKELY(!tk)) {
50  return makeFuture<Unit>(FutureNoTimekeeper());
51  }
52 
53  return tk->after(dur);
54 }
std::shared_ptr< Timekeeper > getTimekeeperSingleton()
#define LIKELY(x)
Definition: Likely.h:47
#define UNLIKELY(x)
Definition: Likely.h:48