2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <folly/futures/Future.h>
19 #include <folly/Portability.h>
23 /// This namespace is for utility functions that would usually be static
24 /// members of Future, except they don't make sense there because they don't
25 /// depend on the template type (rather, on the type of their arguments in
26 /// some cases). This is the least-bad naming scheme we could think of. Some
27 /// of the functions herein have really-likely-to-collide names, like "map"
30 /// Returns a Future that will complete after the specified duration. The
31 /// Duration typedef of a `std::chrono` duration type indicates the
32 /// resolution you can expect to be meaningful (milliseconds at the time of
33 /// writing). Normally you wouldn't need to specify a Timekeeper, we will
34 /// use the global futures timekeeper (we run a thread whose job it is to
35 /// keep time for futures timeouts) but we provide the option for power
38 /// The Timekeeper thread will be lazily created the first time it is
39 /// needed. If your program never uses any timeouts or other time-based
40 /// Futures you will pay no Timekeeper thread overhead.
41 Future<Unit> sleep(Duration, Timekeeper* = nullptr);
44 * Set func as the callback for each input Future and return a vector of
45 * Futures containing the results in the input order.
47 template <class It, class F,
48 class ItT = typename std::iterator_traits<It>::value_type,
50 = typename decltype(std::declval<ItT>().then(std::declval<F>()))::value_type>
51 std::vector<Future<Result>> map(It first, It last, F func);
53 // Sugar for the most common case
54 template <class Collection, class F>
55 auto map(Collection&& c, F&& func)
56 -> decltype(map(c.begin(), c.end(), func)) {
57 return map(c.begin(), c.end(), std::forward<F>(func));
60 } // namespace futures
63 Make a completed Future by moving in a value. e.g.
66 auto f = makeFuture(std::move(foo));
70 auto f = makeFuture<string>("foo");
73 Future<typename std::decay<T>::type> makeFuture(T&& t);
75 /** Make a completed void Future. */
76 Future<Unit> makeFuture();
79 Make a Future by executing a function.
81 If the function returns a value of type T, makeFutureWith
82 returns a completed Future<T>, capturing the value returned
85 If the function returns a Future<T> already, makeFutureWith
88 Either way, if the function throws, a failed Future is
89 returned that captures the exception.
91 Calling makeFutureWith(func) is equivalent to calling
92 makeFuture().then(func).
95 // makeFutureWith(Future<T>()) -> Future<T>
97 typename std::enable_if<isFuture<typename std::result_of<F()>::type>::value,
98 typename std::result_of<F()>::type>::type
99 makeFutureWith(F&& func);
101 // makeFutureWith(T()) -> Future<T>
102 // makeFutureWith(void()) -> Future<Unit>
104 typename std::enable_if<
105 !(isFuture<typename std::result_of<F()>::type>::value),
106 Future<typename Unit::Lift<typename std::result_of<F()>::type>::type>>::type
107 makeFutureWith(F&& func);
109 /// Make a failed Future from an exception_ptr.
110 /// Because the Future's type cannot be inferred you have to specify it, e.g.
112 /// auto f = makeFuture<string>(std::current_exception());
114 FOLLY_DEPRECATED("use makeFuture(exception_wrapper)")
115 Future<T> makeFuture(std::exception_ptr const& e);
117 /// Make a failed Future from an exception_wrapper.
119 Future<T> makeFuture(exception_wrapper ew);
121 /** Make a Future from an exception type E that can be passed to
122 std::make_exception_ptr(). */
123 template <class T, class E>
124 typename std::enable_if<std::is_base_of<std::exception, E>::value,
126 makeFuture(E const& e);
128 /** Make a Future out of a Try */
130 Future<T> makeFuture(Try<T>&& t);
133 * Return a new Future that will call back on the given Executor.
134 * This is just syntactic sugar for makeFuture().via(executor)
136 * @param executor the Executor to call back on
137 * @param priority optionally, the priority to add with. Defaults to 0 which
138 * represents medium priority.
140 * @returns a void Future that will call back on the given executor
142 inline Future<Unit> via(
144 int8_t priority = Executor::MID_PRI);
146 /// Execute a function via the given executor and return a future.
147 /// This is semantically equivalent to via(executor).then(func), but
148 /// easier to read and slightly more efficient.
149 template <class Func>
150 auto via(Executor*, Func func)
151 -> Future<typename isFuture<decltype(func())>::Inner>;
153 /** When all the input Futures complete, the returned Future will complete.
154 Errors do not cause early termination; this Future will always succeed
155 after all its Futures have finished (whether successfully or with an
158 The Futures are moved in, so your copies are invalid. If you need to
159 chain further from these Futures, use the variant with an output iterator.
161 This function is thread-safe for Futures running on different threads. But
162 if you are doing anything non-trivial after, you will probably want to
163 follow with `via(executor)` because it will complete in whichever thread the
164 last Future completes in.
166 The return type for Future<T> input is a Future<std::vector<Try<T>>>
168 template <class InputIterator>
169 Future<std::vector<Try<
170 typename std::iterator_traits<InputIterator>::value_type::value_type>>>
171 collectAll(InputIterator first, InputIterator last);
173 /// Sugar for the most common case
174 template <class Collection>
175 auto collectAll(Collection&& c) -> decltype(collectAll(c.begin(), c.end())) {
176 return collectAll(c.begin(), c.end());
179 /// This version takes a varying number of Futures instead of an iterator.
180 /// The return type for (Future<T1>, Future<T2>, ...) input
181 /// is a Future<std::tuple<Try<T1>, Try<T2>, ...>>.
182 /// The Futures are moved in, so your copies are invalid.
183 template <typename... Fs>
184 typename detail::CollectAllVariadicContext<
185 typename std::decay<Fs>::type::value_type...>::type
186 collectAll(Fs&&... fs);
188 /// Like collectAll, but will short circuit on the first exception. Thus, the
189 /// type of the returned Future is std::vector<T> instead of
190 /// std::vector<Try<T>>
191 template <class InputIterator>
192 Future<typename detail::CollectContext<
193 typename std::iterator_traits<InputIterator>::value_type::value_type
195 collect(InputIterator first, InputIterator last);
197 /// Sugar for the most common case
198 template <class Collection>
199 auto collect(Collection&& c) -> decltype(collect(c.begin(), c.end())) {
200 return collect(c.begin(), c.end());
203 /// Like collectAll, but will short circuit on the first exception. Thus, the
204 /// type of the returned Future is std::tuple<T1, T2, ...> instead of
205 /// std::tuple<Try<T1>, Try<T2>, ...>
206 template <typename... Fs>
207 typename detail::CollectVariadicContext<
208 typename std::decay<Fs>::type::value_type...>::type
211 /** The result is a pair of the index of the first Future to complete and
212 the Try. If multiple Futures complete at the same time (or are already
213 complete when passed in), the "winner" is chosen non-deterministically.
215 This function is thread-safe for Futures running on different threads.
217 template <class InputIterator>
220 Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>
221 collectAny(InputIterator first, InputIterator last);
223 /// Sugar for the most common case
224 template <class Collection>
225 auto collectAny(Collection&& c) -> decltype(collectAny(c.begin(), c.end())) {
226 return collectAny(c.begin(), c.end());
229 /** when n Futures have completed, the Future completes with a vector of
230 the index and Try of those n Futures (the indices refer to the original
231 order, but the result vector will be in an arbitrary order)
235 template <class InputIterator>
236 Future<std::vector<std::pair<
238 Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>>
239 collectN(InputIterator first, InputIterator last, size_t n);
241 /// Sugar for the most common case
242 template <class Collection>
243 auto collectN(Collection&& c, size_t n)
244 -> decltype(collectN(c.begin(), c.end(), n)) {
245 return collectN(c.begin(), c.end(), n);
248 /** window creates up to n Futures using the values
249 in the collection, and then another Future for each Future
252 this is basically a sliding window of Futures of size n
254 func must return a Future for each value in input
256 template <class Collection, class F,
257 class ItT = typename std::iterator_traits<
258 typename Collection::iterator>::value_type,
259 class Result = typename detail::resultOf<F, ItT&&>::value_type>
260 std::vector<Future<Result>>
261 window(Collection input, F func, size_t n);
263 template <typename F, typename T, typename ItT>
264 using MaybeTryArg = typename std::conditional<
265 detail::callableWith<F, T&&, Try<ItT>&&>::value, Try<ItT>, ItT>::type;
267 template<typename F, typename T, typename Arg>
268 using isFutureResult = isFuture<typename std::result_of<F(T&&, Arg&&)>::type>;
270 /** repeatedly calls func on every result, e.g.
271 reduce(reduce(reduce(T initial, result of first), result of second), ...)
273 The type of the final result is a Future of the type of the initial value.
275 Func can either return a T, or a Future<T>
277 func is called in order of the input, see unorderedReduce if that is not
280 template <class It, class T, class F>
281 Future<T> reduce(It first, It last, T&& initial, F&& func);
283 /// Sugar for the most common case
284 template <class Collection, class T, class F>
285 auto reduce(Collection&& c, T&& initial, F&& func)
286 -> decltype(reduce(c.begin(), c.end(), std::forward<T>(initial),
287 std::forward<F>(func))) {
291 std::forward<T>(initial),
292 std::forward<F>(func));
295 /** like reduce, but calls func on finished futures as they complete
296 does NOT keep the order of the input
298 template <class It, class T, class F,
299 class ItT = typename std::iterator_traits<It>::value_type::value_type,
300 class Arg = MaybeTryArg<F, T, ItT>>
301 Future<T> unorderedReduce(It first, It last, T initial, F func);
303 /// Sugar for the most common case
304 template <class Collection, class T, class F>
305 auto unorderedReduce(Collection&& c, T&& initial, F&& func)
306 -> decltype(unorderedReduce(c.begin(), c.end(), std::forward<T>(initial),
307 std::forward<F>(func))) {
308 return unorderedReduce(
311 std::forward<T>(initial),
312 std::forward<F>(func));
320 * Given a policy and a future-factory, creates futures according to the
323 * The policy must be moveable - retrying will move it a lot - and callable of
324 * either of the two forms:
325 * - Future<bool>(size_t, exception_wrapper)
326 * - bool(size_t, exception_wrapper)
327 * Internally, the latter is transformed into the former in the obvious way.
328 * The first parameter is the attempt number of the next prospective attempt;
329 * the second parameter is the most recent exception. The policy returns a
330 * Future<bool> which, when completed with true, indicates that a retry is
333 * We provide a few generic policies:
335 * - CappedJitteredexponentialBackoff
337 * Custom policies may use the most recent try number and exception to decide
338 * whether to retry and optionally to do something interesting like delay
339 * before the retry. Users may pass inline lambda expressions as policies, or
340 * may define their own data types meeting the above requirements. Users are
341 * responsible for managing the lifetimes of anything pointed to or referred to
342 * from inside the policy.
344 * For example, one custom policy may try up to k times, but only if the most
345 * recent exception is one of a few types or has one of a few error codes
346 * indicating that the failure was transitory.
348 * Cancellation is not supported.
350 template <class Policy, class FF>
351 typename std::result_of<FF(size_t)>::type
352 retrying(Policy&& p, FF&& ff);
355 * generic retrying policies
359 std::function<bool(size_t, const exception_wrapper&)>
363 template <class Policy, class URNG>
364 std::function<Future<bool>(size_t, const exception_wrapper&)>
365 retryingPolicyCappedJitteredExponentialBackoff(
367 Duration backoff_min,
368 Duration backoff_max,
374 std::function<Future<bool>(size_t, const exception_wrapper&)>
375 retryingPolicyCappedJitteredExponentialBackoff(
377 Duration backoff_min,
378 Duration backoff_max,
379 double jitter_param);