-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_bus.hpp
More file actions
62 lines (51 loc) · 2.13 KB
/
Copy pathmessage_bus.hpp
File metadata and controls
62 lines (51 loc) · 2.13 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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.
#pragma once
#include <functional>
#include <vector>
#include "datadog/impl/core/feature_message.hpp"
#include "datadog/impl/core/queue.hpp"
#include "datadog/impl/core/util/diagnostics.hpp"
namespace datadog::impl {
/**
* Holds the shared state for the messaging subsystem: a thread-safe queue and the
* fixed list of subscriber callbacks. The background thread that drains the queue and
* invokes the handlers lives outside this class (see `MessagingThreadMain`), following
* the same ownership pattern used by `context_thread`, `storage_thread`, and
* `upload_thread`.
*
* Handlers are supplied at construction time and are never modified afterward, so
* `MessagingThreadMain` can iterate `_handlers` without any synchronization.
*
* `Send()` enqueues a message non-blocking from any thread and returns false if the
* queue has been stopped. Call `Stop()` to signal shutdown; the messaging thread will
* drain any remaining messages before exiting.
*/
class MessageBus {
public:
explicit MessageBus(std::vector<std::function<void(const FeatureMessage&)>> handlers);
~MessageBus() = default;
MessageBus(const MessageBus&) = delete;
MessageBus& operator=(const MessageBus&) = delete;
MessageBus(MessageBus&&) = delete;
MessageBus& operator=(MessageBus&&) = delete;
/**
* Enqueues `msg` for delivery to all handlers. Non-blocking. Returns false if the
* queue has been stopped and the message was therefore dropped.
*/
bool Send(FeatureMessage msg);
/**
* Signals the messaging thread to drain remaining messages and exit. Must be called
* before the bus is destroyed; the caller must join the messaging thread after this
* returns.
*/
void Stop();
private:
friend void MessagingThreadMain(const DiagnosticLogger&, MessageBus&);
Queue<FeatureMessage> _queue;
std::vector<std::function<void(const FeatureMessage&)>> _handlers;
};
} // namespace datadog::impl