forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasource.cc
92 lines (79 loc) · 3.59 KB
/
datasource.cc
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "source/common/config/datasource.h"
#include "envoy/config/core/v3/base.pb.h"
#include "fmt/format.h"
namespace Envoy {
namespace Config {
namespace DataSource {
// Parameters of the jittered backoff strategy.
static constexpr uint32_t RetryInitialDelayMilliseconds = 1000;
static constexpr uint32_t RetryMaxDelayMilliseconds = 10 * 1000;
static constexpr uint32_t RetryCount = 1;
std::string read(const envoy::config::core::v3::DataSource& source, bool allow_empty,
Api::Api& api) {
std::string data;
switch (source.specifier_case()) {
case envoy::config::core::v3::DataSource::SpecifierCase::kFilename:
data = api.fileSystem().fileReadToEnd(source.filename());
break;
case envoy::config::core::v3::DataSource::SpecifierCase::kInlineBytes:
data = source.inline_bytes();
break;
case envoy::config::core::v3::DataSource::SpecifierCase::kInlineString:
data = source.inline_string();
break;
case envoy::config::core::v3::DataSource::SpecifierCase::kEnvironmentVariable: {
const char* environment_variable = std::getenv(source.environment_variable().c_str());
if (environment_variable == nullptr) {
throw EnvoyException(
fmt::format("Environment variable doesn't exist: {}", source.environment_variable()));
}
data = environment_variable;
break;
}
default:
if (!allow_empty) {
throw EnvoyException(
fmt::format("Unexpected DataSource::specifier_case(): {}", source.specifier_case()));
}
}
if (!allow_empty && data.empty()) {
throw EnvoyException("DataSource cannot be empty");
}
return data;
}
absl::optional<std::string> getPath(const envoy::config::core::v3::DataSource& source) {
return source.specifier_case() == envoy::config::core::v3::DataSource::SpecifierCase::kFilename
? absl::make_optional(source.filename())
: absl::nullopt;
}
RemoteAsyncDataProvider::RemoteAsyncDataProvider(
Upstream::ClusterManager& cm, Init::Manager& manager,
const envoy::config::core::v3::RemoteDataSource& source, Event::Dispatcher& dispatcher,
Random::RandomGenerator& random, bool allow_empty, AsyncDataSourceCb&& callback)
: allow_empty_(allow_empty), callback_(std::move(callback)),
fetcher_(std::make_unique<Config::DataFetcher::RemoteDataFetcher>(cm, source.http_uri(),
source.sha256(), *this)),
init_target_("RemoteAsyncDataProvider", [this]() { start(); }),
retries_remaining_(
PROTOBUF_GET_WRAPPED_OR_DEFAULT(source.retry_policy(), num_retries, RetryCount)) {
uint64_t base_interval_ms = RetryInitialDelayMilliseconds;
uint64_t max_interval_ms = RetryMaxDelayMilliseconds;
if (source.has_retry_policy()) {
if (source.retry_policy().has_retry_back_off()) {
base_interval_ms =
PROTOBUF_GET_MS_REQUIRED(source.retry_policy().retry_back_off(), base_interval);
max_interval_ms = PROTOBUF_GET_MS_OR_DEFAULT(source.retry_policy().retry_back_off(),
max_interval, base_interval_ms * 10);
if (max_interval_ms < base_interval_ms) {
throw EnvoyException("max_interval must be greater than or equal to the base_interval");
}
}
}
backoff_strategy_ = std::make_unique<JitteredExponentialBackOffStrategy>(base_interval_ms,
max_interval_ms, random);
retry_timer_ = dispatcher.createTimer([this]() -> void { start(); });
manager.add(init_target_);
}
} // namespace DataSource
} // namespace Config
} // namespace Envoy