|
| 1 | +/** |
| 2 | + * Copyright (c) 2017-present, Facebook, Inc. and its affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include <chrono> |
| 11 | + |
| 12 | +#include <folly/Expected.h> |
| 13 | +#include <folly/executors/InlineExecutor.h> |
| 14 | +#include <folly/futures/Retrying.h> |
| 15 | + |
| 16 | +namespace facebook { namespace logdevice { |
| 17 | + |
| 18 | +/** |
| 19 | + * @file RetryHandler provides an API to retry a certain function if it fails |
| 20 | + * with exponential backoff and optional jitter. |
| 21 | + * |
| 22 | + * It is a thin-wrapper around folly::futures::retrying to support retrying for |
| 23 | + * code that's not future based. |
| 24 | + * |
| 25 | + */ |
| 26 | +template <class T> |
| 27 | +class RetryHandler { |
| 28 | + public: |
| 29 | + using Result = folly::Expected<T, T>; |
| 30 | + |
| 31 | + /** |
| 32 | + * Retries the function until either we exhaust all the retries or |
| 33 | + * should_retry returns false. |
| 34 | + * |
| 35 | + * @param func: The function we want to retry, takes as a param the trial |
| 36 | + * number. |
| 37 | + * @param should_retry: Given the output of func, returns true to retry, or |
| 38 | + * false to stop retrying. |
| 39 | + * |
| 40 | + * @returns A folly::Expected indicating either: |
| 41 | + * 1- Success and contains the returned value. |
| 42 | + * 2- Error which means that we exhausted all the retries. It will also |
| 43 | + * hold the last returned value from the function. |
| 44 | + */ |
| 45 | + static folly::SemiFuture<Result> |
| 46 | + run(folly::Function<T(size_t trial_num) const> func, |
| 47 | + folly::Function<bool(const T&) const> should_retry, |
| 48 | + size_t max_tries, |
| 49 | + std::chrono::milliseconds backoff_min, |
| 50 | + std::chrono::milliseconds backoff_max, |
| 51 | + double jitter_param) { |
| 52 | + return folly::futures::retrying( |
| 53 | + folly::futures::retryingPolicyCappedJitteredExponentialBackoff( |
| 54 | + max_tries, backoff_min, backoff_max, jitter_param), |
| 55 | + [fu = std::move(func), should_retry = std::move(should_retry)]( |
| 56 | + size_t trial) -> folly::SemiFuture<Result> { |
| 57 | + T ret = fu(trial); |
| 58 | + |
| 59 | + // If it's a failure, simulate an execption for |
| 60 | + // futures::retrying to retry. |
| 61 | + if (should_retry(ret)) { |
| 62 | + return folly::make_exception_wrapper<Failure>( |
| 63 | + std::move(ret)); |
| 64 | + } else { |
| 65 | + return folly::makeExpected<T>(std::move(ret)); |
| 66 | + } |
| 67 | + }) |
| 68 | + // When we exhaust all the retries, return it as a folly unexpected |
| 69 | + // of a future carrying an exception. |
| 70 | + .deferError(folly::tag_t<Failure>(), |
| 71 | + [](Failure f) -> folly::SemiFuture<Result> { |
| 72 | + return folly::makeUnexpected(std::move(f.type)); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Synchronous version of RetryHandler<T>::run. |
| 78 | + */ |
| 79 | + static Result syncRun(folly::Function<T(size_t trial_num) const> func, |
| 80 | + folly::Function<bool(const T&) const> should_retry, |
| 81 | + size_t max_tries, |
| 82 | + std::chrono::milliseconds backoff_min, |
| 83 | + std::chrono::milliseconds backoff_max, |
| 84 | + double jitter_param) { |
| 85 | + auto& executor = folly::InlineExecutor::instance(); |
| 86 | + return run(std::move(func), |
| 87 | + std::move(should_retry), |
| 88 | + max_tries, |
| 89 | + backoff_min, |
| 90 | + backoff_max, |
| 91 | + jitter_param) |
| 92 | + .via(&executor) |
| 93 | + .get(); |
| 94 | + } |
| 95 | + |
| 96 | + private: |
| 97 | + struct Failure : std::exception { |
| 98 | + explicit Failure(T t) : type(std::move(t)) {} |
| 99 | + T type; |
| 100 | + }; |
| 101 | +}; |
| 102 | + |
| 103 | +}} // namespace facebook::logdevice |
0 commit comments