2 * Copyright 2015 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();
78 /** Make a completed Future by executing a function. If the function throws
79 we capture the exception, otherwise we capture the result. */
81 auto makeFutureWith(F&& func)
82 -> Future<typename Unit::Lift<decltype(func())>::type>;
84 /// Make a failed Future from an exception_ptr.
85 /// Because the Future's type cannot be inferred you have to specify it, e.g.
87 /// auto f = makeFuture<string>(std::current_exception());
89 FOLLY_DEPRECATED("use makeFuture(exception_wrapper)")
90 Future<T> makeFuture(std::exception_ptr const& e);
92 /// Make a failed Future from an exception_wrapper.
94 Future<T> makeFuture(exception_wrapper ew);
96 /** Make a Future from an exception type E that can be passed to
97 std::make_exception_ptr(). */
98 template <class T, class E>
99 typename std::enable_if<std::is_base_of<std::exception, E>::value,
101 makeFuture(E const& e);
103 /** Make a Future out of a Try */
105 Future<T> makeFuture(Try<T>&& t);
108 * Return a new Future that will call back on the given Executor.
109 * This is just syntactic sugar for makeFuture().via(executor)
111 * @param executor the Executor to call back on
112 * @param priority optionally, the priority to add with. Defaults to 0 which
113 * represents medium priority.
115 * @returns a void Future that will call back on the given executor
117 inline Future<Unit> via(
119 int8_t priority = Executor::MID_PRI);
121 /// Execute a function via the given executor and return a future.
122 /// This is semantically equivalent to via(executor).then(func), but
123 /// easier to read and slightly more efficient.
124 template <class Func>
125 auto via(Executor*, Func func)
126 -> Future<typename isFuture<decltype(func())>::Inner>;
128 /** When all the input Futures complete, the returned Future will complete.
129 Errors do not cause early termination; this Future will always succeed
130 after all its Futures have finished (whether successfully or with an
133 The Futures are moved in, so your copies are invalid. If you need to
134 chain further from these Futures, use the variant with an output iterator.
136 This function is thread-safe for Futures running on different threads. But
137 if you are doing anything non-trivial after, you will probably want to
138 follow with `via(executor)` because it will complete in whichever thread the
139 last Future completes in.
141 The return type for Future<T> input is a Future<std::vector<Try<T>>>
143 template <class InputIterator>
144 Future<std::vector<Try<
145 typename std::iterator_traits<InputIterator>::value_type::value_type>>>
146 collectAll(InputIterator first, InputIterator last);
148 /// Sugar for the most common case
149 template <class Collection>
150 auto collectAll(Collection&& c) -> decltype(collectAll(c.begin(), c.end())) {
151 return collectAll(c.begin(), c.end());
154 /// This version takes a varying number of Futures instead of an iterator.
155 /// The return type for (Future<T1>, Future<T2>, ...) input
156 /// is a Future<std::tuple<Try<T1>, Try<T2>, ...>>.
157 /// The Futures are moved in, so your copies are invalid.
158 template <typename... Fs>
159 typename detail::CollectAllVariadicContext<
160 typename std::decay<Fs>::type::value_type...>::type
161 collectAll(Fs&&... fs);
163 /// Like collectAll, but will short circuit on the first exception. Thus, the
164 /// type of the returned Future is std::vector<T> instead of
165 /// std::vector<Try<T>>
166 template <class InputIterator>
167 Future<typename detail::CollectContext<
168 typename std::iterator_traits<InputIterator>::value_type::value_type
170 collect(InputIterator first, InputIterator last);
172 /// Sugar for the most common case
173 template <class Collection>
174 auto collect(Collection&& c) -> decltype(collect(c.begin(), c.end())) {
175 return collect(c.begin(), c.end());
178 /// Like collectAll, but will short circuit on the first exception. Thus, the
179 /// type of the returned Future is std::tuple<T1, T2, ...> instead of
180 /// std::tuple<Try<T1>, Try<T2>, ...>
181 template <typename... Fs>
182 typename detail::CollectVariadicContext<
183 typename std::decay<Fs>::type::value_type...>::type
186 /** The result is a pair of the index of the first Future to complete and
187 the Try. If multiple Futures complete at the same time (or are already
188 complete when passed in), the "winner" is chosen non-deterministically.
190 This function is thread-safe for Futures running on different threads.
192 template <class InputIterator>
195 Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>
196 collectAny(InputIterator first, InputIterator last);
198 /// Sugar for the most common case
199 template <class Collection>
200 auto collectAny(Collection&& c) -> decltype(collectAny(c.begin(), c.end())) {
201 return collectAny(c.begin(), c.end());
204 /** when n Futures have completed, the Future completes with a vector of
205 the index and Try of those n Futures (the indices refer to the original
206 order, but the result vector will be in an arbitrary order)
210 template <class InputIterator>
211 Future<std::vector<std::pair<
213 Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>>
214 collectN(InputIterator first, InputIterator last, size_t n);
216 /// Sugar for the most common case
217 template <class Collection>
218 auto collectN(Collection&& c, size_t n)
219 -> decltype(collectN(c.begin(), c.end(), n)) {
220 return collectN(c.begin(), c.end(), n);
223 /** window creates up to n Futures using the values
224 in the collection, and then another Future for each Future
227 this is basically a sliding window of Futures of size n
229 func must return a Future for each value in input
231 template <class Collection, class F,
232 class ItT = typename std::iterator_traits<
233 typename Collection::iterator>::value_type,
234 class Result = typename detail::resultOf<F, ItT&&>::value_type>
235 std::vector<Future<Result>>
236 window(Collection input, F func, size_t n);
238 template <typename F, typename T, typename ItT>
239 using MaybeTryArg = typename std::conditional<
240 detail::callableWith<F, T&&, Try<ItT>&&>::value, Try<ItT>, ItT>::type;
242 template<typename F, typename T, typename Arg>
243 using isFutureResult = isFuture<typename std::result_of<F(T&&, Arg&&)>::type>;
245 /** repeatedly calls func on every result, e.g.
246 reduce(reduce(reduce(T initial, result of first), result of second), ...)
248 The type of the final result is a Future of the type of the initial value.
250 Func can either return a T, or a Future<T>
252 func is called in order of the input, see unorderedReduce if that is not
255 template <class It, class T, class F>
256 Future<T> reduce(It first, It last, T&& initial, F&& func);
258 /// Sugar for the most common case
259 template <class Collection, class T, class F>
260 auto reduce(Collection&& c, T&& initial, F&& func)
261 -> decltype(reduce(c.begin(), c.end(), std::forward<T>(initial),
262 std::forward<F>(func))) {
266 std::forward<T>(initial),
267 std::forward<F>(func));
270 /** like reduce, but calls func on finished futures as they complete
271 does NOT keep the order of the input
273 template <class It, class T, class F,
274 class ItT = typename std::iterator_traits<It>::value_type::value_type,
275 class Arg = MaybeTryArg<F, T, ItT>>
276 Future<T> unorderedReduce(It first, It last, T initial, F func);
278 /// Sugar for the most common case
279 template <class Collection, class T, class F>
280 auto unorderedReduce(Collection&& c, T&& initial, F&& func)
281 -> decltype(unorderedReduce(c.begin(), c.end(), std::forward<T>(initial),
282 std::forward<F>(func))) {
283 return unorderedReduce(
286 std::forward<T>(initial),
287 std::forward<F>(func));
295 * Given a policy and a future-factory, creates futures according to the
298 * The policy must be moveable - retrying will move it a lot - and callable of
299 * either of the two forms:
300 * - Future<bool>(size_t, exception_wrapper)
301 * - bool(size_t, exception_wrapper)
302 * Internally, the latter is transformed into the former in the obvious way.
303 * The first parameter is the attempt number of the next prospective attempt;
304 * the second parameter is the most recent exception. The policy returns a
305 * Future<bool> which, when completed with true, indicates that a retry is
308 * We provide a few generic policies:
310 * - CappedJitteredexponentialBackoff
312 * Custom policies may use the most recent try number and exception to decide
313 * whether to retry and optionally to do something interesting like delay
314 * before the retry. Users may pass inline lambda expressions as policies, or
315 * may define their own data types meeting the above requirements. Users are
316 * responsible for managing the lifetimes of anything pointed to or referred to
317 * from inside the policy.
319 * For example, one custom policy may try up to k times, but only if the most
320 * recent exception is one of a few types or has one of a few error codes
321 * indicating that the failure was transitory.
323 * Cancellation is not supported.
325 template <class Policy, class FF>
326 typename std::result_of<FF(size_t)>::type
327 retrying(Policy&& p, FF&& ff);
330 * generic retrying policies
334 std::function<bool(size_t, const exception_wrapper&)>
338 template <class Policy, class URNG>
339 std::function<Future<bool>(size_t, const exception_wrapper&)>
340 retryingPolicyCappedJitteredExponentialBackoff(
342 Duration backoff_min,
343 Duration backoff_max,
349 std::function<Future<bool>(size_t, const exception_wrapper&)>
350 retryingPolicyCappedJitteredExponentialBackoff(
352 Duration backoff_min,
353 Duration backoff_max,
354 double jitter_param);