|
| 1 | +using System; |
| 2 | +using LaunchDarkly.Sdk.Server.Interfaces; |
| 3 | + |
| 4 | +namespace LaunchDarkly.Sdk.Server.Integrations |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Contains methods for configuring the SDK's big segments behavior. |
| 8 | + /// </summary> |
| 9 | + /// <remarks> |
| 10 | + /// <para> |
| 11 | + /// "Big segments" are a specific type of user segments. For more information, read the LaunchDarkly |
| 12 | + /// documentation about user segments: https://docs.launchdarkly.com/home/users |
| 13 | + /// </para> |
| 14 | + /// <para> |
| 15 | + /// If you want to set non-default values for any of these properties, create a builder with |
| 16 | + /// <see cref="Components.BigSegments(IBigSegmentStoreFactory)"/>, change its properties with the |
| 17 | + /// methods of this class, and pass it to <see cref="ConfigurationBuilder.BigSegments(IBigSegmentsConfigurationFactory)"/>: |
| 18 | + /// </para> |
| 19 | + /// </remarks> |
| 20 | + /// <example> |
| 21 | + /// <code> |
| 22 | + /// // This example uses the Redis integration |
| 23 | + /// var config = Configuration.Builder(sdkKey) |
| 24 | + /// .BigSegments(Components.BigSegments(Redis.DataStore().Prefix("app1")) |
| 25 | + /// .UserCacheSize(2000)) |
| 26 | + /// .Build(); |
| 27 | + /// </code> |
| 28 | + /// </example> |
| 29 | + public sealed class BigSegmentsConfigurationBuilder : IBigSegmentsConfigurationFactory |
| 30 | + { |
| 31 | + /// <summary> |
| 32 | + /// Default value for <see cref="UserCacheSize(int)"/>. |
| 33 | + /// </summary> |
| 34 | + public const int DefaultUserCacheSize = 1000; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Default value for <see cref="UserCacheTime(TimeSpan)"/>: five seconds. |
| 38 | + /// </summary> |
| 39 | + public static readonly TimeSpan DefaultUserCacheTime = TimeSpan.FromSeconds(5); |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Default value for <see cref="StatusPollInterval(TimeSpan)"/>: five seconds. |
| 43 | + /// </summary> |
| 44 | + public static readonly TimeSpan DefaultStatusPollInterval = TimeSpan.FromSeconds(5); |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Default value for <see cref="StaleAfter(TimeSpan)"/>: two minutes. |
| 48 | + /// </summary> |
| 49 | + public static readonly TimeSpan DefaultStaleAfter = TimeSpan.FromMinutes(2); |
| 50 | + |
| 51 | + private readonly IBigSegmentStoreFactory _storeFactory; |
| 52 | + private int _userCacheSize = DefaultUserCacheSize; |
| 53 | + private TimeSpan _userCacheTime = DefaultUserCacheTime; |
| 54 | + private TimeSpan _statusPollInterval = DefaultStatusPollInterval; |
| 55 | + private TimeSpan _staleAfter = DefaultStaleAfter; |
| 56 | + |
| 57 | + internal BigSegmentsConfigurationBuilder(IBigSegmentStoreFactory storeFactory) |
| 58 | + { |
| 59 | + _storeFactory = storeFactory; |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Sets the maximum number of users whose big segment state will be cached by the SDK |
| 64 | + /// at any given time. |
| 65 | + /// </summary> |
| 66 | + /// <remarks> |
| 67 | + /// <para> |
| 68 | + /// To reduce database traffic, the SDK maintains a least-recently-used cache by user key. When a feature |
| 69 | + /// flag that references a big segment is evaluated for some user who is not currently in the cache, the |
| 70 | + /// SDK queries the database for all big segment memberships of that user, and stores them together in a |
| 71 | + /// single cache entry. If the cache is full, the oldest entry is dropped. |
| 72 | + /// </para> |
| 73 | + /// <para> |
| 74 | + /// A higher value for <see cref="UserCacheSize(int)"/> means that database queries for big segments will |
| 75 | + /// be done less often for recently-referenced users, if the application has many users, at the cost of |
| 76 | + /// increased memory used by the cache. |
| 77 | + /// </para> |
| 78 | + /// <para> |
| 79 | + /// Cache entries can also expire based on the setting of <see cref="UserCacheTime(TimeSpan)"/>. |
| 80 | + /// </para> |
| 81 | + /// </remarks> |
| 82 | + /// <param name="userCacheSize">the maximum number of user states to cache</param> |
| 83 | + /// <returns>the builder</returns> |
| 84 | + /// <seealso cref="DefaultUserCacheSize"/> |
| 85 | + public BigSegmentsConfigurationBuilder UserCacheSize(int userCacheSize) |
| 86 | + { |
| 87 | + _userCacheSize = userCacheSize; |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Sets the maximum length of time that the big segment state for a user will be cached |
| 93 | + /// by the SDK. |
| 94 | + /// </summary> |
| 95 | + /// <remarks> |
| 96 | + /// <para> |
| 97 | + /// See <see cref="UserCacheSize(int)"/> for more about this cache. A higher value for |
| 98 | + /// <see cref="UserCacheTime(TimeSpan)"/> means that database queries for the big segment state of any |
| 99 | + /// given user will be done less often, but that changes to segment membership may not be detected as soon. |
| 100 | + /// </para> |
| 101 | + /// </remarks> |
| 102 | + /// <param name="userCacheTime">the cache TTL</param> |
| 103 | + /// <returns>the builder</returns> |
| 104 | + /// <seealso cref="DefaultUserCacheTime"/> |
| 105 | + public BigSegmentsConfigurationBuilder UserCacheTime(TimeSpan userCacheTime) |
| 106 | + { |
| 107 | + _userCacheTime = userCacheTime; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Sets the interval at which the SDK will poll the big segment store to make sure |
| 113 | + /// it is available and to determine how long ago it was updated. |
| 114 | + /// </summary> |
| 115 | + /// <param name="statusPollInterval">the status polling interval (any value less than or |
| 116 | + /// equal to zero will be changed to <see cref="DefaultStatusPollInterval"/>)</param> |
| 117 | + /// <returns>the builder</returns> |
| 118 | + /// <seealso cref="DefaultStatusPollInterval"/> |
| 119 | + public BigSegmentsConfigurationBuilder StatusPollInterval(TimeSpan statusPollInterval) |
| 120 | + { |
| 121 | + _statusPollInterval = statusPollInterval > TimeSpan.Zero ? statusPollInterval : |
| 122 | + DefaultStatusPollInterval; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Sets the maximum length of time between updates of the big segments data before the data |
| 128 | + /// is considered out of date. |
| 129 | + /// </summary> |
| 130 | + /// <remarks> |
| 131 | + /// <para> |
| 132 | + /// Normally, the LaunchDarkly Relay Proxy updates a timestamp in the big segments store at intervals to |
| 133 | + /// confirm that it is still in sync with the LaunchDarkly data, even if there have been no changes to the |
| 134 | + /// data. If the timestamp falls behind the current time by the amount specified in |
| 135 | + /// <see cref="StaleAfter(TimeSpan)"/>, the SDK assumes that something is not working correctly in this |
| 136 | + /// process and that the data may not be accurate. |
| 137 | + /// </para> |
| 138 | + /// <para> |
| 139 | + /// While in a stale state, the SDK will still continue using the last known data, but |
| 140 | + /// <see cref="IBigSegmentStoreStatusProvider.Status"/> will return true in its Stale property, and any |
| 141 | + /// <see cref="EvaluationReason"/> generated from a feature flag that references a big segment will have |
| 142 | + /// a <see cref="EvaluationReason.BigSegmentsStatus"/> of <see cref="BigSegmentsStatus.Stale"/>. |
| 143 | + /// </para> |
| 144 | + /// </remarks> |
| 145 | + /// <param name="staleAfter">the time limit for marking the data as stale (any value less |
| 146 | + /// than or equal to zero will be changed to <see cref="DefaultStaleAfter"/>)</param> |
| 147 | + /// <returns>the builder</returns> |
| 148 | + public BigSegmentsConfigurationBuilder StaleAfter(TimeSpan staleAfter) |
| 149 | + { |
| 150 | + _staleAfter = staleAfter > TimeSpan.Zero ? staleAfter : DefaultStaleAfter; |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + /// <inheritdoc/> |
| 155 | + public BigSegmentsConfiguration CreateBigSegmentsConfiguration(LdClientContext context) |
| 156 | + { |
| 157 | + var store = _storeFactory is null ? null : _storeFactory.CreateBigSegmentStore(context); |
| 158 | + return new BigSegmentsConfiguration( |
| 159 | + store, |
| 160 | + _userCacheSize, |
| 161 | + _userCacheTime, |
| 162 | + _statusPollInterval, |
| 163 | + _staleAfter |
| 164 | + ); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments