Skip to content
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

Feature/bootstrapping #35

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:


- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DENABLE_TEST_COVERAGE=1 -DCMAKE_PREFIX_PATH=${{github.workspace}}/build
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DENABLE_TEST_COVERAGE=1 -DCMAKE_PREFIX_PATH=${{github.workspace}}/build -DUNLEASH_ENABLE_TESTING=ON

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:


- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DENABLE_TEST_COVERAGE=1 -G Ninja -DCMAKE_PREFIX_PATH=${{github.workspace}}/build
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DENABLE_TEST_COVERAGE=1 -G Ninja -DCMAKE_PREFIX_PATH=${{github.workspace}}/build -DUNLEASH_ENABLE_TESTING=ON

- name: Build with sonnar wrapper
run: build-wrapper-linux-x86-64 --out-dir ${{ env.BUILD_WRAPPER_OUT_DIR }} cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j10
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: conan install . --output-folder=build --build=missing --settings=build_type=${{env.BUILD_TYPE}}

- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_PREFIX_PATH=${{github.workspace}}/build
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_PREFIX_PATH=${{github.workspace}}/build -DUNLEASH_ENABLE_TESTING=ON

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j10
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "client-specification"]
path = client-specification
url = git@github.com:Unleash/client-specification.git
url = https://github.com/Unleash/client-specification.git
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if(NOT CMAKE_BUILD_TYPE)
RelWithDebInfo MinSizeRel." FORCE)
endif(NOT CMAKE_BUILD_TYPE)

option(ENABLE_TESTING "Enable Test Builds" ON)
option(UNLEASH_ENABLE_TESTING "Enable Test Builds" ON)
option(ENABLE_TEST_COVERAGE "Enable Test Coverage" OFF)

find_package(cpr)
Expand All @@ -30,7 +30,7 @@ endif()
# Create the main unleash library target
add_subdirectory(src)

if(ENABLE_TESTING)
if(UNLEASH_ENABLE_TESTING)
enable_testing()
add_subdirectory(test)
endif()
Expand Down
2 changes: 1 addition & 1 deletion include/unleash/feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <vector>

namespace unleash {
class Context;
struct Context;
class Feature {
public:
Feature(std::string name, std::vector<std::unique_ptr<Strategy>> strategies, bool enable);
Expand Down
4 changes: 3 additions & 1 deletion include/unleash/unleashclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace unleash {

class UnleashClientBuilder;
class Context;
struct Context;
struct variant_t;

class UNLEASH_EXPORT UnleashClient {
Expand Down Expand Up @@ -42,6 +42,7 @@ class UNLEASH_EXPORT UnleashClient {
std::string m_environment;
std::string m_authentication;
bool m_registration = false;
std::string m_cacheFilePath;
unsigned int m_refreshInterval = 15000;
std::thread m_thread;
bool m_stopThread = false;
Expand All @@ -65,6 +66,7 @@ class UNLEASH_EXPORT UnleashClientBuilder {
UnleashClientBuilder &apiClient(std::shared_ptr<ApiClient> apiClient);
UnleashClientBuilder &authentication(std::string authentication);
UnleashClientBuilder &registration(bool registration);
UnleashClientBuilder &cacheFilePath(std::string cacheFilePath);

private:
UnleashClient unleashClient;
Expand Down
41 changes: 35 additions & 6 deletions src/unleashclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "unleash/api/cprclient.h"
#include "unleash/strategies/strategy.h"
#include <nlohmann/json.hpp>
#include <fstream>
#include <sstream>

namespace unleash {

Expand Down Expand Up @@ -45,6 +47,11 @@ UnleashClientBuilder &UnleashClientBuilder::registration(bool registration) {
return *this;
}

UnleashClientBuilder &UnleashClientBuilder::cacheFilePath(std::string cacheFilePath) {
unleashClient.m_cacheFilePath = std::move(cacheFilePath);
return *this;
}

void UnleashClient::initializeClient() {
if (!m_isInitialized) {
// Set-up Unleash API client
Expand All @@ -61,12 +68,19 @@ void UnleashClient::initializeClient() {
// Initial fetch of feature flags
auto apiFeatures = m_apiClient->features();
if (apiFeatures.empty()) {
std::cerr << "Attempted to initialize an Unleash Client instance "
"without server response."
<< std::endl;
return;
std::cerr << "Attempted to initialize an Unleash Client instance without server response." << std::endl;
std::ifstream cacheFile(m_cacheFilePath, std::fstream::in);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could avoid to load the file if the cacheFile is not set or disable the "Could not open cache file" comment at least.

if (cacheFile.is_open()){
std::cout << "Reading configuration from cached file " << m_cacheFilePath << std::endl;
std::stringstream features_buffer;
features_buffer << cacheFile.rdbuf();
cacheFile.close();
m_features = loadFeatures(features_buffer.str());
} else
std::cout << "Could not open cache file '" << m_cacheFilePath << "' for reading." << std::endl;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a missing return in this case. Otherwise the periodic task is launched and the instance is marked as initialized.

} else {
m_features = loadFeatures(apiFeatures);
}
m_features = loadFeatures(apiFeatures);
m_thread = std::thread(&UnleashClient::periodicTask, this);
m_isInitialized = true;
} else {
Expand All @@ -86,7 +100,22 @@ void UnleashClient::periodicTask() {
if (globalTimer >= m_refreshInterval) {
globalTimer = 0;
auto features_response = m_apiClient->features();
if (!features_response.empty()) m_features = loadFeatures(features_response);
if (!features_response.empty()){
std::ofstream cacheFile(m_cacheFilePath);
if (cacheFile.is_open())
cacheFile << features_response;
cacheFile.close();
m_features = loadFeatures(features_response);
} else {
std::ifstream cacheFile(m_cacheFilePath);
if(cacheFile.is_open()){
std::stringstream features_buffer;
features_buffer << cacheFile.rdbuf();
cacheFile.close();
m_features = loadFeatures(features_buffer.str());

}
}
}
}
}
Expand Down
Loading