Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions olp-cpp-sdk-core/src/http/DefaultNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
namespace olp {
namespace http {
DefaultNetwork::DefaultNetwork(std::shared_ptr<Network> network)
: current_statistics_bucket_{0}, network_{std::move(network)} {}
: current_statistics_bucket_{0},
buckets_{std::make_shared<thread::Atomic<BucketsContainer>>()},
network_{std::move(network)} {}

DefaultNetwork::~DefaultNetwork() = default;

Expand All @@ -43,10 +45,13 @@ SendOutcome DefaultNetwork::Send(NetworkRequest request, Payload payload,
AppendDefaultHeaders(request_headers);
}

const auto buckets = buckets_;
const auto bucket_id = current_statistics_bucket_.load();

auto user_callback = [=](NetworkResponse response) {
LockStatistics(bucket_id, [&](Statistics& stats) {
auto user_callback = [buckets, bucket_id,
callback](NetworkResponse response) {
buckets->locked([&](BucketsContainer& container) {
auto& stats = container[bucket_id];
const auto status = response.GetStatus();
if (status < HttpStatusCode::OK ||
status >= HttpStatusCode::BAD_REQUEST) {
Expand Down Expand Up @@ -81,10 +86,9 @@ void DefaultNetwork::SetCurrentBucket(uint8_t bucket_id) {
}

DefaultNetwork::Statistics DefaultNetwork::GetStatistics(uint8_t bucket_id) {
Statistics result;
LockStatistics(bucket_id,
[&](Statistics& statistics) { result = statistics; });
return result;
return buckets_->locked([bucket_id](BucketsContainer& container) {
return container[bucket_id];
});
}

void DefaultNetwork::AppendUserAgent(Headers& request_headers) const {
Expand All @@ -110,11 +114,5 @@ void DefaultNetwork::AppendDefaultHeaders(Headers& request_headers) const {
default_headers_.end());
}

void DefaultNetwork::LockStatistics(uint8_t bucket_id,
std::function<void(Statistics&)> callback) {
buckets_.locked(
[&](BucketsContainer& container) { callback(container[bucket_id]); });
}

} // namespace http
} // namespace olp
5 changes: 1 addition & 4 deletions olp-cpp-sdk-core/src/http/DefaultNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,10 @@ class DefaultNetwork final : public Network {
void AppendUserAgent(Headers& request_headers) const;
void AppendDefaultHeaders(Headers& request_headers) const;

void LockStatistics(uint8_t bucket_id,
std::function<void(Statistics&)> callback);

std::atomic<uint8_t> current_statistics_bucket_;

using BucketsContainer = std::unordered_map<uint8_t, Statistics>;
thread::Atomic<BucketsContainer> buckets_;
std::shared_ptr<thread::Atomic<BucketsContainer>> buckets_;

std::mutex default_headers_mutex_;
Headers default_headers_;
Expand Down
Loading