diff options
| author | Dan Engelbrecht <[email protected]> | 2024-01-24 11:41:18 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-01-24 11:41:18 +0100 |
| commit | 0e63573fbe9973f6b922656a785817a711581b78 (patch) | |
| tree | 48e18f0b4aea958a536ba50f72f589a580c4b798 /src/zenhttp/include | |
| parent | oplog import/export improvements (#634) (diff) | |
| download | zen-0e63573fbe9973f6b922656a785817a711581b78.tar.xz zen-0e63573fbe9973f6b922656a785817a711581b78.zip | |
Add retry with optional resume logic to HttpClient::Download (#639)
- Improvement: Refactored Jupiter upstream to use HttpClient
- Improvement: Added retry and resume logic to HttpClient
- Improvement: Added authentication support to HttpClient
- Improvement: Clearer logging in GCV2 compact of FileCas/BlockStore
- Improvement: Size details in oplog import logging
Diffstat (limited to 'src/zenhttp/include')
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpclient.h | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/zenhttp/include/zenhttp/httpclient.h b/src/zenhttp/include/zenhttp/httpclient.h index 9de5c7cce..f3559f214 100644 --- a/src/zenhttp/include/zenhttp/httpclient.h +++ b/src/zenhttp/include/zenhttp/httpclient.h @@ -6,9 +6,11 @@ #include <zencore/iobuffer.h> #include <zencore/logbase.h> +#include <zencore/thread.h> #include <zencore/uid.h> #include <zenhttp/httpcommon.h> +#include <functional> #include <optional> #include <unordered_map> @@ -27,12 +29,32 @@ class CompositeBuffer; */ +struct HttpClientAccessToken +{ + using Clock = std::chrono::system_clock; + using TimePoint = Clock::time_point; + + static constexpr int64_t ExpireMarginInSeconds = 30; + + std::string Value; + TimePoint ExpireTime; + + bool IsValid() const + { + return Value.empty() == false && + ExpireMarginInSeconds < std::chrono::duration_cast<std::chrono::seconds>(ExpireTime - Clock::now()).count(); + } +}; + struct HttpClientSettings { - std::string LogCategory = "httpclient"; - std::chrono::milliseconds ConnectTimeout{3000}; - std::chrono::milliseconds Timeout{}; - bool AssumeHttp2 = false; + std::string LogCategory = "httpclient"; + std::chrono::milliseconds ConnectTimeout{3000}; + std::chrono::milliseconds Timeout{}; + std::optional<std::function<HttpClientAccessToken()>> AccessTokenProvider; + bool AssumeHttp2 = false; + bool AllowResume = false; + uint8_t RetryCount = 0; }; class HttpClient @@ -134,6 +156,7 @@ public: const CompositeBuffer& Payload, ZenContentType ContentType, const KeyValueMap& AdditionalHeader = {}); + [[nodiscard]] Response Download(std::string_view Url, const std::filesystem::path& TempFolderPath, const KeyValueMap& AdditionalHeader = {}); @@ -147,14 +170,18 @@ public: LoggerRef Logger() { return m_Log; } std::string_view GetBaseUri() const { return m_BaseUri; } + bool Authenticate(); private: + const std::optional<HttpClientAccessToken> GetAccessToken(); struct Impl; LoggerRef m_Log; std::string m_BaseUri; std::string m_SessionId; const HttpClientSettings m_ConnectionSettings; + RwLock m_AccessTokenLock; + HttpClientAccessToken m_CachedAccessToken; Ref<Impl> m_Impl; }; |