Repository: envoyproxy/envoy
File: source/common/config/datasource.cc (C++)

Code before:
  // TODO(wbpcode): consider moving this check to read() to avoid duplicate checks.
  if (max_size > 0 && initial_data_or_error.value().length() > max_size) {
    return absl::InvalidArgumentError(fmt::format("response body size is {} bytes; maximum is {}",
                                                  initial_data_or_error.value().length(),
                                                  max_size));
  }

  if (!source.has_watched_directory() ||
      source.specifier_case() != envoy::config::core::v3::DataSource::kFilename) {
    return std::unique_ptr<DataSourceProvider>(
        new DataSourceProvider(std::move(initial_data_or_error).value()));
  }

  auto slot = ThreadLocal::TypedSlot<DynamicData::ThreadLocalData>::makeUnique(tls);
  slot->set([initial_data = std::make_shared<std::string>(
                 std::move(initial_data_or_error.value()))](Event::Dispatcher&) {
    return std::make_shared<DynamicData::ThreadLocalData>(initial_data);
  });

  const auto& filename = source.filename();
  auto directory_watcher_or_error =
      WatchedDirectory::create(source.watched_directory(), main_dispatcher);
  RETURN_IF_NOT_OK_REF(directory_watcher_or_error.status());

  auto& directory_watcher = directory_watcher_or_error.value();
  directory_watcher->setCallback(
      [slot_ptr = slot.get(), &api, filename, allow_empty, max_size]() -> absl::Status {
        auto new_data_or_error = readFile(filename, api, allow_empty, max_size);
        if (!new_data_or_error.ok()) {
          // Log an error but don't fail the watch to avoid throwing EnvoyException at runtime.
          ENVOY_LOG_TO_LOGGER(Logger::Registry::getLog(Logger::Id::config), error,
                              "Failed to read file: {}", new_data_or_error.status().message());
          return absl::OkStatus();
        }
        slot_ptr->runOnAllThreads(
            [new_data = std::make_shared<std::string>(std::move(new_data_or_error.value()))](
                OptRef<DynamicData::ThreadLocalData> obj) {
              if (obj.has_value()) {
                obj->data_ = new_data;
              }
            });
        return absl::OkStatus();
      });

  return std::unique_ptr<DataSourceProvider>(new DataSourceProvider(DynamicData(
      main_dispatcher, std::move(slot), std::move(directory_watcher_or_error.value()))));
}

} // namespace DataSource
} // namespace Config
} // namespace Envoy

Review comment (suggestion):
nit
```suggestion
  directory_watcher_or_error.value()->setCallback(
```

Code after:
  // TODO(wbpcode): consider moving this check to read() to avoid duplicate checks.
  if (max_size > 0 && initial_data_or_error.value().length() > max_size) {
    return absl::InvalidArgumentError(fmt::format("response body size is {} bytes; maximum is {}",
                                                  initial_data_or_error.value().length(),
                                                  max_size));
  }

  if (!source.has_watched_directory() ||
      source.specifier_case() != envoy::config::core::v3::DataSource::kFilename) {
    return std::unique_ptr<DataSourceProvider>(
        new DataSourceProvider(std::move(initial_data_or_error).value()));
  }

  auto slot = ThreadLocal::TypedSlot<DynamicData::ThreadLocalData>::makeUnique(tls);
  slot->set([initial_data = std::make_shared<std::string>(
                 std::move(initial_data_or_error.value()))](Event::Dispatcher&) {
    return std::make_shared<DynamicData::ThreadLocalData>(initial_data);
  });

  const auto& filename = source.filename();
  auto directory_watcher_or_error =
      WatchedDirectory::create(source.watched_directory(), main_dispatcher);
  RETURN_IF_NOT_OK_REF(directory_watcher_or_error.status());

  directory_watcher_or_error.value()->setCallback(
      [slot_ptr = slot.get(), &api, filename, allow_empty, max_size]() -> absl::Status {
        auto new_data_or_error = readFile(filename, api, allow_empty, max_size);
        if (!new_data_or_error.ok()) {
          // Log an error but don't fail the watch to avoid throwing EnvoyException at runtime.
          ENVOY_LOG_TO_LOGGER(Logger::Registry::getLog(Logger::Id::config), error,
                              "Failed to read file: {}", new_data_or_error.status().message());
          return absl::OkStatus();
        }
        slot_ptr->runOnAllThreads(
            [new_data = std::make_shared<std::string>(std::move(new_data_or_error.value()))](
                OptRef<DynamicData::ThreadLocalData> obj) {
              if (obj.has_value()) {
                obj->data_ = new_data;
              }
            });
        return absl::OkStatus();
      });

  return std::unique_ptr<DataSourceProvider>(new DataSourceProvider(DynamicData(
      main_dispatcher, std::move(slot), std::move(directory_watcher_or_error.value()))));
}

} // namespace DataSource
} // namespace Config
} // namespace Envoy