forked from qdrvm/kagome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterval.hpp
More file actions
51 lines (44 loc) · 1.35 KB
/
interval.hpp
File metadata and controls
51 lines (44 loc) · 1.35 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
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef KAGOME_AUTHORITY_DISCOVERY_INTERVAL_HPP
#define KAGOME_AUTHORITY_DISCOVERY_INTERVAL_HPP
#include <libp2p/basic/scheduler.hpp>
namespace kagome::authority_discovery {
/**
* Exponentially increasing interval
*
* Doubles interval duration on each tick until the configured maximum is
* reached.
*/
class ExpIncInterval {
public:
ExpIncInterval(std::chrono::milliseconds initial,
std::chrono::milliseconds max,
std::shared_ptr<libp2p::basic::Scheduler> scheduler)
: delay_{initial}, max_{max}, scheduler_{scheduler} {}
void start(std::function<void()> cb) {
BOOST_ASSERT(not cb_);
BOOST_ASSERT(cb);
cb_ = std::move(cb);
step();
}
private:
void step() {
handle_ = scheduler_->scheduleWithHandle(
[this] {
cb_();
delay_ = std::min(delay_ * 2, max_);
step();
},
delay_);
}
std::chrono::milliseconds delay_;
std::chrono::milliseconds max_;
std::shared_ptr<libp2p::basic::Scheduler> scheduler_;
std::function<void()> cb_;
libp2p::basic::Scheduler::Handle handle_;
};
} // namespace kagome::authority_discovery
#endif // KAGOME_AUTHORITY_DISCOVERY_INTERVAL_HPP