|
| 1 | +package com.launchdarkly.sdk.server.integrations; |
| 2 | + |
| 3 | +import com.launchdarkly.sdk.server.subsystems.ComponentConfigurer; |
| 4 | +import com.launchdarkly.sdk.server.subsystems.DataStore; |
| 5 | +import com.launchdarkly.sdk.server.subsystems.DataSystemConfiguration; |
| 6 | + |
| 7 | +/** |
| 8 | + * A set of different data system modes which provide pre-configured {@link DataSystemBuilder}s. |
| 9 | + * <p> |
| 10 | + * This class is not stable, and not subject to any backwards compatibility guarantees or semantic versioning. |
| 11 | + * It is in early access. If you want access to this feature please join the EAP. https://launchdarkly.com/docs/sdk/features/data-saving-mode |
| 12 | + * </p> |
| 13 | + * <p> |
| 14 | + * This implementation is non-static to allow for easy usage with "Components". |
| 15 | + * Where we can return an instance of this object, and the user can chain into their desired configuration. |
| 16 | + * </p> |
| 17 | + */ |
| 18 | +public final class DataSystemModes { |
| 19 | + // This implementation is non-static to allow for easy usage with "Components". |
| 20 | + // Where we can return an instance of this object, and the user can chain into their desired configuration. |
| 21 | + |
| 22 | + /** |
| 23 | + * Configure's LaunchDarkly's recommended flag data acquisition strategy. |
| 24 | + * <p> |
| 25 | + * Currently, it operates a two-phase method for getting data: first, it requests data from LaunchDarkly's |
| 26 | + * global CDN. Then, it initiates a streaming connection to LaunchDarkly's Flag Delivery services to receive |
| 27 | + * real-time updates. If the streaming connection is interrupted for an extended period of time, the SDK will |
| 28 | + * automatically fall back to polling the global CDN for updates. |
| 29 | + * </p> |
| 30 | + * <p> |
| 31 | + * <b>Example:</b> |
| 32 | + * </p> |
| 33 | + * <pre><code> |
| 34 | + * LDConfig config = new LDConfig.Builder("my-sdk-key") |
| 35 | + * .dataSystem(Components.dataSystem().defaultMode()); |
| 36 | + * </code></pre> |
| 37 | + * |
| 38 | + * @return a builder containing our default configuration |
| 39 | + */ |
| 40 | + public DataSystemBuilder defaultMode() { |
| 41 | + return custom() |
| 42 | + .initializers(DataSystemComponents.polling()) |
| 43 | + .synchronizers(DataSystemComponents.streaming(), DataSystemComponents.polling()) |
| 44 | + .fDv1FallbackSynchronizer(DataSystemComponents.fDv1Polling()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Configures the SDK to stream data without polling for the initial payload. |
| 49 | + * <p> |
| 50 | + * This is not our recommended strategy, which is {@link #defaultMode()}, but it may be |
| 51 | + * suitable for some situations. |
| 52 | + * </p> |
| 53 | + * <p> |
| 54 | + * This configuration will not automatically fall back to polling, but it can be instructed by LaunchDarkly |
| 55 | + * to fall back to polling in certain situations. |
| 56 | + * </p> |
| 57 | + * <p> |
| 58 | + * <b>Example:</b> |
| 59 | + * </p> |
| 60 | + * <pre><code> |
| 61 | + * LDConfig config = new LDConfig.Builder("my-sdk-key") |
| 62 | + * .dataSystem(Components.dataSystem().streaming()); |
| 63 | + * </code></pre> |
| 64 | + * |
| 65 | + * @return a builder containing a primarily streaming configuration |
| 66 | + */ |
| 67 | + public DataSystemBuilder streaming() { |
| 68 | + return custom() |
| 69 | + .synchronizers(DataSystemComponents.streaming()) |
| 70 | + .fDv1FallbackSynchronizer(DataSystemComponents.fDv1Polling()); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Configure the SDK to poll data instead of receiving real-time updates via a stream. |
| 75 | + * <p> |
| 76 | + * This is not our recommended strategy, which is {@link #defaultMode()}, but it may be |
| 77 | + * required for certain network configurations. |
| 78 | + * </p> |
| 79 | + * <p> |
| 80 | + * <b>Example:</b> |
| 81 | + * </p> |
| 82 | + * <pre><code> |
| 83 | + * LDConfig config = new LDConfig.Builder("my-sdk-key") |
| 84 | + * .dataSystem(Components.dataSystem().polling()); |
| 85 | + * </code></pre> |
| 86 | + * |
| 87 | + * @return a builder containing a polling-only configuration |
| 88 | + */ |
| 89 | + public DataSystemBuilder polling() { |
| 90 | + return custom() |
| 91 | + .synchronizers(DataSystemComponents.polling()) |
| 92 | + .fDv1FallbackSynchronizer(DataSystemComponents.fDv1Polling()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Configures the SDK to read from a persistent store integration that is populated by Relay Proxy |
| 97 | + * or other SDKs. The SDK will not connect to LaunchDarkly. In this mode, the SDK never writes to the data |
| 98 | + * store. |
| 99 | + * <p> |
| 100 | + * <b>Example:</b> |
| 101 | + * </p> |
| 102 | + * <pre><code> |
| 103 | + * LDConfig config = new LDConfig.Builder("my-sdk-key") |
| 104 | + * .dataSystem(Components.dataSystem().daemon(persistentStore)); |
| 105 | + * </code></pre> |
| 106 | + * |
| 107 | + * @param persistentStore the persistent store configurer |
| 108 | + * @return a builder which is configured for daemon mode |
| 109 | + */ |
| 110 | + public DataSystemBuilder daemon(ComponentConfigurer<DataStore> persistentStore) { |
| 111 | + return custom() |
| 112 | + .persistentStore(persistentStore, DataSystemConfiguration.DataStoreMode.READ_ONLY); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * PersistentStore is similar to Default, with the addition of a persistent store integration. Before data has |
| 117 | + * arrived from LaunchDarkly, the SDK is able to evaluate flags using data from the persistent store. |
| 118 | + * Once fresh data is available, the SDK will no longer read from the persistent store, although it will keep |
| 119 | + * it up to date. |
| 120 | + * <p> |
| 121 | + * <b>Example:</b> |
| 122 | + * </p> |
| 123 | + * <pre><code> |
| 124 | + * LDConfig config = new LDConfig.Builder("my-sdk-key") |
| 125 | + * .dataSystem(Components.dataSystem() |
| 126 | + * .persistentStore(Components.persistentDataStore(SomeDatabaseName.dataStore()))); |
| 127 | + * </code></pre> |
| 128 | + * |
| 129 | + * @param persistentStore the persistent store configurer |
| 130 | + * @return a builder which is configured for persistent store mode |
| 131 | + */ |
| 132 | + public DataSystemBuilder persistentStore(ComponentConfigurer<DataStore> persistentStore) { |
| 133 | + return defaultMode() |
| 134 | + .persistentStore(persistentStore, DataSystemConfiguration.DataStoreMode.READ_WRITE); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Custom returns a builder suitable for creating a custom data acquisition strategy. You may configure |
| 139 | + * how the SDK uses a Persistent Store, how the SDK obtains an initial set of data, and how the SDK keeps data |
| 140 | + * up to date. |
| 141 | + * <p> |
| 142 | + * <b>Example:</b> |
| 143 | + * </p> |
| 144 | + * <pre><code> |
| 145 | + * LDConfig config = new LDConfig.Builder("my-sdk-key") |
| 146 | + * .dataSystem(Components.dataSystem().custom() |
| 147 | + * .initializers(DataSystemComponents.polling()) |
| 148 | + * .synchronizers(DataSystemComponents.streaming(), DataSystemComponents.polling()) |
| 149 | + * .fDv1FallbackSynchronizer(DataSystemComponents.fDv1Polling())); |
| 150 | + * </code></pre> |
| 151 | + * |
| 152 | + * @return a builder without any base configuration |
| 153 | + */ |
| 154 | + public DataSystemBuilder custom() { |
| 155 | + return new DataSystemBuilder(); |
| 156 | + } |
| 157 | +} |
| 158 | + |
0 commit comments