Skip to content

Commit c4a1952

Browse files
authored
fix: Move fdv2 data source builders into public API (#371)
1 parent 7e96729 commit c4a1952

13 files changed

Lines changed: 551 additions & 340 deletions

lib/ldclient-rb/config.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,15 @@ def self.default_capacity
468468
# @return [String] "https://sdk.launchdarkly.com"
469469
#
470470
def self.default_base_uri
471-
Impl::DataSystem::PollingDataSourceBuilder::DEFAULT_BASE_URI
471+
DataSystem::PollingDataSourceBuilder::DEFAULT_BASE_URI
472472
end
473473

474474
#
475475
# The default value for {#stream_uri}.
476476
# @return [String] "https://stream.launchdarkly.com"
477477
#
478478
def self.default_stream_uri
479-
Impl::DataSystem::StreamingDataSourceBuilder::DEFAULT_BASE_URI
479+
DataSystem::StreamingDataSourceBuilder::DEFAULT_BASE_URI
480480
end
481481

482482
#
@@ -516,7 +516,7 @@ def self.default_read_timeout
516516
# @return [Float] 1
517517
#
518518
def self.default_initial_reconnect_delay
519-
Impl::DataSystem::StreamingDataSourceBuilder::DEFAULT_INITIAL_RECONNECT_DELAY
519+
DataSystem::StreamingDataSourceBuilder::DEFAULT_INITIAL_RECONNECT_DELAY
520520
end
521521

522522
#
@@ -578,7 +578,7 @@ def self.default_offline
578578
# @return [Float] 30
579579
#
580580
def self.default_poll_interval
581-
Impl::DataSystem::PollingDataSourceBuilder::DEFAULT_POLL_INTERVAL
581+
DataSystem::PollingDataSourceBuilder::DEFAULT_POLL_INTERVAL
582582
end
583583

584584
#

lib/ldclient-rb/data_system.rb

Lines changed: 50 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,88 @@
44
require 'ldclient-rb/config'
55
require 'ldclient-rb/impl/data_system/polling'
66
require 'ldclient-rb/impl/data_system/streaming'
7+
require 'ldclient-rb/data_system/config_builder'
8+
require 'ldclient-rb/data_system/polling_data_source_builder'
9+
require 'ldclient-rb/data_system/streaming_data_source_builder'
710

811
module LaunchDarkly
912
#
1013
# Configuration for LaunchDarkly's data acquisition strategy.
1114
#
12-
# This module provides factory methods for creating data system configurations.
15+
# This module provides factory methods for creating data system configurations,
16+
# as well as builder classes for constructing individual data sources (polling
17+
# and streaming).
18+
#
19+
# == Quick Start
20+
#
21+
# For most users, the predefined strategies are sufficient:
22+
#
23+
# # Use the default strategy (recommended)
24+
# config = LaunchDarkly::Config.new(
25+
# data_system: LaunchDarkly::DataSystem.default
26+
# )
27+
#
28+
# # Use streaming only
29+
# config = LaunchDarkly::Config.new(
30+
# data_system: LaunchDarkly::DataSystem.streaming
31+
# )
32+
#
33+
# # Use polling only
34+
# config = LaunchDarkly::Config.new(
35+
# data_system: LaunchDarkly::DataSystem.polling
36+
# )
37+
#
38+
# == Custom Configurations
39+
#
40+
# For advanced use cases, you can build custom configurations using the
41+
# data source builders:
42+
#
43+
# polling = LaunchDarkly::DataSystem.polling_ds_builder
44+
# .poll_interval(60)
45+
# .base_uri("https://custom-polling.example.com")
46+
#
47+
# streaming = LaunchDarkly::DataSystem.streaming_ds_builder
48+
# .initial_reconnect_delay(2)
49+
# .base_uri("https://custom-streaming.example.com")
50+
#
51+
# data_system = LaunchDarkly::DataSystem.custom
52+
# .initializers([polling])
53+
# .synchronizers([streaming, polling])
54+
#
55+
# config = LaunchDarkly::Config.new(data_system: data_system)
1356
#
1457
module DataSystem
15-
#
16-
# Builder for the data system configuration.
17-
#
18-
class ConfigBuilder
19-
def initialize
20-
@initializers = nil
21-
@synchronizers = nil
22-
@fdv1_fallback_synchronizer = nil
23-
@data_store_mode = LaunchDarkly::Interfaces::DataSystem::DataStoreMode::READ_ONLY
24-
@data_store = nil
25-
end
26-
27-
#
28-
# Sets the initializers for the data system.
29-
#
30-
# @param initializers [Array<#build(String, Config)>]
31-
# Array of builders that respond to build(sdk_key, config) and return an Initializer
32-
# @return [ConfigBuilder] self for chaining
33-
#
34-
def initializers(initializers)
35-
@initializers = initializers
36-
self
37-
end
38-
39-
#
40-
# Sets the synchronizers for the data system.
41-
#
42-
# @param synchronizers [Array<#build(String, Config)>]
43-
# Array of builders that respond to build(sdk_key, config) and return a Synchronizer
44-
# @return [ConfigBuilder] self for chaining
45-
#
46-
def synchronizers(synchronizers)
47-
@synchronizers = synchronizers
48-
self
49-
end
50-
51-
#
52-
# Configures the SDK with a fallback synchronizer that is compatible with
53-
# the Flag Delivery v1 API.
54-
#
55-
# @param fallback [#build(String, Config)] Builder that responds to build(sdk_key, config) and returns the fallback Synchronizer
56-
# @return [ConfigBuilder] self for chaining
57-
#
58-
def fdv1_compatible_synchronizer(fallback)
59-
@fdv1_fallback_synchronizer = fallback
60-
self
61-
end
62-
63-
#
64-
# Sets the data store configuration for the data system.
65-
#
66-
# @param data_store [LaunchDarkly::Interfaces::FeatureStore] The data store
67-
# @param store_mode [Symbol] The store mode
68-
# @return [ConfigBuilder] self for chaining
69-
#
70-
def data_store(data_store, store_mode)
71-
@data_store = data_store
72-
@data_store_mode = store_mode
73-
self
74-
end
75-
76-
#
77-
# Builds the data system configuration.
78-
#
79-
# @return [DataSystemConfig]
80-
#
81-
def build
82-
DataSystemConfig.new(
83-
initializers: @initializers,
84-
synchronizers: @synchronizers,
85-
data_store_mode: @data_store_mode,
86-
data_store: @data_store,
87-
fdv1_fallback_synchronizer: @fdv1_fallback_synchronizer
88-
)
89-
end
90-
end
91-
9258
#
9359
# Returns a builder for creating a polling data source.
9460
# This is a building block that can be used with {ConfigBuilder#initializers}
9561
# or {ConfigBuilder#synchronizers} to create custom data system configurations.
9662
#
97-
# @return [LaunchDarkly::Impl::DataSystem::PollingDataSourceBuilder]
63+
# @return [PollingDataSourceBuilder]
9864
#
9965
def self.polling_ds_builder
100-
LaunchDarkly::Impl::DataSystem::PollingDataSourceBuilder.new
66+
PollingDataSourceBuilder.new
10167
end
10268

10369
#
10470
# Returns a builder for creating an FDv1 fallback polling data source.
10571
# This is a building block that can be used with {ConfigBuilder#fdv1_compatible_synchronizer}
10672
# to provide FDv1 compatibility in custom data system configurations.
10773
#
108-
# @return [LaunchDarkly::Impl::DataSystem::FDv1PollingDataSourceBuilder]
74+
# @return [FDv1PollingDataSourceBuilder]
10975
#
11076
def self.fdv1_fallback_ds_builder
111-
LaunchDarkly::Impl::DataSystem::FDv1PollingDataSourceBuilder.new
77+
FDv1PollingDataSourceBuilder.new
11278
end
11379

11480
#
11581
# Returns a builder for creating a streaming data source.
11682
# This is a building block that can be used with {ConfigBuilder#synchronizers}
11783
# to create custom data system configurations.
11884
#
119-
# @return [LaunchDarkly::Impl::DataSystem::StreamingDataSourceBuilder]
85+
# @return [StreamingDataSourceBuilder]
12086
#
12187
def self.streaming_ds_builder
122-
LaunchDarkly::Impl::DataSystem::StreamingDataSourceBuilder.new
88+
StreamingDataSourceBuilder.new
12389
end
12490

12591
#
@@ -224,4 +190,3 @@ def self.persistent_store(store)
224190
end
225191
end
226192
end
227-
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# frozen_string_literal: true
2+
3+
require "ldclient-rb/interfaces/data_system"
4+
5+
module LaunchDarkly
6+
module DataSystem
7+
#
8+
# Builder for the data system configuration.
9+
#
10+
# This builder configures the overall data acquisition strategy for the SDK,
11+
# including which data sources to use for initialization and synchronization,
12+
# and how to interact with a persistent data store.
13+
#
14+
# @see DataSystem.default
15+
# @see DataSystem.streaming
16+
# @see DataSystem.polling
17+
# @see DataSystem.custom
18+
#
19+
class ConfigBuilder
20+
def initialize
21+
@initializers = nil
22+
@synchronizers = nil
23+
@fdv1_fallback_synchronizer = nil
24+
@data_store_mode = LaunchDarkly::Interfaces::DataSystem::DataStoreMode::READ_ONLY
25+
@data_store = nil
26+
end
27+
28+
#
29+
# Sets the initializers for the data system.
30+
#
31+
# Initializers are used to fetch an initial set of data when the SDK starts.
32+
# They are tried in order; if the first one fails, the next is tried, and so on.
33+
#
34+
# @param initializers [Array<#build(String, Config)>]
35+
# Array of builders that respond to build(sdk_key, config) and return an Initializer
36+
# @return [ConfigBuilder] self for chaining
37+
#
38+
def initializers(initializers)
39+
@initializers = initializers
40+
self
41+
end
42+
43+
#
44+
# Sets the synchronizers for the data system.
45+
#
46+
# Synchronizers keep data up-to-date after initialization. Like initializers,
47+
# they are tried in order. If the primary synchronizer fails, the next one
48+
# takes over.
49+
#
50+
# @param synchronizers [Array<#build(String, Config)>]
51+
# Array of builders that respond to build(sdk_key, config) and return a Synchronizer
52+
# @return [ConfigBuilder] self for chaining
53+
#
54+
def synchronizers(synchronizers)
55+
@synchronizers = synchronizers
56+
self
57+
end
58+
59+
#
60+
# Configures the SDK with a fallback synchronizer that is compatible with
61+
# the Flag Delivery v1 API.
62+
#
63+
# This fallback is used when the server signals that the environment should
64+
# revert to FDv1 protocol. Most users will not need to set this directly.
65+
#
66+
# @param fallback [#build(String, Config)] Builder that responds to build(sdk_key, config) and returns the fallback Synchronizer
67+
# @return [ConfigBuilder] self for chaining
68+
#
69+
def fdv1_compatible_synchronizer(fallback)
70+
@fdv1_fallback_synchronizer = fallback
71+
self
72+
end
73+
74+
#
75+
# Sets the data store configuration for the data system.
76+
#
77+
# @param data_store [LaunchDarkly::Interfaces::FeatureStore] The data store
78+
# @param store_mode [Symbol] The store mode (use constants from
79+
# {LaunchDarkly::Interfaces::DataSystem::DataStoreMode})
80+
# @return [ConfigBuilder] self for chaining
81+
#
82+
def data_store(data_store, store_mode)
83+
@data_store = data_store
84+
@data_store_mode = store_mode
85+
self
86+
end
87+
88+
#
89+
# Builds the data system configuration.
90+
#
91+
# @return [DataSystemConfig]
92+
#
93+
def build
94+
DataSystemConfig.new(
95+
initializers: @initializers,
96+
synchronizers: @synchronizers,
97+
data_store_mode: @data_store_mode,
98+
data_store: @data_store,
99+
fdv1_fallback_synchronizer: @fdv1_fallback_synchronizer
100+
)
101+
end
102+
end
103+
end
104+
end

0 commit comments

Comments
 (0)