Skip to content

Commit

Permalink
Improve sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
estringana committed Jul 16, 2024
1 parent 3053d70 commit 2b7101a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions appsec/src/helper/sampler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ class sampler {

std::optional<scope> get()
{
const std::lock_guard<std::mutex> lock_guard(mtx_);

std::optional<scope> result = std::nullopt;
if (sample_rate_ == 1) {
std::atomic<bool> concurrent;
return {scope{concurrent}};
}

const std::lock_guard<std::mutex> lock_guard(mtx_);

if (!concurrent_ && floor(request_ * sample_rate_) !=
floor((request_ + 1) * sample_rate_)) {
Expand Down
2 changes: 1 addition & 1 deletion appsec/tests/helper/sampler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ TEST(ScopeTest, TestConcurrent)

TEST(ScopeTest, TestItDoesNotPickTokenUntilScopeReleased)
{
sampler sampler(1);
sampler sampler(0.9999);
auto is_pick = sampler.get();
EXPECT_TRUE(is_pick != std::nullopt);
is_pick = sampler.get();
Expand Down

0 comments on commit 2b7101a

Please sign in to comment.