Add MSVC support to futures/Deprecated.h
[folly.git] / folly / futures / helpers.h
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #pragma once
17
18 #include <folly/futures/Future.h>
19 #include <folly/Portability.h>
20
21 namespace folly {
22
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"
28 /// and "sleep".
29 namespace futures {
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
36   /// users.
37   ///
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);
42
43   /**
44    * Set func as the callback for each input Future and return a vector of
45    * Futures containing the results in the input order.
46    */
47   template <class It, class F,
48             class ItT = typename std::iterator_traits<It>::value_type,
49             class Result
50       = typename decltype(std::declval<ItT>().then(std::declval<F>()))::value_type>
51   std::vector<Future<Result>> map(It first, It last, F func);
52
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));
58   }
59
60 } // namespace futures
61
62 /**
63   Make a completed Future by moving in a value. e.g.
64
65     string foo = "foo";
66     auto f = makeFuture(std::move(foo));
67
68   or
69
70     auto f = makeFuture<string>("foo");
71 */
72 template <class T>
73 Future<typename std::decay<T>::type> makeFuture(T&& t);
74
75 /** Make a completed void Future. */
76 Future<Unit> makeFuture();
77
78 /** Make a completed Future by executing a function. If the function throws
79   we capture the exception, otherwise we capture the result. */
80 template <class F>
81 auto makeFutureWith(F&& func)
82     -> Future<typename Unit::Lift<decltype(func())>::type>;
83
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.
86 ///
87 ///   auto f = makeFuture<string>(std::current_exception());
88 template <class T>
89 FOLLY_DEPRECATED("use makeFuture(exception_wrapper)")
90 Future<T> makeFuture(std::exception_ptr const& e);
91
92 /// Make a failed Future from an exception_wrapper.
93 template <class T>
94 Future<T> makeFuture(exception_wrapper ew);
95
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,
100                         Future<T>>::type
101 makeFuture(E const& e);
102
103 /** Make a Future out of a Try */
104 template <class T>
105 Future<T> makeFuture(Try<T>&& t);
106
107 /*
108  * Return a new Future that will call back on the given Executor.
109  * This is just syntactic sugar for makeFuture().via(executor)
110  *
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.
114  *
115  * @returns a void Future that will call back on the given executor
116  */
117 inline Future<Unit> via(
118     Executor* executor,
119     int8_t priority = Executor::MID_PRI);
120
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>;
127
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
131   error).
132
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.
135
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.
140
141   The return type for Future<T> input is a Future<std::vector<Try<T>>>
142   */
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);
147
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());
152 }
153
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);
162
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
169 >::result_type>
170 collect(InputIterator first, InputIterator last);
171
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());
176 }
177
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
184 collect(Fs&&... fs);
185
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.
189
190   This function is thread-safe for Futures running on different threads.
191   */
192 template <class InputIterator>
193 Future<std::pair<
194   size_t,
195   Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>
196 collectAny(InputIterator first, InputIterator last);
197
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());
202 }
203
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)
207
208   Not thread safe.
209   */
210 template <class InputIterator>
211 Future<std::vector<std::pair<
212   size_t,
213   Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>>
214 collectN(InputIterator first, InputIterator last, size_t n);
215
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);
221 }
222
223 /** window creates up to n Futures using the values
224     in the collection, and then another Future for each Future
225     that completes
226
227     this is basically a sliding window of Futures of size n
228
229     func must return a Future for each value in input
230   */
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);
237
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;
241
242 template<typename F, typename T, typename Arg>
243 using isFutureResult = isFuture<typename std::result_of<F(T&&, Arg&&)>::type>;
244
245 /** repeatedly calls func on every result, e.g.
246     reduce(reduce(reduce(T initial, result of first), result of second), ...)
247
248     The type of the final result is a Future of the type of the initial value.
249
250     Func can either return a T, or a Future<T>
251
252     func is called in order of the input, see unorderedReduce if that is not
253     a requirement
254   */
255 template <class It, class T, class F>
256 Future<T> reduce(It first, It last, T&& initial, F&& func);
257
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))) {
263   return reduce(
264       c.begin(),
265       c.end(),
266       std::forward<T>(initial),
267       std::forward<F>(func));
268 }
269
270 /** like reduce, but calls func on finished futures as they complete
271     does NOT keep the order of the input
272   */
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);
277
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(
284       c.begin(),
285       c.end(),
286       std::forward<T>(initial),
287       std::forward<F>(func));
288 }
289
290 namespace futures {
291
292 /**
293  *  retrying
294  *
295  *  Given a policy and a future-factory, creates futures according to the
296  *  policy.
297  *
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
306  *  desired.
307  *
308  *  We provide a few generic policies:
309  *  - Basic
310  *  - CappedJitteredexponentialBackoff
311  *
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.
318  *
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.
322  *
323  *  Cancellation is not supported.
324  */
325 template <class Policy, class FF>
326 typename std::result_of<FF(size_t)>::type
327 retrying(Policy&& p, FF&& ff);
328
329 /**
330  *  generic retrying policies
331  */
332
333 inline
334 std::function<bool(size_t, const exception_wrapper&)>
335 retryingPolicyBasic(
336     size_t max_tries);
337
338 template <class Policy, class URNG>
339 std::function<Future<bool>(size_t, const exception_wrapper&)>
340 retryingPolicyCappedJitteredExponentialBackoff(
341     size_t max_tries,
342     Duration backoff_min,
343     Duration backoff_max,
344     double jitter_param,
345     URNG rng,
346     Policy&& p);
347
348 inline
349 std::function<Future<bool>(size_t, const exception_wrapper&)>
350 retryingPolicyCappedJitteredExponentialBackoff(
351     size_t max_tries,
352     Duration backoff_min,
353     Duration backoff_max,
354     double jitter_param);
355
356 }
357
358 } // namespace