Skip to content

Commit

Permalink
fix(push): Lock access to collectables_
Browse files Browse the repository at this point in the history
  • Loading branch information
gjasny committed Nov 12, 2021
1 parent 882fec9 commit 7aaecba
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions push/include/prometheus/gateway.h
Expand Up @@ -3,6 +3,7 @@
#include <future>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -57,6 +58,7 @@ class PROMETHEUS_CPP_PUSH_EXPORT Gateway {
std::string jobUri_;
std::string labels_;
std::unique_ptr<detail::CurlWrapper> curlWrapper_;
std::mutex mutex_;

using CollectableEntry = std::pair<std::weak_ptr<Collectable>, std::string>;
std::vector<CollectableEntry> collectables_;
Expand Down
3 changes: 3 additions & 0 deletions push/src/gateway.cc
Expand Up @@ -55,6 +55,7 @@ void Gateway::RegisterCollectable(const std::weak_ptr<Collectable>& collectable,
}
}

std::lock_guard<std::mutex> lock{mutex_};
CleanupStalePointers(collectables_);
collectables_.push_back(std::make_pair(collectable, ss.str()));
}
Expand All @@ -73,6 +74,7 @@ int Gateway::PushAdd() { return push(detail::HttpMethod::Put); }
int Gateway::push(detail::HttpMethod method) {
const auto serializer = TextSerializer{};

std::lock_guard<std::mutex> lock{mutex_};
for (auto& wcollectable : collectables_) {
auto collectable = wcollectable.first.lock();
if (!collectable) {
Expand Down Expand Up @@ -104,6 +106,7 @@ std::future<int> Gateway::async_push(detail::HttpMethod method) {
const auto serializer = TextSerializer{};
std::vector<std::future<int>> futures;

std::lock_guard<std::mutex> lock{mutex_};
for (auto& wcollectable : collectables_) {
auto collectable = wcollectable.first.lock();
if (!collectable) {
Expand Down

0 comments on commit 7aaecba

Please sign in to comment.