|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <launchdarkly/config/shared/builders/data_source_builder.hpp> |
| 4 | +#include <launchdarkly/config/shared/sdks.hpp> |
| 5 | +#include <launchdarkly/server_side/config/built/data_system/fdv2_config.hpp> |
| 6 | + |
| 7 | +#include <chrono> |
| 8 | +#include <optional> |
| 9 | +#include <string> |
| 10 | + |
| 11 | +namespace launchdarkly::server_side::config::builders { |
| 12 | + |
| 13 | +class FDv2Builder { |
| 14 | + public: |
| 15 | + class Streaming { |
| 16 | + public: |
| 17 | + Streaming& InitialReconnectDelay(std::chrono::milliseconds delay); |
| 18 | + Streaming& BaseUrl(std::string base_url); |
| 19 | + [[nodiscard]] built::FDv2Config::StreamingConfig Build() const; |
| 20 | + |
| 21 | + private: |
| 22 | + std::chrono::milliseconds initial_reconnect_delay_{1000}; |
| 23 | + std::optional<std::string> filter_key_; |
| 24 | + std::optional<std::string> base_url_override_; |
| 25 | + }; |
| 26 | + |
| 27 | + class Polling { |
| 28 | + public: |
| 29 | + Polling& PollInterval(std::chrono::seconds interval); |
| 30 | + Polling& BaseUrl(std::string base_url); |
| 31 | + [[nodiscard]] built::FDv2Config::PollingConfig Build() const; |
| 32 | + |
| 33 | + private: |
| 34 | + std::chrono::seconds poll_interval_{30}; |
| 35 | + std::optional<std::string> filter_key_; |
| 36 | + std::optional<std::string> base_url_override_; |
| 37 | + }; |
| 38 | + |
| 39 | + /** |
| 40 | + * @return A builder pre-populated with the spec-recommended initializers, |
| 41 | + * synchronizers, and FDv1 fallback. Equivalent to calling |
| 42 | + * Initializer(), Synchronizer(), and FDv1Fallback() with the |
| 43 | + * standard sources. |
| 44 | + */ |
| 45 | + static FDv2Builder Default(); |
| 46 | + |
| 47 | + /** |
| 48 | + * @return A builder with no initializers, no synchronizers, and no |
| 49 | + * FDv1 fallback. Callers must add sources explicitly. |
| 50 | + */ |
| 51 | + static FDv2Builder Custom(); |
| 52 | + |
| 53 | + /** |
| 54 | + * @brief Appends a polling initializer to the initializers list. |
| 55 | + * @param source Polling source configuration for the initializer. |
| 56 | + * @return Reference to this. |
| 57 | + */ |
| 58 | + FDv2Builder& Initializer(Polling source); |
| 59 | + |
| 60 | + /** |
| 61 | + * @brief Appends a streaming synchronizer to the synchronizers list. |
| 62 | + * Order in the list determines preference: the first entry is the |
| 63 | + * primary synchronizer, subsequent entries are fallbacks. |
| 64 | + * @param source Streaming source configuration. |
| 65 | + * @return Reference to this. |
| 66 | + */ |
| 67 | + FDv2Builder& Synchronizer(Streaming source); |
| 68 | + |
| 69 | + /** |
| 70 | + * @brief Appends a polling synchronizer to the synchronizers list. See |
| 71 | + * Synchronizer(Streaming) for ordering semantics. |
| 72 | + * @param source Polling source configuration. |
| 73 | + * @return Reference to this. |
| 74 | + */ |
| 75 | + FDv2Builder& Synchronizer(Polling source); |
| 76 | + |
| 77 | + using FDv1Streaming = |
| 78 | + launchdarkly::config::shared::builders::StreamingBuilder< |
| 79 | + launchdarkly::config::shared::ServerSDK>; |
| 80 | + using FDv1Polling = launchdarkly::config::shared::builders::PollingBuilder< |
| 81 | + launchdarkly::config::shared::ServerSDK>; |
| 82 | + |
| 83 | + /** |
| 84 | + * @brief Configures the FDv1 streaming source used as a last-resort |
| 85 | + * fallback when the LaunchDarkly service signals (via the |
| 86 | + * X-LD-FD-Fallback header) that the SDK should switch to FDv1. The |
| 87 | + * fallback reads its endpoint from the top-level ServiceEndpoints; to |
| 88 | + * point the fallback at a custom URL, configure ServiceEndpoints |
| 89 | + * accordingly. |
| 90 | + * @param source FDv1 streaming source configuration. |
| 91 | + * @return Reference to this. |
| 92 | + */ |
| 93 | + FDv2Builder& FDv1Fallback(FDv1Streaming source); |
| 94 | + |
| 95 | + /** |
| 96 | + * @brief Configures the FDv1 polling source used as a last-resort |
| 97 | + * fallback when the LaunchDarkly service signals (via the |
| 98 | + * X-LD-FD-Fallback header) that the SDK should switch to FDv1. The |
| 99 | + * fallback reads its endpoint from the top-level ServiceEndpoints; to |
| 100 | + * point the fallback at a custom URL, configure ServiceEndpoints |
| 101 | + * accordingly. |
| 102 | + * @param source FDv1 polling source configuration. |
| 103 | + * @return Reference to this. |
| 104 | + */ |
| 105 | + FDv2Builder& FDv1Fallback(FDv1Polling source); |
| 106 | + |
| 107 | + /** |
| 108 | + * @brief Disables the FDv1 fallback. After this call, an FDv1 |
| 109 | + * fallback directive from the service leaves the data source in |
| 110 | + * the interrupted state and schedules an FDv2 retry on the |
| 111 | + * directive's TTL. |
| 112 | + * @return Reference to this. |
| 113 | + */ |
| 114 | + FDv2Builder& DisableFDv1Fallback(); |
| 115 | + |
| 116 | + /** |
| 117 | + * @brief Sets how long the active synchronizer may remain interrupted |
| 118 | + * before the orchestrator falls back to the next-preferred synchronizer. |
| 119 | + * @param timeout Duration the synchronizer must be continuously |
| 120 | + * interrupted for before fallback fires. |
| 121 | + * @return Reference to this. |
| 122 | + */ |
| 123 | + FDv2Builder& FallbackTimeout(std::chrono::milliseconds timeout); |
| 124 | + |
| 125 | + /** |
| 126 | + * @brief Sets how long a fallback synchronizer must run successfully |
| 127 | + * before the orchestrator attempts to recover to the primary |
| 128 | + * synchronizer. |
| 129 | + * @param timeout Duration the fallback synchronizer must run before a |
| 130 | + * recovery attempt is made. |
| 131 | + * @return Reference to this. |
| 132 | + */ |
| 133 | + FDv2Builder& RecoveryTimeout(std::chrono::milliseconds timeout); |
| 134 | + |
| 135 | + [[nodiscard]] built::FDv2Config Build() const; |
| 136 | + |
| 137 | + private: |
| 138 | + FDv2Builder(); |
| 139 | + |
| 140 | + built::FDv2Config config_; |
| 141 | +}; |
| 142 | + |
| 143 | +} // namespace launchdarkly::server_side::config::builders |
0 commit comments