Skip to content

Commit

Permalink
Merge pull request #88 from launchdarkly/eb/data-source
Browse files Browse the repository at this point in the history
change name of "update processor" to "data source"
  • Loading branch information
eli-darkly authored Dec 18, 2018
2 parents 216a1e8 + 414af99 commit 0fc71a8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 42 deletions.
28 changes: 14 additions & 14 deletions lib/ldclient-rb/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class Config
# @option opts [Integer] :user_keys_capacity (1000) See {#user_keys_capacity}.
# @option opts [Float] :user_keys_flush_interval (300) See {#user_keys_flush_interval}.
# @option opts [Boolean] :inline_users_in_events (false) See {#inline_users_in_events}.
# @option opts [Object] :update_processor See {#update_processor}.
# @option opts [Object] :update_processor_factory See {#update_processor_factory}.
# @option opts [Object] :data_source See {#data_source}.
# @option opts [Object] :update_processor Obsolete synonym for `data_source`.
# @option opts [Object] :update_processor_factory Obsolete synonym for `data_source`.
#
def initialize(opts = {})
@base_uri = (opts[:base_uri] || Config.default_base_uri).chomp("/")
Expand All @@ -59,6 +60,7 @@ def initialize(opts = {})
@user_keys_capacity = opts[:user_keys_capacity] || Config.default_user_keys_capacity
@user_keys_flush_interval = opts[:user_keys_flush_interval] || Config.default_user_keys_flush_interval
@inline_users_in_events = opts[:inline_users_in_events] || false
@data_source = opts[:data_source] || opts[:update_processor] || opts[:update_processor_factory]
@update_processor = opts[:update_processor]
@update_processor_factory = opts[:update_processor_factory]
end
Expand Down Expand Up @@ -245,22 +247,20 @@ def offline?
# An object that is responsible for receiving feature flag data from LaunchDarkly. By default,
# the client uses its standard polling or streaming implementation; this is customizable for
# testing purposes.
# @return [LaunchDarkly::Interfaces::UpdateProcessor]
# @deprecated The preferred way to set this is now with {#update_processor_factory}.
#
attr_reader :update_processor

#
# Factory for an object that is responsible for receiving feature flag data from LaunchDarkly
# By default, the client uses its standard polling or streaming implementation; this is
# customizable for testing purposes.
#
# The factory is a lambda or Proc that takes two parameters: the SDK key and the {Config}. It
# must return an object that conforms to {LaunchDarkly::Interfaces::UpdateProcessor}.
# This may be set to either an object that conforms to {LaunchDarkly::Interfaces::DataSource},
# or a lambda (or Proc) that takes two parameters-- SDK key and {Config}-- and returns such an
# object.
#
# @return [lambda]
# @return [LaunchDarkly::Interfaces::DataSource|lambda]
# @see FileDataSource
#
attr_reader :data_source

# @deprecated This is replaced by {#data_source}.
attr_reader :update_processor

# @deprecated This is replaced by {#data_source}.
attr_reader :update_processor_factory

#
Expand Down
12 changes: 5 additions & 7 deletions lib/ldclient-rb/file_data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def self.have_listen?
# actual LaunchDarkly connection.
#
# To use this component, call {FileDataSource#factory}, and store its return value in the
# {Config#update_processor_factory} property of your LaunchDarkly client configuration. In the options
# {Config#data_source} property of your LaunchDarkly client configuration. In the options
# to `factory`, set `paths` to the file path(s) of your data file(s):
#
# factory = FileDataSource.factory(paths: [ myFilePath ])
# config = LaunchDarkly::Config.new(update_processor_factory: factory)
# file_source = FileDataSource.factory(paths: [ myFilePath ])
# config = LaunchDarkly::Config.new(data_source: file_source)
#
# This will cause the client not to connect to LaunchDarkly to get feature flags. The
# client may still make network connections to send analytics events, unless you have disabled
Expand Down Expand Up @@ -113,12 +113,10 @@ class FileDataSource
# @option options [Float] :poll_interval The minimum interval, in seconds, between checks for
# file modifications - used only if auto_update is true, and if the native file-watching
# mechanism from 'listen' is not being used. The default value is 1 second.
# @return an object that can be stored in {Config#update_processor_factory}
# @return an object that can be stored in {Config#data_source}
#
def self.factory(options={})
return Proc.new do |sdk_key, config|
FileDataSourceImpl.new(config.feature_store, config.logger, options)
end
return lambda { |sdk_key, config| FileDataSourceImpl.new(config.feature_store, config.logger, options) }
end
end

Expand Down
12 changes: 6 additions & 6 deletions lib/ldclient-rb/interfaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ def stop
end

#
# Mixin that defines the required methods of an update processor implementation. This is
# the component that delivers feature flag data from LaunchDarkly to the LDClient by putting
# Mixin that defines the required methods of a data source implementation. This is the
# component that delivers feature flag data from LaunchDarkly to the LDClient by putting
# the data in the {FeatureStore}. It is expected to run concurrently on its own thread.
#
# The client has its own standard implementation, which uses either a streaming connection or
# polling depending on your configuration. Normally you will not need to use another one
# except for testing purposes. {FileDataSource} provides one such test fixture.
#
module UpdateProcessor
module DataSource
#
# Checks whether the processor has finished initializing. Initialization is considered done
# Checks whether the data source has finished initializing. Initialization is considered done
# once it has received one complete data set from LaunchDarkly.
#
# @return [Boolean] true if initialization is complete
Expand All @@ -127,7 +127,7 @@ def initialized?
end

#
# Puts the processor into an active state. Normally this means it will make its first
# Puts the data source into an active state. Normally this means it will make its first
# connection attempt to LaunchDarkly. If `start` has already been called, calling it again
# should simply return the same value as the first call.
#
Expand All @@ -137,7 +137,7 @@ def start
end

#
# Puts the processor into an inactive state and releases all of its resources.
# Puts the data source into an inactive state and releases all of its resources.
# This state should be considered permanent (`start` does not have to work after `stop`).
#
def stop
Expand Down
18 changes: 9 additions & 9 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ def initialize(sdk_key, config = Config.default, wait_for_sec = 5)
return # requestor and update processor are not used in this mode
end

if @config.update_processor
@update_processor = @config.update_processor
data_source_or_factory = @config.data_source || self.method(:create_default_data_source)
if data_source_or_factory.respond_to? :call
@data_source = data_source_or_factory.call(sdk_key, config)
else
factory = @config.update_processor_factory || self.method(:create_default_update_processor)
@update_processor = factory.call(sdk_key, config)
@data_source = data_source_or_factory
end

ready = @update_processor.start
ready = @data_source.start
if wait_for_sec > 0
ok = ready.wait(wait_for_sec)
if !ok
@config.logger.error { "[LDClient] Timeout encountered waiting for LaunchDarkly client initialization" }
elsif !@update_processor.initialized?
elsif !@data_source.initialized?
@config.logger.error { "[LDClient] LaunchDarkly client initialization failed" }
end
end
Expand Down Expand Up @@ -97,7 +97,7 @@ def secure_mode_hash(user)
# Returns whether the client has been initialized and is ready to serve feature flag requests
# @return [Boolean] true if the client has been initialized
def initialized?
@config.offline? || @config.use_ldd? || @update_processor.initialized?
@config.offline? || @config.use_ldd? || @data_source.initialized?
end

#
Expand Down Expand Up @@ -270,14 +270,14 @@ def all_flags_state(user, options={})
# @return [void]
def close
@config.logger.info { "[LDClient] Closing LaunchDarkly client..." }
@update_processor.stop
@data_source.stop
@event_processor.stop
@store.stop
end

private

def create_default_update_processor(sdk_key, config)
def create_default_data_source(sdk_key, config)
if config.offline?
return NullUpdateProcessor.new
end
Expand Down
4 changes: 2 additions & 2 deletions spec/file_data_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def test_auto_reload(options)
it "evaluates simplified flag with client as expected" do
file = make_temp_file(all_properties_json)
factory = LaunchDarkly::FileDataSource.factory({ paths: file.path })
config = LaunchDarkly::Config.new(send_events: false, update_processor_factory: factory)
config = LaunchDarkly::Config.new(send_events: false, data_source: factory)
client = LaunchDarkly::LDClient.new('sdkKey', config)

begin
Expand All @@ -233,7 +233,7 @@ def test_auto_reload(options)
it "evaluates full flag with client as expected" do
file = make_temp_file(all_properties_json)
factory = LaunchDarkly::FileDataSource.factory({ paths: file.path })
config = LaunchDarkly::Config.new(send_events: false, update_processor_factory: factory)
config = LaunchDarkly::Config.new(send_events: false, data_source: factory)
client = LaunchDarkly::LDClient.new('sdkKey', config)

begin
Expand Down
8 changes: 4 additions & 4 deletions spec/ldclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
let(:offline_client) do
subject.new("secret", offline_config)
end
let(:update_processor) { LaunchDarkly::NullUpdateProcessor.new }
let(:config) { LaunchDarkly::Config.new({send_events: false, update_processor: update_processor}) }
let(:null_data) { LaunchDarkly::NullUpdateProcessor.new }
let(:config) { LaunchDarkly::Config.new({send_events: false, data_source: null_data}) }
let(:client) do
subject.new("secret", config)
end
Expand Down Expand Up @@ -357,7 +357,7 @@ def event_processor
end

describe 'with send_events: false' do
let(:config) { LaunchDarkly::Config.new({offline: true, send_events: false, update_processor: update_processor}) }
let(:config) { LaunchDarkly::Config.new({offline: true, send_events: false, data_source: null_data}) }
let(:client) { subject.new("secret", config) }

it "uses a NullEventProcessor" do
Expand All @@ -367,7 +367,7 @@ def event_processor
end

describe 'with send_events: true' do
let(:config_with_events) { LaunchDarkly::Config.new({offline: false, send_events: true, update_processor: update_processor}) }
let(:config_with_events) { LaunchDarkly::Config.new({offline: false, send_events: true, data_source: null_data}) }
let(:client_with_events) { subject.new("secret", config_with_events) }

it "does not use a NullEventProcessor" do
Expand Down

0 comments on commit 0fc71a8

Please sign in to comment.