-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add the ability to persist and restore flag configuration. #93
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
33 commits
Select commit
Hold shift + click to select a range
1e0748c
feat: Add the ability to persist and restore flag configuration.
kinyoklion 667bed4
Flags can be exported.
kinyoklion 6c7c005
Progress
kinyoklion bec8466
Start getting things inplace.
kinyoklion cd617a1
Hashing.
kinyoklion c34b462
Untested functionality.
kinyoklion 28603ce
Move context index.
kinyoklion a7ad607
Move encoding to internal
kinyoklion 515b705
Add context index tests.
kinyoklion 466e6dc
Cleanup.
kinyoklion bf62cb3
Persistence tests.
kinyoklion eff6d70
Add logger.
kinyoklion 0e7001a
Add config.
kinyoklion 905402c
Working sample
kinyoklion 2d98903
Add extra blank line.
kinyoklion 821c698
Add blank line.
kinyoklion 4e6f290
Add blank line.
kinyoklion eccee36
Tidy import
kinyoklion ac2f149
Format code.
kinyoklion 8903b70
Change log message to info.
kinyoklion 4a9ea37
Add eviction test.
kinyoklion 89e5540
Merge branch 'main' into rlamb/sc-202753/persistence-interface
kinyoklion 544de96
Persistence working with identify.
kinyoklion 8a99a0f
Include header for size_t.
kinyoklion d652ad5
Construction order.
kinyoklion fb4c818
Use base64 encoded sha256.
kinyoklion 64abd62
Support new builder.
kinyoklion 62caf41
C bindings for persistence.
kinyoklion 34fbe2d
Remove Value from names.
kinyoklion 28dbfbd
Try data instead of begin for windows.
kinyoklion f1e87cd
PR feedback.
kinyoklion 7b0e47e
Merge branch 'main' into rlamb/sc-202753/persistence-interface
kinyoklion acdfb21
Fix C errors.
kinyoklion 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 |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
|
|
||
| #include <launchdarkly/context_builder.hpp> | ||
|
|
||
| #include <filesystem> | ||
| #include <fstream> | ||
| #include <iostream> | ||
|
|
||
| namespace net = boost::asio; // from <boost/asio.hpp> | ||
|
|
@@ -13,8 +15,56 @@ using launchdarkly::LogLevel; | |
| using launchdarkly::client_side::Client; | ||
| using launchdarkly::client_side::ConfigBuilder; | ||
| using launchdarkly::client_side::DataSourceBuilder; | ||
| using launchdarkly::client_side::PersistenceBuilder; | ||
| using launchdarkly::config::shared::builders::LoggingBuilder; | ||
|
|
||
| class FilePersistence : public IPersistence { | ||
| public: | ||
| FilePersistence(std::string directory) : directory_(std::move(directory)) { | ||
| std::filesystem::create_directories(directory_); | ||
| } | ||
| void Set(std::string storage_namespace, | ||
| std::string key, | ||
| std::string data) noexcept override { | ||
| try { | ||
| std::ofstream file; | ||
| file.open(MakePath(storage_namespace, key)); | ||
| file << data; | ||
| file.close(); | ||
| } catch (...) { | ||
| std::cout << "Problem writing" << std::endl; | ||
| } | ||
| } | ||
|
|
||
| void Remove(std::string storage_namespace, | ||
| std::string key) noexcept override { | ||
| std::filesystem::remove(MakePath(storage_namespace, key)); | ||
| } | ||
|
|
||
| std::optional<std::string> Read(std::string storage_namespace, | ||
| std::string key) noexcept override { | ||
| auto path = MakePath(storage_namespace, key); | ||
|
|
||
| try { | ||
| if (std::filesystem::exists(path)) { | ||
| std::ifstream file(path); | ||
| std::stringstream buffer; | ||
| buffer << file.rdbuf(); | ||
| return buffer.str(); | ||
| } | ||
| } catch (...) { | ||
| std::cout << "Problem reading" << std::endl; | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| private: | ||
| std::string MakePath(std::string storage_namespace, std::string key) { | ||
| return directory_ + "/" + storage_namespace + "_" + key; | ||
| } | ||
| std::string directory_; | ||
| }; | ||
|
|
||
| int main() { | ||
| net::io_context ioc; | ||
|
|
||
|
|
@@ -39,6 +89,8 @@ int main() { | |
| config_builder.Logging().Logging( | ||
| LoggingBuilder::BasicLogging().Level(LogLevel::kDebug)); | ||
| config_builder.Events().FlushInterval(std::chrono::seconds(5)); | ||
| config_builder.Persistence().Custom( | ||
| std::make_shared<FilePersistence>("ld_persist")); | ||
|
|
||
| auto config = config_builder.Build(); | ||
| if (!config) { | ||
|
|
@@ -49,6 +101,11 @@ int main() { | |
| Client client(std::move(*config), | ||
| ContextBuilder().kind("user", "ryan").build()); | ||
|
|
||
| auto before_init = client.BoolVariationDetail("my-boolean-flag", false); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read a flag before init is complete. Result can be different if the value was persisted. |
||
| // This should be the cached version from our persistence, if the | ||
| // persistence is populated. | ||
| std::cout << "Before Init Complete: " << *before_init << std::endl; | ||
|
|
||
| std::cout << "Initial Status: " << client.DataSourceStatus().Status() | ||
| << std::endl; | ||
|
|
||
|
|
@@ -68,6 +125,27 @@ int main() { | |
| std::cout << "Reason was: " << *reason << std::endl; | ||
| } | ||
|
|
||
| // Identify a series of contexts. | ||
| for (auto context_index = 0; context_index < 4; context_index++) { | ||
| std::cout << "Identifying user: " | ||
| << "ryan" << context_index << std::endl; | ||
| auto future = client.IdentifyAsync( | ||
| ContextBuilder() | ||
| .kind("user", "ryan" + std::to_string(context_index)) | ||
| .build()); | ||
| auto before_ident = | ||
| client.BoolVariationDetail("my-boolean-flag", false); | ||
| future.get(); | ||
| auto after_ident = client.BoolVariationDetail("my-boolean-flag", false); | ||
|
|
||
| std::cout << "For: " | ||
| << "ryan" << context_index << ": " | ||
| << "Before ident complete: " << *before_init | ||
| << " After: " << *after_ident << std::endl; | ||
|
|
||
| sleep(1); | ||
| } | ||
|
|
||
| // Sit around. | ||
| std::cout << "Press enter to exit" << std::endl << std::endl; | ||
| std::cin.get(); | ||
|
|
||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple persistence implementation for demonstration purposes.