nx_metadata_sdk 1.0 Metadata SDK

random_pauser.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #pragma once
4
5 #include <chrono>
6
7 namespace nx {
8 namespace vms_server_plugins {
9 namespace analytics {
10 namespace stub {
11
12 class RandomPauser
13 {
14 public:
15 RandomPauser(
16 const std::chrono::milliseconds& pauseDuration = std::chrono::seconds(1),
17 int maxNumberOfPauses = 3,
18 int randModulus = 9)
19 :
20 m_duration(pauseDuration),
21 m_maxNumberOfPauses(maxNumberOfPauses),
22 m_randModulus(randModulus)
23 {
24 }
25
26 void update()
27 {
28 if (m_paused && std::chrono::high_resolution_clock::now() - m_pauseStartTime < m_duration)
29 return;
30
31 if (m_paused)
32 {
33 --m_maxNumberOfPauses;
34 m_pauseStartTime = std::chrono::high_resolution_clock::time_point();
35 m_resuming = true;
36 }
37 else if(m_resuming)
38 {
39 m_resuming = false;
40 }
41
42 m_paused = m_maxNumberOfPauses > 0 && rand() % m_randModulus == 0;
43
44 if (m_paused)
45 m_pauseStartTime = std::chrono::high_resolution_clock::now();
46 }
47
48 bool paused() const
49 {
50 return m_paused;
51 }
52
53 bool resuming() const
54 {
55 return m_resuming;
56 }
57
58 private:
59 const std::chrono::milliseconds m_duration;
60 int m_maxNumberOfPauses;
61 const int m_randModulus;
62 bool m_paused = false;
63 std::chrono::high_resolution_clock::time_point m_pauseStartTime;
64 bool m_resuming = false;
65 };
66
67 } // namespace stub
68 } // namespace analytics
69 } // namespace vms_server_plugins
70 } // namespace nx
71
nx
Definition: apple_utils.h:6
nx::vms_server_plugins::analytics::stub::RandomPauser
Definition: random_pauser.h:12