Method: LaunchDarkly::LDClient#initialize

Defined in:
lib/ldclient-rb/ldclient.rb

#initialize(sdk_key, config = Config.default, wait_for_sec = 5) ⇒ LDClient

Creates a new client instance that connects to LaunchDarkly. A custom configuration parameter can also supplied to specify advanced options, but for most use cases, the default configuration is appropriate.

The client will immediately attempt to connect to LaunchDarkly and retrieve your feature flag data. If it cannot successfully do so within the time limit specified by wait_for_sec, the constructor will return a client that is in an uninitialized state. See #initialized? for more details.

Parameters:

  • sdk_key (String)

    the SDK key for your LaunchDarkly account

  • config (Config) (defaults to: Config.default)

    an optional client configuration object

  • wait_for_sec (Float) (defaults to: 5)

    maximum time (in seconds) to wait for initialization



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ldclient-rb/ldclient.rb', line 32

def initialize(sdk_key, config = Config.default, wait_for_sec = 5)
  @sdk_key = sdk_key

  # We need to wrap the feature store object with a FeatureStoreClientWrapper in order to add
  # some necessary logic around updates. Unfortunately, we have code elsewhere that accesses
  # the feature store through the Config object, so we need to make a new Config that uses
  # the wrapped store.
  @store = Impl::FeatureStoreClientWrapper.new(config.feature_store)
  updated_config = config.clone
  updated_config.instance_variable_set(:@feature_store, @store)
  @config = updated_config

  if @config.offline? || !@config.send_events
    @event_processor = NullEventProcessor.new
  else
    @event_processor = EventProcessor.new(sdk_key, config)
  end

  if @config.use_ldd?
    @config.logger.info { "[LDClient] Started LaunchDarkly Client in LDD mode" }
    return  # requestor and update processor are not used in this mode
  end

  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
    @data_source = data_source_or_factory
  end

  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 !@data_source.initialized?
      @config.logger.error { "[LDClient] LaunchDarkly client initialization failed" }
    end
  end
end