forked from lix-project/lix
78fa47a7f0
Once we've started writing data to a Sink, we can't restart a download
request, because then we end up writing duplicate data to the
Sink. Therefore we shouldn't handle retries in Downloader but at a
higher level (in particular, in copyStorePath()).
Fixes #2952.
(cherry picked from commit a67cf5a358
)
39 lines
855 B
C++
39 lines
855 B
C++
#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));
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|