-
Notifications
You must be signed in to change notification settings - Fork 4
refactor: implement fdv2 polling initializer / synchronizer #519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
bf945b3
refactor: add Apply method to MemoryStore for transactional change sets
beekld cc59635
fix an unneeded copy, and warn on missing enum case
beekld 82d09d2
add an ApplyResult with the changed keys
beekld 259d8e9
mark the apply result as nodiscard for now
beekld f5b82bf
simplify tests
beekld 7826357
simplify, since FDv2 doesnt require version checking in memory store
beekld 651a780
refactor: define new Synchronizer and Initializer interfaces for FDv2
beekld fe37f55
move selector source into Next
beekld c470763
Merge branch 'main' into beeklimt/SDK-2096
beekld ed22857
distinguish between timeouts and errors
beekld f0013da
refactor: implement fdv2 polling initializer / synchronizer
beekld 29a94c8
update for upstream change
beekld 9e75b47
Merge branch 'main' into beeklimt/SDK-2096
beekld c8e1c7f
Merge branch 'beeklimt/SDK-2096' into beeklimt/SDK-2097
beekld 0996cca
refactor: fix an error type that could be better
beekld 92489a6
refactor polling synchronizer to make it more threadsafe
beekld 071a4eb
update asio_requester to use new style
beekld f1b3f06
add comments
beekld 37fb4c0
fix missing kInactive handling
beekld 07810cd
refactor error types to preserve more info
beekld 55e4d8c
refactor shared code into a separate file
beekld b8a334c
refactor FDv2ProtocolHandler::HandleEvent to be clearer
beekld 6e5b1d4
clang format
beekld 1e2d438
handle closed first in network response
beekld 01cc26b
handle url parsing failure
beekld 7a60c2a
minor cleanup and add tests
beekld c8e736f
clang-format
beekld 19da904
Merge branch 'main' into beeklimt/SDK-2097
beekld 48989a4
rewrite everything to use promises.
beekld 45befda
fix: make it safe to delete the synchronizer at any time
beekld cd0914d
refactor: reorder and clean up code
beekld 9d08671
docs: fix some comments
beekld 20e3f96
refactor: some simple optimizations
beekld d37dc5d
refactor: more cleanup
beekld 3378ed1
fix: close the synchronizer on destruction
beekld 66e3936
fix: minor feedback fix
beekld e9afed0
docs: fix some comments
beekld 112c78e
refactor: use generic http requester interface
beekld 4b02b67
refactor: break up HandleFDv2PollResponse into separate functions
beekld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #pragma once | ||
|
|
||
| #include <launchdarkly/async/promise.hpp> | ||
|
|
||
| #include <boost/asio/any_io_executor.hpp> | ||
| #include <boost/asio/error.hpp> | ||
| #include <boost/asio/steady_timer.hpp> | ||
| #include <boost/system/error_code.hpp> | ||
|
|
||
| #include <chrono> | ||
| #include <memory> | ||
|
|
||
| namespace launchdarkly::async { | ||
|
|
||
| // Returns a Future<bool> that resolves once the given duration elapses. | ||
| // The future resolves with true if the timer fired normally, or false if | ||
| // the timer was cancelled before it expired. | ||
| template <typename Rep, typename Period> | ||
| Future<bool> Delay(boost::asio::any_io_executor executor, | ||
| std::chrono::duration<Rep, Period> duration) { | ||
| auto timer = std::make_shared<boost::asio::steady_timer>(executor); | ||
| timer->expires_after(duration); | ||
| Promise<bool> promise; | ||
| auto future = promise.GetFuture(); | ||
| timer->async_wait([p = std::move(promise), | ||
| timer](boost::system::error_code code) mutable { | ||
| p.Resolve(code != boost::asio::error::operation_aborted); | ||
| }); | ||
| return future; | ||
| } | ||
|
|
||
| } // namespace launchdarkly::async |
114 changes: 114 additions & 0 deletions
114
libs/internal/include/launchdarkly/fdv2_protocol_handler.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.