-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaky_bucket_algorithm.cpp
More file actions
64 lines (58 loc) · 1.71 KB
/
leaky_bucket_algorithm.cpp
File metadata and controls
64 lines (58 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
#include <chrono>
#include<windows.h>
using namespace std;
void init_code(){
#ifndef ONLINE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int main() {
init_code();
// Get the current time point
auto now1 = std::chrono::system_clock::now();
// Get the duration since the epoch
auto duration1 = now1.time_since_epoch();
// Convert the duration to milliseconds
auto millis1 = std::chrono::duration_cast<std::chrono::milliseconds>(duration1).count();
millis1*=0.001;
// Print the current time in milliseconds since the epoch
std::cout << "Current time in seconds since the epoch: " << millis1 << std::endl;
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(1);
s.push(2);
s.push(3);
Sleep(7000);
// Get the current time point
auto now2 = std::chrono::system_clock::now();
// Get the duration since the epoch
auto duration2 = now2.time_since_epoch();
// Convert the duration to milliseconds
auto millis2 = std::chrono::duration_cast<std::chrono::milliseconds>(duration2).count();
millis2*=0.001;
// Print the current time in milliseconds since the epoch
std::cout << "Current time in seconds since the epoch: " << millis2 << std::endl;
if(s.size()>5 && millis2-millis1<5){
cout<<"rate limit exceed. wait 5 seconds before sending next request";
while(s.size()>5)
s.pop();
}
else if(s.size()>5 && millis2-millis1>=5){
if(s.size()>=10)
cout<<"rate limit exceed. wait 5 seconds before sending next request";
while(s.size()>5)
s.pop();
}
return 0;
}