about summary refs log tree commit diff
path: root/src/libutil/retry.hh
blob: b45cb37f736b54dae18d57e3d243bd4fc9b47690 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once

#include "logging.hh"

#include <functional>
#include <cmath>
#include <random>
#include <thread>

namespace nix {

inline unsigned int retrySleepTime(unsigned int attempt)
{
    std::random_device rd;
    std::mt19937 mt19937;
    return 250.0 * std::pow(2.0f,
        attempt - 1 + std::uniform_real_distribution<>(0.0, 0.5)(mt19937));
}

template<typename C>
C retry(unsigned int attempts, std::function<C()> && f)
{
    unsigned int attempt = 0;
    while (true) {
        try {
            return f();
        } catch (BaseError & e) {
            ++attempt;
            if (attempt >= attempts || !e.isTransient())
                throw;
            auto ms = retrySleepTime(attempt);
            warn("%s; retrying in %d ms", e.what(), ms);
            std::this_thread::sleep_for(std::chrono::milliseconds(ms));
        }
    }
}

}