-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfdv2_protocol_handler.cpp
More file actions
220 lines (205 loc) · 7.85 KB
/
fdv2_protocol_handler.cpp
File metadata and controls
220 lines (205 loc) · 7.85 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <launchdarkly/fdv2_protocol_handler.hpp>
#include <launchdarkly/data_model/flag.hpp>
#include <launchdarkly/data_model/item_descriptor.hpp>
#include <launchdarkly/data_model/segment.hpp>
#include <launchdarkly/serialization/json_flag.hpp>
#include <launchdarkly/serialization/json_segment.hpp>
#include <boost/json.hpp>
#include <tl/expected.hpp>
namespace launchdarkly {
static char const* const kServerIntent = "server-intent";
static char const* const kPutObject = "put-object";
static char const* const kDeleteObject = "delete-object";
static char const* const kPayloadTransferred = "payload-transferred";
static char const* const kError = "error";
static char const* const kGoodbye = "goodbye";
// Returns the parsed FDv2Change on success, nullopt for unknown kinds (which
// should be silently skipped for forward-compatibility), or an error string if
// a known kind fails to deserialize.
static tl::expected<std::optional<data_model::FDv2Change>, std::string>
ParsePut(PutObject const& put) {
if (put.kind == "flag") {
auto result = boost::json::value_to<
tl::expected<std::optional<data_model::Flag>, JsonError>>(
put.object);
// One bad flag aborts the entire transfer so the store is never
// left in a partially-updated state.
if (!result) {
return tl::make_unexpected("could not deserialize flag '" +
put.key + "'");
}
if (!result->has_value()) {
return tl::make_unexpected("flag '" + put.key + "' object was null");
}
return data_model::FDv2Change{
put.key,
data_model::ItemDescriptor<data_model::Flag>{std::move(**result)}};
}
if (put.kind == "segment") {
auto result = boost::json::value_to<
tl::expected<std::optional<data_model::Segment>, JsonError>>(
put.object);
// One bad segment aborts the entire transfer so the store is never
// left in a partially-updated state.
if (!result) {
return tl::make_unexpected("could not deserialize segment '" +
put.key + "'");
}
if (!result->has_value()) {
return tl::make_unexpected("segment '" + put.key +
"' object was null");
}
return data_model::FDv2Change{
put.key,
data_model::ItemDescriptor<data_model::Segment>{
std::move(**result)}};
}
// Silently skip unknown kinds for forward-compatibility.
return std::nullopt;
}
static data_model::FDv2Change MakeDeleteChange(DeleteObject const& del) {
if (del.kind == "flag") {
return data_model::FDv2Change{
del.key,
data_model::ItemDescriptor<data_model::Flag>{
data_model::Tombstone{static_cast<uint64_t>(del.version)}}};
}
return data_model::FDv2Change{
del.key,
data_model::ItemDescriptor<data_model::Segment>{
data_model::Tombstone{static_cast<uint64_t>(del.version)}}};
}
FDv2ProtocolHandler::Result FDv2ProtocolHandler::HandleEvent(
std::string_view event_type,
boost::json::value const& data) {
if (event_type == kServerIntent) {
auto result = boost::json::value_to<
tl::expected<std::optional<ServerIntent>, JsonError>>(data);
if (!result) {
Reset();
return FDv2Error{std::nullopt, "could not deserialize server-intent"};
}
if (!result->has_value()) {
Reset();
return FDv2Error{std::nullopt, "server-intent data was null"};
}
auto const& intent = **result;
if (intent.payloads.empty()) {
return std::monostate{};
}
auto const& code = intent.payloads[0].intent_code;
changes_.clear();
if (code == IntentCode::kTransferFull) {
state_ = State::kFull;
} else if (code == IntentCode::kTransferChanges) {
state_ = State::kPartial;
} else {
// kNone or kUnknown: emit an empty changeset immediately.
state_ = State::kInactive;
return data_model::FDv2ChangeSet{data_model::FDv2ChangeSet::Type::kNone,
{},
data_model::Selector{}};
}
return std::monostate{};
}
if (event_type == kPutObject) {
if (state_ == State::kInactive) {
return std::monostate{};
}
auto result = boost::json::value_to<
tl::expected<std::optional<PutObject>, JsonError>>(data);
if (!result) {
Reset();
return FDv2Error{std::nullopt, "could not deserialize put-object"};
}
if (!result->has_value()) {
Reset();
return FDv2Error{std::nullopt, "put-object data was null"};
}
auto change = ParsePut(**result);
if (!change) {
Reset();
return FDv2Error{std::nullopt, std::move(change.error())};
}
if (*change) {
changes_.push_back(std::move(**change));
}
return std::monostate{};
}
if (event_type == kDeleteObject) {
if (state_ == State::kInactive) {
return std::monostate{};
}
auto result = boost::json::value_to<
tl::expected<std::optional<DeleteObject>, JsonError>>(data);
if (!result) {
Reset();
return FDv2Error{std::nullopt, "could not deserialize delete-object"};
}
if (!result->has_value()) {
Reset();
return FDv2Error{std::nullopt, "delete-object data was null"};
}
auto const& del = **result;
// Silently skip unknown kinds for forward-compatibility.
if (del.kind != "flag" && del.kind != "segment") {
return std::monostate{};
}
changes_.push_back(MakeDeleteChange(del));
return std::monostate{};
}
if (event_type == kPayloadTransferred) {
auto result = boost::json::value_to<
tl::expected<std::optional<PayloadTransferred>, JsonError>>(data);
if (!result) {
Reset();
return FDv2Error{std::nullopt,
"could not deserialize payload-transferred"};
}
if (!result->has_value()) {
Reset();
return FDv2Error{std::nullopt, "payload-transferred data was null"};
}
auto const& transferred = **result;
auto type = (state_ == State::kPartial)
? data_model::FDv2ChangeSet::Type::kPartial
: data_model::FDv2ChangeSet::Type::kFull;
data_model::FDv2ChangeSet changeset{
type,
std::move(changes_),
data_model::Selector{data_model::Selector::State{
transferred.version, transferred.state}}};
Reset();
return changeset;
}
if (event_type == kError) {
auto result = boost::json::value_to<
tl::expected<std::optional<FDv2Error>, JsonError>>(data);
Reset();
if (!result) {
return FDv2Error{std::nullopt, "could not deserialize error event"};
}
if (!result->has_value()) {
return FDv2Error{std::nullopt, "error event data was null"};
}
return **result;
}
if (event_type == kGoodbye) {
auto result = boost::json::value_to<
tl::expected<std::optional<Goodbye>, JsonError>>(data);
if (!result) {
return Goodbye{std::nullopt};
}
if (!result->has_value()) {
return Goodbye{std::nullopt};
}
return **result;
}
// heartbeat and unrecognized events: no-op.
return std::monostate{};
}
void FDv2ProtocolHandler::Reset() {
state_ = State::kInactive;
changes_.clear();
}
} // namespace launchdarkly