Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bf945b3
refactor: add Apply method to MemoryStore for transactional change sets
beekld Mar 27, 2026
cc59635
fix an unneeded copy, and warn on missing enum case
beekld Mar 27, 2026
82d09d2
add an ApplyResult with the changed keys
beekld Mar 27, 2026
259d8e9
mark the apply result as nodiscard for now
beekld Mar 27, 2026
f5b82bf
simplify tests
beekld Mar 27, 2026
7826357
simplify, since FDv2 doesnt require version checking in memory store
beekld Mar 30, 2026
651a780
refactor: define new Synchronizer and Initializer interfaces for FDv2
beekld Mar 31, 2026
fe37f55
move selector source into Next
beekld Apr 2, 2026
c470763
Merge branch 'main' into beeklimt/SDK-2096
beekld Apr 2, 2026
ed22857
distinguish between timeouts and errors
beekld Apr 2, 2026
f0013da
refactor: implement fdv2 polling initializer / synchronizer
beekld Apr 2, 2026
29a94c8
update for upstream change
beekld Apr 2, 2026
9e75b47
Merge branch 'main' into beeklimt/SDK-2096
beekld Apr 3, 2026
c8e1c7f
Merge branch 'beeklimt/SDK-2096' into beeklimt/SDK-2097
beekld Apr 3, 2026
0996cca
refactor: fix an error type that could be better
beekld Apr 3, 2026
92489a6
refactor polling synchronizer to make it more threadsafe
beekld Apr 7, 2026
071a4eb
update asio_requester to use new style
beekld Apr 7, 2026
f1b3f06
add comments
beekld Apr 7, 2026
37fb4c0
fix missing kInactive handling
beekld Apr 7, 2026
07810cd
refactor error types to preserve more info
beekld Apr 7, 2026
55e4d8c
refactor shared code into a separate file
beekld Apr 7, 2026
b8a334c
refactor FDv2ProtocolHandler::HandleEvent to be clearer
beekld Apr 7, 2026
6e5b1d4
clang format
beekld Apr 7, 2026
1e2d438
handle closed first in network response
beekld Apr 7, 2026
01cc26b
handle url parsing failure
beekld Apr 7, 2026
7a60c2a
minor cleanup and add tests
beekld Apr 7, 2026
c8e736f
clang-format
beekld Apr 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions libs/internal/include/launchdarkly/fdv2_protocol_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#pragma once

#include <launchdarkly/data_model/fdv2_change.hpp>
#include <launchdarkly/serialization/json_fdv2_events.hpp>

#include <boost/json/value.hpp>

#include <string_view>
#include <variant>
#include <vector>

namespace launchdarkly {

/**
* Protocol state machine for the FDv2 wire format.
*
* Accumulates put-object and delete-object events between a server-intent
* and payload-transferred event, then emits a complete FDv2ChangeSet.
*
* Shared between the polling and streaming synchronizers.
*/
class FDv2ProtocolHandler {
public:
/**
* Typed error returned by HandleEvent. Carries the original underlying
* error context rather than converting to a plain string.
*/
struct Error {
enum class Kind {
kJsonError, // Failed to deserialise an event's data field.
kProtocolError, // Out-of-order or unexpected event.
kServerError, // Server sent a valid 'error' event.
};

Kind kind;
std::string message;

/**
* Set for kJsonError when the tl::expected parse returned an error.
* Nullopt when parse succeeded but the data value was null.
*/
std::optional<JsonError> json_error;

/**
* Set for kServerError: the full wire error including id and reason.
*/
std::optional<FDv2Error> server_error;

/** JSON deserialisation failed — carries the original JsonError. */
static Error JsonParseError(JsonError err, std::string msg) {
return {Kind::kJsonError, std::move(msg), err, std::nullopt};
}
/** Parse succeeded but data was null — no underlying JsonError. */
static Error JsonParseError(std::string msg) {
return {Kind::kJsonError, std::move(msg), std::nullopt,
std::nullopt};
}
/** Out-of-order or unexpected protocol event. */
static Error ProtocolError(std::string msg) {
return {Kind::kProtocolError, std::move(msg), std::nullopt,
std::nullopt};
}
/** Server sent a well-formed 'error' event. */
static Error ServerError(FDv2Error err) {
return {Kind::kServerError, err.reason, std::nullopt,
std::move(err)};
}
};

/**
* Result of handling a single FDv2 event:
* - monostate: no output yet (accumulating, heartbeat, or unknown event)
* - FDv2ChangeSet: complete changeset ready to apply
* - Error: protocol error (JSON parse failure, protocol violation, or
* server-sent error event)
* - Goodbye: server is closing; caller should rotate sources
*/
using Result =
std::variant<std::monostate, data_model::FDv2ChangeSet, Error, Goodbye>;

/**
* Process one FDv2 event.
*
* @param event_type The event type string (e.g. "server-intent",
* "put-object", "payload-transferred").
* @param data The parsed JSON value for the event's data field.
* @return A Result indicating what (if anything) the caller
* should act on.
*/
Result HandleEvent(std::string_view event_type,
boost::json::value const& data);

/**
* Reset accumulated state. Call on reconnect before processing new events.
*/
void Reset();

FDv2ProtocolHandler() = default;

private:
enum class State { kInactive, kFull, kPartial };

Result HandleServerIntent(boost::json::value const& data);
Result HandlePutObject(boost::json::value const& data);
Result HandleDeleteObject(boost::json::value const& data);
Result HandlePayloadTransferred(boost::json::value const& data);
Result HandleError(boost::json::value const& data);
Result HandleGoodbye(boost::json::value const& data);

State state_ = State::kInactive;
std::vector<data_model::FDv2Change> changes_;
};

} // namespace launchdarkly
28 changes: 11 additions & 17 deletions libs/internal/include/launchdarkly/network/asio_requester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,22 +283,16 @@ class AsioRequester {

template <typename CompletionToken>
auto Request(HttpRequest request, CompletionToken&& token) {
// TODO: Clang-tidy wants to pass the request by reference, but I am not
// confident that lifetime would make sense.

namespace asio = boost::asio;
namespace system = boost::system;

using Sig = void(HttpResult result);
using Result = asio::async_result<std::decay_t<CompletionToken>, Sig>;
using Handler = typename Result::completion_handler_type;

Handler handler(std::forward<decltype(token)>(token));
Result result(handler);

InnerRequest(net::make_strand(ctx_), request, std::move(handler), 0);

return result.get();
return boost::asio::async_initiate<CompletionToken, void(HttpResult)>(
[this](auto handler, HttpRequest req) {
InnerRequest(
net::make_strand(ctx_), std::move(req),
[h = std::move(handler)](HttpResult result) mutable {
std::move(h)(std::move(result));
},
0);
},
token, std::move(request));
}

private:
Expand Down Expand Up @@ -336,7 +330,7 @@ class AsioRequester {
redirect_count]() mutable {
auto beast_request = MakeBeastRequest(*request);

const auto& properties = request->Properties();
auto const& properties = request->Properties();

std::string service =
request->Port().value_or(request->Https() ? "https" : "http");
Expand Down
1 change: 1 addition & 0 deletions libs/internal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(INTERNAL_SOURCES
serialization/value_mapping.cpp
serialization/json_evaluation_result.cpp
serialization/json_fdv2_events.cpp
fdv2_protocol_handler.cpp
serialization/json_sdk_data_set.cpp
serialization/json_segment.cpp
serialization/json_primitives.cpp
Expand Down
Loading
Loading