blob: 9365ef94368bf07d78cd79e98b0941532b9053c7 (
plain)
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
65
66
67
68
69
70
71
72
73
74
|
#pragma once
#include "orchestrator.hpp"
#include <kel/lbm/swarm/registry.hpp>
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <functional>
#include <mutex>
#include <string>
#include <thread>
namespace kel {
namespace lbm {
namespace swarm {
struct control_config {
uint64_t heartbeat_ms = 2000u;
registry::thresholds limits;
};
/**
* The control protocol on top of the transport.
*
* Kept apart from `orchestrator` so each can be reasoned about alone: the
* transport knows nothing about hellos, and this knows nothing about ICE.
*
* Owns the heartbeat thread. libdatachannel provides no timers, so liveness is
* a plain ticker -- which is fine, since the work it does per beat is a lock, a
* vector copy and some sends.
*/
class control {
public:
control(orchestrator &orch, control_config cfg);
~control();
control(const control &) = delete;
control &operator=(const control &) = delete;
/// Installs the handlers on the orchestrator. Call before listen().
void attach();
void start_heartbeat();
void stop_heartbeat();
registry &workers() { return registry_; }
/// Emitted for anything worth a line in the log.
void on_event(std::function<void(const std::string &)> handler);
private:
void handle_message(const std::shared_ptr<session> &sess,
const std::string &text);
void handle_hello(const std::shared_ptr<session> &sess,
const std::string &text);
void heartbeat_loop();
void emit(const std::string &line);
orchestrator &orch_;
control_config cfg_;
registry registry_;
std::thread ticker_;
std::mutex tick_mutex_;
std::condition_variable tick_cv_;
std::atomic<bool> running_{false};
std::function<void(const std::string &)> on_event_;
};
} // namespace swarm
} // namespace lbm
} // namespace kel
|