Skip to content

Commit d822e8a

Browse files
authored
watched: removing exceptions (envoyproxy#37461)
Risk Level: low Testing: updated tests Docs Changes: n/a Release Notes: n/a envoyproxy/envoy-mobile#176 Signed-off-by: Alyssa Wilk <[email protected]>
1 parent 9494a9d commit d822e8a

File tree

6 files changed

+34
-16
lines changed

6 files changed

+34
-16
lines changed

source/common/config/watched_directory.cc

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@
33
namespace Envoy {
44
namespace Config {
55

6+
absl::StatusOr<std::unique_ptr<WatchedDirectory>>
7+
WatchedDirectory::create(const envoy::config::core::v3::WatchedDirectory& config,
8+
Event::Dispatcher& dispatcher) {
9+
absl::Status creation_status = absl::OkStatus();
10+
auto ret =
11+
std::unique_ptr<WatchedDirectory>(new WatchedDirectory(config, dispatcher, creation_status));
12+
RETURN_IF_NOT_OK_REF(creation_status);
13+
return ret;
14+
}
15+
616
WatchedDirectory::WatchedDirectory(const envoy::config::core::v3::WatchedDirectory& config,
7-
Event::Dispatcher& dispatcher) {
17+
Event::Dispatcher& dispatcher, absl::Status& creation_status) {
818
watcher_ = dispatcher.createFilesystemWatcher();
9-
THROW_IF_NOT_OK(watcher_->addWatch(absl::StrCat(config.path(), "/"),
10-
Filesystem::Watcher::Events::MovedTo,
11-
[this](uint32_t) { return cb_(); }));
19+
SET_AND_RETURN_IF_NOT_OK(watcher_->addWatch(absl::StrCat(config.path(), "/"),
20+
Filesystem::Watcher::Events::MovedTo,
21+
[this](uint32_t) { return cb_(); }),
22+
creation_status);
1223
}
1324

1425
} // namespace Config

source/common/config/watched_directory.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ namespace Config {
1010
// Implement the common functionality of envoy::config::core::v3::WatchedDirectory.
1111
class WatchedDirectory {
1212
public:
13-
using Callback = std::function<absl::Status()>;
13+
static absl::StatusOr<std::unique_ptr<WatchedDirectory>>
14+
create(const envoy::config::core::v3::WatchedDirectory& config, Event::Dispatcher& dispatcher);
1415

15-
WatchedDirectory(const envoy::config::core::v3::WatchedDirectory& config,
16-
Event::Dispatcher& dispatcher);
16+
using Callback = std::function<absl::Status()>;
1717

1818
void setCallback(Callback cb) { cb_ = cb; }
1919

20+
protected:
21+
WatchedDirectory(const envoy::config::core::v3::WatchedDirectory& config,
22+
Event::Dispatcher& dispatcher, absl::Status& creation_status);
23+
2024
private:
2125
std::unique_ptr<Filesystem::Watcher> watcher_;
2226
Callback cb_;

source/common/secret/sds_api.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,9 @@ void TlsCertificateSdsApi::setSecret(
270270
secret.tls_certificate());
271271
resolved_tls_certificate_secrets_ = nullptr;
272272
if (secret.tls_certificate().has_watched_directory()) {
273-
watched_directory_ = std::make_unique<Config::WatchedDirectory>(
274-
secret.tls_certificate().watched_directory(), dispatcher_);
273+
watched_directory_ = THROW_OR_RETURN_VALUE(
274+
Config::WatchedDirectory::create(secret.tls_certificate().watched_directory(), dispatcher_),
275+
std::unique_ptr<Config::WatchedDirectory>);
275276
} else {
276277
watched_directory_.reset();
277278
}
@@ -324,8 +325,10 @@ void CertificateValidationContextSdsApi::setSecret(
324325
secret.validation_context());
325326
resolved_certificate_validation_context_secrets_ = nullptr;
326327
if (secret.validation_context().has_watched_directory()) {
327-
watched_directory_ = std::make_unique<Config::WatchedDirectory>(
328-
secret.validation_context().watched_directory(), dispatcher_);
328+
watched_directory_ =
329+
THROW_OR_RETURN_VALUE(Config::WatchedDirectory::create(
330+
secret.validation_context().watched_directory(), dispatcher_),
331+
std::unique_ptr<Config::WatchedDirectory>);
329332
} else {
330333
watched_directory_.reset();
331334
}

source/extensions/config_subscription/filesystem/filesystem_subscription_impl.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ FilesystemSubscriptionImpl::FilesystemSubscriptionImpl(
3535
return absl::OkStatus();
3636
}));
3737
} else {
38-
directory_watcher_ =
39-
std::make_unique<WatchedDirectory>(path_config_source.watched_directory(), dispatcher);
38+
directory_watcher_ = THROW_OR_RETURN_VALUE(
39+
WatchedDirectory::create(path_config_source.watched_directory(), dispatcher),
40+
std::unique_ptr<WatchedDirectory>);
4041
directory_watcher_->setCallback([this]() {
4142
if (started_) {
4243
refresh();

test/common/config/watched_directory_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ TEST(WatchedDirectory, All) {
2323
Filesystem::Watcher::OnChangedCb cb;
2424
EXPECT_CALL(*watcher, addWatch("foo/bar/", Filesystem::Watcher::Events::MovedTo, _))
2525
.WillOnce(DoAll(SaveArg<2>(&cb), Return(absl::OkStatus())));
26-
WatchedDirectory wd(config, dispatcher);
26+
auto wd = *WatchedDirectory::create(config, dispatcher);
2727
bool called = false;
28-
wd.setCallback([&called] {
28+
wd->setCallback([&called] {
2929
called = true;
3030
return absl::OkStatus();
3131
});

tools/code_format/config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ paths:
140140
- source/common/grpc/async_client_impl.cc
141141
- source/common/grpc/google_grpc_creds_impl.cc
142142
- source/common/local_reply/local_reply.cc
143-
- source/common/config/watched_directory.cc
144143
- source/server/drain_manager_impl.cc
145144
- source/common/router/rds_impl.cc
146145
- source/server/hot_restarting_parent.cc

0 commit comments

Comments
 (0)