|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <chrono> |
| 4 | +#include <optional> |
| 5 | +#include <type_traits> |
| 6 | + |
| 7 | +#include "config/detail/data_source_config.hpp" |
| 8 | +#include "config/detail/defaults.hpp" |
| 9 | +#include "config/detail/sdks.hpp" |
| 10 | + |
| 11 | +#include <boost/variant.hpp> |
| 12 | + |
| 13 | +namespace launchdarkly::config::detail { |
| 14 | + |
| 15 | +/** |
| 16 | + * Used to construct a DataSourceConfiguration for the specified SDK type. |
| 17 | + * @tparam SDK ClientSDK or ServerSDK. |
| 18 | + */ |
| 19 | +template <typename SDK> |
| 20 | +class DataSourceBuilder; |
| 21 | + |
| 22 | +/** |
| 23 | + * Builds a configuration for a streaming data source. |
| 24 | + */ |
| 25 | +class StreamingBuilder { |
| 26 | + public: |
| 27 | + StreamingBuilder(); |
| 28 | + |
| 29 | + /** |
| 30 | + * Sets the initial reconnect delay for the streaming connection. |
| 31 | + * |
| 32 | + * The streaming service uses a backoff algorithm (with jitter) every time |
| 33 | + * the connection needs to be reestablished.The delay for the first |
| 34 | + * reconnection will start near this value, and then increase exponentially |
| 35 | + * for any subsequent connection failures. |
| 36 | + * |
| 37 | + * @param initial_reconnect_delay The initial delay for a reconnection |
| 38 | + * attempt. |
| 39 | + * @return Reference to this builder. |
| 40 | + */ |
| 41 | + StreamingBuilder& initial_reconnect_delay( |
| 42 | + std::chrono::milliseconds initial_reconnect_delay); |
| 43 | + |
| 44 | + /** |
| 45 | + * Build the streaming config. Used internal to the SDK. |
| 46 | + * @return The built config. |
| 47 | + */ |
| 48 | + [[nodiscard]] StreamingConfig build() const; |
| 49 | + |
| 50 | + private: |
| 51 | + StreamingConfig config_; |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Contains methods for configuring the polling data source. |
| 56 | + */ |
| 57 | +class PollingBuilder { |
| 58 | + public: |
| 59 | + PollingBuilder(); |
| 60 | + |
| 61 | + /** |
| 62 | + * Sets the interval at which the SDK will poll for feature flag updates. |
| 63 | + * @param poll_interval The polling interval. |
| 64 | + * @return Reference to this builder. |
| 65 | + */ |
| 66 | + PollingBuilder& poll_interval(std::chrono::seconds poll_interval); |
| 67 | + |
| 68 | + /** |
| 69 | + * Build the polling config. Used internal to the SDK. |
| 70 | + * @return The built config. |
| 71 | + */ |
| 72 | + [[nodiscard]] PollingConfig build() const; |
| 73 | + |
| 74 | + private: |
| 75 | + PollingConfig config_; |
| 76 | +}; |
| 77 | + |
| 78 | +/** |
| 79 | + * The method visitor is only needed inside this file |
| 80 | + */ |
| 81 | +namespace { |
| 82 | +struct MethodVisitor { |
| 83 | + boost::variant<StreamingConfig, PollingConfig> operator()( |
| 84 | + StreamingBuilder streaming) { |
| 85 | + return streaming.build(); |
| 86 | + } |
| 87 | + boost::variant<StreamingConfig, PollingConfig> operator()( |
| 88 | + PollingBuilder polling) { |
| 89 | + return polling.build(); |
| 90 | + } |
| 91 | +}; |
| 92 | +} // namespace |
| 93 | + |
| 94 | +template <> |
| 95 | +class DataSourceBuilder<ClientSDK> { |
| 96 | + public: |
| 97 | + using Streaming = StreamingBuilder; |
| 98 | + using Polling = PollingBuilder; |
| 99 | + |
| 100 | + DataSourceBuilder(); |
| 101 | + |
| 102 | + /** |
| 103 | + * Whether LaunchDarkly should provide additional information about how flag |
| 104 | + * values were calculated. |
| 105 | + * |
| 106 | + * The additional information will then be available through the client's |
| 107 | + * {TODO variation detail} method. Since this increases the size of network |
| 108 | + * requests, such information is not sent unless you set this option to |
| 109 | + * true. |
| 110 | + * @param value True to enable reasons. |
| 111 | + * @return Reference to this builder. |
| 112 | + */ |
| 113 | + DataSourceBuilder& with_reasons(bool value); |
| 114 | + |
| 115 | + /** |
| 116 | + * Whether or not to use the REPORT verb to fetch flag settings. |
| 117 | + * |
| 118 | + * If this is true, flag settings will be fetched with a REPORT request |
| 119 | + * including a JSON entity body with the context object. |
| 120 | + * |
| 121 | + * Otherwise (by default) a GET request will be issued with the context |
| 122 | + * passed as a base64 URL-encoded path parameter. |
| 123 | + * |
| 124 | + * Do not use unless advised by LaunchDarkly. |
| 125 | + * @param value True to enable using the REPORT verb. |
| 126 | + * @return Reference to this builder. |
| 127 | + */ |
| 128 | + DataSourceBuilder& use_report(bool value); |
| 129 | + |
| 130 | + /** |
| 131 | + * Set the streaming configuration for the builder. |
| 132 | + * |
| 133 | + * A data source may either be streaming or polling. Setting a streaming |
| 134 | + * builder indicates the data source will use streaming. Setting a polling |
| 135 | + * builder will indicate the use of polling. |
| 136 | + * |
| 137 | + * @param stream_builder The streaming builder. |
| 138 | + * @return Reference to this builder. |
| 139 | + */ |
| 140 | + DataSourceBuilder& method(Streaming stream_builder); |
| 141 | + |
| 142 | + /** |
| 143 | + * Set the polling configuration for the builder. |
| 144 | + * |
| 145 | + * A data source may either be streaming or polling. Setting a stream |
| 146 | + * builder indicates the data source will use streaming. Setting a polling |
| 147 | + * builder will indicate the use of polling. |
| 148 | + * |
| 149 | + * @param polling_builder The polling builder. |
| 150 | + * @return Reference to this builder. |
| 151 | + */ |
| 152 | + DataSourceBuilder& method(Polling polling_builder); |
| 153 | + |
| 154 | + /** |
| 155 | + * Build a data source config. This is used internal to the SDK. |
| 156 | + * |
| 157 | + * @return The built config. |
| 158 | + */ |
| 159 | + [[nodiscard]] DataSourceConfig<ClientSDK> build() const; |
| 160 | + |
| 161 | + private: |
| 162 | + boost::variant<Streaming, Polling> method_; |
| 163 | + bool with_reasons_; |
| 164 | + bool use_report_; |
| 165 | +}; |
| 166 | + |
| 167 | +template <> |
| 168 | +class DataSourceBuilder<ServerSDK> { |
| 169 | + public: |
| 170 | + using Streaming = StreamingBuilder; |
| 171 | + using Polling = PollingBuilder; |
| 172 | + |
| 173 | + DataSourceBuilder(); |
| 174 | + |
| 175 | + /** |
| 176 | + * Set the streaming configuration for the builder. |
| 177 | + * |
| 178 | + * A data source may either be streaming or polling. Setting a streaming |
| 179 | + * builder indicates the data source will use streaming. Setting a polling |
| 180 | + * builder will indicate the use of polling. |
| 181 | + * |
| 182 | + * @param stream_builder The streaming builder. |
| 183 | + * @return Reference to this builder. |
| 184 | + */ |
| 185 | + DataSourceBuilder& method(Streaming builder); |
| 186 | + |
| 187 | + /** |
| 188 | + * Set the polling configuration for the builder. |
| 189 | + * |
| 190 | + * A data source may either be streaming or polling. Setting a stream |
| 191 | + * builder indicates the data source will use streaming. Setting a polling |
| 192 | + * builder will indicate the use of polling. |
| 193 | + * |
| 194 | + * @param polling_builder The polling builder. |
| 195 | + * @return Reference to this builder. |
| 196 | + */ |
| 197 | + DataSourceBuilder& method(Polling builder); |
| 198 | + |
| 199 | + /** |
| 200 | + * Build a data source config. This is used internal to the SDK. |
| 201 | + * |
| 202 | + * @return The built config. |
| 203 | + */ |
| 204 | + [[nodiscard]] DataSourceConfig<ServerSDK> build() const; |
| 205 | + |
| 206 | + private: |
| 207 | + boost::variant<Streaming, Polling> method_; |
| 208 | + bool with_reasons_; |
| 209 | + bool use_report_; |
| 210 | +}; |
| 211 | + |
| 212 | +} // namespace launchdarkly::config::detail |
0 commit comments