C++ Client-Side SDK
LaunchDarkly SDK
client_impl.hpp
1 #pragma once
2 
3 #include "data_sources/data_source.hpp"
4 #include "data_sources/data_source_status_manager.hpp"
5 #include "flag_manager/flag_manager.hpp"
6 
7 #include <launchdarkly/client_side/client.hpp>
8 #include <launchdarkly/client_side/data_source_status.hpp>
9 #include <launchdarkly/client_side/flag_notifier.hpp>
10 #include <launchdarkly/config/client.hpp>
11 #include <launchdarkly/context.hpp>
12 #include <launchdarkly/data/evaluation_detail.hpp>
13 #include <launchdarkly/error.hpp>
14 #include <launchdarkly/events/event_processor_interface.hpp>
15 #include <launchdarkly/logging/logger.hpp>
16 #include <launchdarkly/value.hpp>
17 
18 #include <boost/asio/executor_work_guard.hpp>
19 #include <boost/asio/io_context.hpp>
20 
21 #include <condition_variable>
22 #include <cstdint>
23 #include <memory>
24 #include <optional>
25 #include <shared_mutex>
26 #include <thread>
27 
28 namespace launchdarkly::client_side {
29 class ClientImpl : public IClient {
30  public:
31  ClientImpl(Config config, Context context, std::string const& version);
32 
33  ClientImpl(ClientImpl&&) = delete;
34  ClientImpl(ClientImpl const&) = delete;
35  ClientImpl& operator=(ClientImpl) = delete;
36  ClientImpl& operator=(ClientImpl&& other) = delete;
37 
38  bool Initialized() const override;
39 
40  using FlagKey = std::string;
41  [[nodiscard]] std::unordered_map<FlagKey, Value> AllFlags() const override;
42 
43  void Track(std::string event_name,
44  Value data,
45  double metric_value) override;
46 
47  void Track(std::string event_name, Value data) override;
48 
49  void Track(std::string event_name) override;
50 
51  void FlushAsync() override;
52 
53  std::future<bool> IdentifyAsync(Context context) override;
54 
55  bool BoolVariation(FlagKey const& key, bool default_value) override;
56 
58  bool default_value) override;
59 
60  std::string StringVariation(FlagKey const& key,
61  std::string default_value) override;
62 
64  FlagKey const& key,
65  std::string default_value) override;
66 
67  double DoubleVariation(FlagKey const& key, double default_value) override;
68 
70  FlagKey const& key,
71  double default_value) override;
72 
73  int IntVariation(FlagKey const& key, int default_value) override;
74 
76  int default_value) override;
77 
78  Value JsonVariation(FlagKey const& key, Value default_value) override;
79 
81  Value default_value) override;
82 
84 
86 
87  ~ClientImpl();
88 
89  std::future<bool> StartAsync() override;
90 
91  private:
92  template <typename T>
93  [[nodiscard]] EvaluationDetail<T> VariationInternal(FlagKey const& key,
94  Value default_value,
95  bool check_type,
96  bool detailed);
97  void TrackInternal(std::string event_name,
98  std::optional<Value> data,
99  std::optional<double> metric_value);
100 
101  template <typename F>
102  auto ReadContextSynchronized(F fn) const
103  -> std::invoke_result_t<F, Context const&> {
104  std::shared_lock lock(context_mutex_);
105  return fn(context_);
106  }
107 
108  void UpdateContextSynchronized(Context context);
109 
110  void RestartDataSource();
111 
112  std::future<bool> StartAsyncInternal();
113  Config config_;
114  Logger logger_;
115 
117 
118  boost::asio::io_context ioc_;
119  boost::asio::executor_work_guard<boost::asio::io_context::executor_type>
120  work_;
121 
122  Context context_;
123  mutable std::shared_mutex context_mutex_;
124 
125  flag_manager::FlagManager flag_manager_;
126  std::function<std::shared_ptr<data_sources::IDataSource>()>
127  data_source_factory_;
128 
129  std::shared_ptr<data_sources::IDataSource> data_source_;
130 
131  std::unique_ptr<events::IEventProcessor> event_processor_;
132 
133  mutable std::mutex init_mutex_;
134  std::condition_variable init_waiter_;
135 
137 
138  std::thread run_thread_;
139 
140  bool eval_reasons_available_;
141 };
142 } // namespace launchdarkly::client_side
Definition: context.hpp:29
Definition: evaluation_detail.hpp:18
Definition: value.hpp:42
Definition: client_impl.hpp:29
bool Initialized() const override
Definition: client_impl.cpp:206
std::string StringVariation(FlagKey const &key, std::string default_value) override
Definition: client_impl.cpp:356
Value JsonVariation(FlagKey const &key, Value default_value) override
Definition: client_impl.cpp:389
EvaluationDetail< Value > JsonVariationDetail(FlagKey const &key, Value default_value) override
Definition: client_impl.cpp:383
void FlushAsync() override
Definition: client_impl.cpp:244
flag_manager::IFlagNotifier & FlagNotifier() override
Definition: client_impl.cpp:399
EvaluationDetail< int > IntVariationDetail(FlagKey const &key, int default_value) override
Definition: client_impl.cpp:373
std::future< bool > IdentifyAsync(Context context) override
Definition: client_impl.cpp:161
std::future< bool > StartAsync() override
Definition: client_impl.cpp:202
void Track(std::string event_name, Value data, double metric_value) override
Definition: client_impl.cpp:230
std::unordered_map< FlagKey, Value > AllFlags() const override
Definition: client_impl.cpp:210
EvaluationDetail< std::string > StringVariationDetail(FlagKey const &key, std::string default_value) override
Definition: client_impl.cpp:349
data_sources::IDataSourceStatusProvider & DataSourceStatus() override
Definition: client_impl.cpp:395
bool BoolVariation(FlagKey const &key, bool default_value) override
Definition: client_impl.cpp:344
EvaluationDetail< double > DoubleVariationDetail(FlagKey const &key, double default_value) override
Definition: client_impl.cpp:362
int IntVariation(FlagKey const &key, int default_value) override
Definition: client_impl.cpp:379
double DoubleVariation(FlagKey const &key, double default_value) override
Definition: client_impl.cpp:368
EvaluationDetail< bool > BoolVariationDetail(FlagKey const &key, bool default_value) override
Definition: client_impl.cpp:338
Definition: client.hpp:26
std::string FlagKey
Definition: client.hpp:31
Definition: data_source_status_manager.hpp:17
Definition: http_properties.hpp:26
Definition: config.hpp:20