File tree 2 files changed +28
-4
lines changed
2 files changed +28
-4
lines changed Original file line number Diff line number Diff line change 7
7
#include " rate_limit.hpp"
8
8
9
9
#include < chrono>
10
+ #include < iostream>
10
11
11
12
using std::chrono::duration_cast;
12
13
using std::chrono::microseconds;
@@ -32,25 +33,42 @@ bool rate_limiter::allow()
32
33
33
34
std::lock_guard<std::mutex> const lock (mtx_);
34
35
36
+ std::cout << " START\n ----------\n " ;
35
37
if (now_s != index_) {
38
+ std::cout << " New second\n " ;
36
39
if (index_ == now_s - 1 ) {
40
+ std::cout << " Following second\n " ;
37
41
precounter_ = counter_;
38
42
} else {
43
+ std::cout << " Not following second\n " ;
39
44
precounter_ = 0 ;
40
45
}
41
46
counter_ = 0 ;
42
47
index_ = now_s;
48
+ } else {
49
+ std::cout << " Same second\n " ;
43
50
}
44
51
52
+ std::cout << " max_per_second_ is " << max_per_second_ << std::endl;
53
+ ;
54
+ std::cout << " counter_ is " << counter_ << std::endl;
55
+ ;
56
+ std::cout << " precounter_ is " << precounter_ << std::endl;
57
+ ;
58
+ std::cout << " index_ is " << index_ << std::endl;
59
+ ;
60
+
45
61
constexpr uint64_t mil = 1000 ;
46
62
uint32_t const count =
47
63
(precounter_ * (mil - (now_ms % mil))) / mil + counter_;
48
64
49
65
if (count >= max_per_second_) {
66
+ std::cout << " Not allowed\n " ;
50
67
return false ;
51
68
}
52
69
53
70
counter_++;
71
+ std::cout << " Allowed\n " ;
54
72
55
73
return true ;
56
74
}
Original file line number Diff line number Diff line change @@ -47,27 +47,33 @@ class rate_limiter : public dds::rate_limiter {
47
47
48
48
TEST (RateLimitTest, OnlyAllowedMaxPerSecond)
49
49
{
50
+ auto first_round_time = system_clock::now ();
51
+ auto second_round_time = first_round_time + std::chrono::seconds (5 );
52
+
53
+ std::cout << " first_round_time is " << first_round_time.time_since_epoch ().count () << std::endl;
54
+ std::cout << " second_round_time is " << second_round_time.time_since_epoch ().count () << std::endl;
55
+
50
56
std::unique_ptr<mock::timer> timer = std::make_unique<mock::timer>();
51
57
// Four calls within the same second
52
- timer->responses .push (system_clock::duration ( 1708963615 ));
58
+ timer->responses .push (first_round_time. time_since_epoch ( ));
53
59
54
60
mock::rate_limiter rate_limiter (2 );
55
61
rate_limiter.set_timer (std::move (timer));
56
62
57
63
int allowed = 0 ;
58
- for (int i = 0 ; i < 100 ; i++) {
64
+ for (int i = 0 ; i < 10 ; i++) {
59
65
if (rate_limiter.allow ()) {
60
66
allowed++;
61
67
}
62
68
}
63
69
EXPECT_EQ (2 , allowed);
64
70
65
71
timer = std::make_unique<mock::timer>();
66
- timer->responses .push (system_clock::duration ( 1709963630 ));
72
+ timer->responses .push (second_round_time. time_since_epoch ( ));
67
73
rate_limiter.set_timer (std::move (timer));
68
74
69
75
allowed = 0 ;
70
- for (int i = 0 ; i < 100 ; i++) {
76
+ for (int i = 0 ; i < 10 ; i++) {
71
77
if (rate_limiter.allow ()) {
72
78
allowed++;
73
79
}
You can’t perform that action at this time.
0 commit comments