Belle II Software light-2406-ragdoll
Downloader.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <framework/utilities/ScopeGuard.h>
12
13#include <iosfwd>
14#include <memory>
15#include <random>
16
17namespace Belle2::Conditions {
19 struct CurlSession;
20
22 class Downloader final {
23 public:
25 Downloader() = default;
28
37 bool startSession();
39 void finishSession();
44 {
45 bool started = startSession();
46 return ScopeGuard([this, started] {if (started) finishSession();});
47 }
48
50 unsigned int getConnectionTimeout() const { return m_connectionTimeout; }
52 unsigned int getStalledTimeout() const { return m_stalledTimeout; }
54 unsigned int getMaxRetries() const { return m_maxRetries; }
56 unsigned int getBackoffFactor() const { return m_backoffFactor; }
58 void setConnectionTimeout(unsigned int timeout);
60 void setStalledTimeout(unsigned int timeout);
62 void setMaxRetries(unsigned int retries) { m_maxRetries = retries; }
64 void setBackoffFactor(unsigned int factor) { m_backoffFactor = std::max(1u, factor); }
74 bool download(const std::string& url, std::ostream& stream, bool silentOnMissing = false);
75
81 bool verifyChecksum(std::istream& input, const std::string& checksum) { return calculateChecksum(input) == checksum; }
83 std::string escapeString(const std::string& text);
85 std::string joinWithSlash(const std::string& base, const std::string& second);
86
87 private:
88
93 static std::string calculateChecksum(std::istream& input);
94
96 std::unique_ptr<CurlSession> m_session;
98 static bool s_globalInit;
100 unsigned int m_connectionTimeout{60};
102 unsigned int m_stalledTimeout{60};
104 unsigned int m_maxRetries{5};
106 unsigned int m_backoffFactor{3};
107
119 std::unique_ptr<std::mt19937> m_rnd{std::make_unique<std::mt19937>()};
121 std::unique_ptr<std::uniform_real_distribution<double>> m_rndDistribution{std::make_unique<std::uniform_real_distribution<double>>()};
124 };
125} // namespace Belle2::Conditions
Simple class to encapsulate libcurl as used by the ConditionsDatabase.
Definition: Downloader.h:22
void finishSession()
Finish an existing curl session if any is active at the moment.
Definition: Downloader.cc:216
void setBackoffFactor(unsigned int factor)
Set the backoff factor for retries in seconds.
Definition: Downloader.h:64
static bool s_globalInit
flag to indicate whether curl has been initialized already
Definition: Downloader.h:98
unsigned int getMaxRetries() const
Get the number of retries to perform when downloading fails with HTTP response code >=500,...
Definition: Downloader.h:54
std::unique_ptr< std::uniform_real_distribution< double > > m_rndDistribution
A uniform real distribution for extracting random numbers.
Definition: Downloader.h:121
unsigned int getStalledTimeout() const
Get the timeout to wait for stalled connections (<10KB/s), 0 means no timeout.
Definition: Downloader.h:52
bool startSession()
Start a new curl session if none is active at the moment.
Definition: Downloader.cc:162
void initializeRandomGeneratorSeed()
Initialize the seed of the internal random number generator.
Definition: Downloader.cc:332
unsigned int m_maxRetries
Number of retries to perform when downloading fails with HTTP response code >=300.
Definition: Downloader.h:104
unsigned int m_connectionTimeout
Timeout to wait for connections in seconds.
Definition: Downloader.h:100
Downloader()=default
Create a new payload downloader.
void setStalledTimeout(unsigned int timeout)
Set the timeout to wait for stalled connections (<10KB/s), 0 disables timeout.
Definition: Downloader.cc:252
unsigned int getBackoffFactor() const
Get the backoff factor for retries in seconds.
Definition: Downloader.h:56
std::unique_ptr< CurlSession > m_session
curl session handle
Definition: Downloader.h:96
unsigned int m_stalledTimeout
Timeout to wait for stalled connections (<10KB/s)
Definition: Downloader.h:102
bool m_rndIsInitialized
Flag for keeping track if the internal random generator is correctly initialized or not.
Definition: Downloader.h:123
std::unique_ptr< std::mt19937 > m_rnd
This is a special exception in basf2 where an instance of gRandom is NOT used: since this class inter...
Definition: Downloader.h:119
std::string joinWithSlash(const std::string &base, const std::string &second)
Join two strings and make sure that there is exactly one '/' between them.
Definition: Downloader.cc:156
static std::string calculateChecksum(std::istream &input)
calculate the digest/checksum on a given string.
Definition: Downloader.cc:226
void setConnectionTimeout(unsigned int timeout)
Set the timeout to wait for connections in seconds, 0 means built in curl default.
Definition: Downloader.cc:244
std::string escapeString(const std::string &text)
Escape a string to make it safe to be used in web requests.
Definition: Downloader.cc:142
unsigned int getConnectionTimeout() const
Get the timeout to wait for connections in seconds, 0 means the built in curl default.
Definition: Downloader.h:50
ScopeGuard ensureSession()
Make sure there's an active session and return a ScopeGuard object that closes the session on destruc...
Definition: Downloader.h:43
void setMaxRetries(unsigned int retries)
Set the number of retries to perform when downloading fails with HTTP response code >=500,...
Definition: Downloader.h:62
bool verifyChecksum(std::istream &input, const std::string &checksum)
check the digest of a stream
Definition: Downloader.h:81
unsigned int m_backoffFactor
Backoff factor for retries in seconds.
Definition: Downloader.h:106
static Downloader & getDefaultInstance()
Return the default instance.
Definition: Downloader.cc:134
Simple ScopeGuard to execute a function at the end of the object lifetime.
Definition: ScopeGuard.h:36