Class: LaunchDarkly::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/config.rb

Overview

This class exposes advanced configuration options for the LaunchDarkly client library. Most users will not need to use a custom configuration– the default configuration sets sane defaults for most use cases.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ type

Constructor for creating custom LaunchDarkly configurations.

rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity

Parameters:

  • opts (Hash) (defaults to: {})

    the configuration options

Options Hash (opts):

  • :logger (Logger)

    A logger to use for messages from the LaunchDarkly client. Defaults to the Rails logger in a Rails environment, or stdout otherwise.

  • :base_uri (String) — default: "https://app.launchdarkly.com"

    The base URL for the LaunchDarkly server. Most users should use the default value.

  • :stream_uri (String) — default: "https://stream.launchdarkly.com"

    The URL for the LaunchDarkly streaming events server. Most users should use the default value.

  • :events_uri (String) — default: "https://events.launchdarkly.com"

    The URL for the LaunchDarkly events server. Most users should use the default value.

  • :capacity (Integer) — default: 10000

    The capacity of the events buffer. The client buffers up to this many events in memory before flushing. If the capacity is exceeded before the buffer is flushed, events will be discarded.

  • :flush_interval (Float) — default: 30

    The number of seconds between flushes of the event buffer.

  • :read_timeout (Float) — default: 10

    The read timeout for network connections in seconds.

  • :connect_timeout (Float) — default: 2

    The connect timeout for network connections in seconds.

  • :cache_store (Object)

    A cache store for the Faraday HTTP caching library. Defaults to the Rails cache in a Rails environment, or a thread-safe in-memory store otherwise.

  • :offline (Boolean) — default: false

    Whether the client should be initialized in offline mode. In offline mode, default values are returned for all flags and no remote network requests are made.

  • :poll_interval (Float) — default: 1

    The number of seconds between polls for flag updates if streaming is off.

  • :stream (Boolean) — default: true

    Whether or not the streaming API should be used to receive flag updates.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ldclient-rb/config.rb', line 46

def initialize(opts = {})
  @base_uri = (opts[:base_uri] || Config.default_base_uri).chomp("/")
  @stream_uri = (opts[:stream_uri] || Config.default_stream_uri).chomp("/")
  @events_uri = (opts[:events_uri] || Config.default_events_uri).chomp("/")
  @capacity = opts[:capacity] || Config.default_capacity
  @logger = opts[:logger] || Config.default_logger
  @cache_store = opts[:cache_store] || Config.default_cache_store
  @flush_interval = opts[:flush_interval] || Config.default_flush_interval
  @connect_timeout = opts[:connect_timeout] || Config.default_connect_timeout
  @read_timeout = opts[:read_timeout] || Config.default_read_timeout
  @feature_store = opts[:feature_store] || Config.default_feature_store
  @stream = opts.has_key?(:stream) ? opts[:stream] : Config.default_stream
  @offline = opts.has_key?(:offline) ? opts[:offline] : Config.default_offline
  @poll_interval = opts.has_key?(:poll_interval) && opts[:poll_interval] > 1 ? opts[:poll_interval] : Config.default_poll_interval
  @proxy = opts[:proxy] || Config.default_proxy
end

Instance Attribute Details

#base_uriString (readonly)

The base URL for the LaunchDarkly server.

Returns:

  • (String)

    The configured base URL for the LaunchDarkly server.



67
68
69
# File 'lib/ldclient-rb/config.rb', line 67

def base_uri
  @base_uri
end

#cache_storeObject (readonly)

The store for the Faraday HTTP caching library. Stores should respond to ‘read’ and ‘write’ requests.

Returns:

  • (Object)

    The configured store for the Faraday HTTP caching library.



129
130
131
# File 'lib/ldclient-rb/config.rb', line 129

def cache_store
  @cache_store
end

#capacityInteger (readonly)

The capacity of the events buffer. The client buffers up to this many events in memory before flushing. If the capacity is exceeded before the buffer is flushed, events will be discarded. Increasing the capacity means that events are less likely to be discarded, at the cost of consuming more memory.

Returns:

  • (Integer)

    The configured capacity of the event buffer



122
123
124
# File 'lib/ldclient-rb/config.rb', line 122

def capacity
  @capacity
end

#connect_timeoutFloat (readonly)

The connect timeout for network connections in seconds.

Returns:

  • (Float)

    The connect timeout in seconds.



141
142
143
# File 'lib/ldclient-rb/config.rb', line 141

def connect_timeout
  @connect_timeout
end

#events_uriString (readonly)

The base URL for the LaunchDarkly events server.

Returns:

  • (String)

    The configured base URL for the LaunchDarkly events server.



79
80
81
# File 'lib/ldclient-rb/config.rb', line 79

def events_uri
  @events_uri
end

#feature_storeObject (readonly)

A store for feature flag configuration rules.



146
147
148
# File 'lib/ldclient-rb/config.rb', line 146

def feature_store
  @feature_store
end

#flush_intervalFloat (readonly)

The number of seconds between flushes of the event buffer. Decreasing the flush interval means that the event buffer is less likely to reach capacity.

Returns:

  • (Float)

    The configured number of seconds between flushes of the event buffer.



100
101
102
# File 'lib/ldclient-rb/config.rb', line 100

def flush_interval
  @flush_interval
end

#loggerLogger (readonly)

The configured logger for the LaunchDarkly client. The client library uses the log to print warning and error messages.

Returns:

  • (Logger)

    The configured logger



112
113
114
# File 'lib/ldclient-rb/config.rb', line 112

def logger
  @logger
end

#poll_intervalObject (readonly)

The number of seconds to wait before polling for feature flag updates. This option has no effect unless streaming is disabled



105
106
107
# File 'lib/ldclient-rb/config.rb', line 105

def poll_interval
  @poll_interval
end

#proxyObject (readonly)

The proxy configuration string



151
152
153
# File 'lib/ldclient-rb/config.rb', line 151

def proxy
  @proxy
end

#read_timeoutFloat (readonly)

The read timeout for network connections in seconds.

Returns:

  • (Float)

    The read timeout in seconds.



135
136
137
# File 'lib/ldclient-rb/config.rb', line 135

def read_timeout
  @read_timeout
end

#stream_uriString (readonly)

The base URL for the LaunchDarkly streaming server.

Returns:

  • (String)

    The configured base URL for the LaunchDarkly streaming server.



73
74
75
# File 'lib/ldclient-rb/config.rb', line 73

def stream_uri
  @stream_uri
end

Class Method Details

.defaultConfig

The default LaunchDarkly client configuration. This configuration sets reasonable defaults for most users.

Returns:

  • (Config)

    The default LaunchDarkly configuration.



158
159
160
# File 'lib/ldclient-rb/config.rb', line 158

def self.default
  Config.new
end

.default_base_uriObject



166
167
168
# File 'lib/ldclient-rb/config.rb', line 166

def self.default_base_uri
  "https://app.launchdarkly.com"
end

.default_cache_storeObject



178
179
180
# File 'lib/ldclient-rb/config.rb', line 178

def self.default_cache_store
  defined?(Rails) && Rails.respond_to?(:cache) ? Rails.cache : ThreadSafeMemoryStore.new
end

.default_capacityObject



162
163
164
# File 'lib/ldclient-rb/config.rb', line 162

def self.default_capacity
  10000
end

.default_connect_timeoutObject



190
191
192
# File 'lib/ldclient-rb/config.rb', line 190

def self.default_connect_timeout
  2
end

.default_events_uriObject



174
175
176
# File 'lib/ldclient-rb/config.rb', line 174

def self.default_events_uri
  "https://events.launchdarkly.com"
end

.default_feature_storeObject



212
213
214
# File 'lib/ldclient-rb/config.rb', line 212

def self.default_feature_store
  InMemoryFeatureStore.new
end

.default_flush_intervalObject



182
183
184
# File 'lib/ldclient-rb/config.rb', line 182

def self.default_flush_interval
  10
end

.default_loggerObject



198
199
200
201
202
203
204
205
206
# File 'lib/ldclient-rb/config.rb', line 198

def self.default_logger
  if defined?(Rails) && Rails.respond_to?(:logger)
    Rails.logger 
  else 
    log = ::Logger.new($stdout)
    log.level = ::Logger::WARN
    log
  end
end

.default_offlineObject



216
217
218
# File 'lib/ldclient-rb/config.rb', line 216

def self.default_offline
  false
end

.default_poll_intervalObject



220
221
222
# File 'lib/ldclient-rb/config.rb', line 220

def self.default_poll_interval
  1
end

.default_proxyObject



194
195
196
# File 'lib/ldclient-rb/config.rb', line 194

def self.default_proxy
  nil
end

.default_read_timeoutObject



186
187
188
# File 'lib/ldclient-rb/config.rb', line 186

def self.default_read_timeout
  10
end

.default_streamObject



208
209
210
# File 'lib/ldclient-rb/config.rb', line 208

def self.default_stream
  true
end

.default_stream_uriObject



170
171
172
# File 'lib/ldclient-rb/config.rb', line 170

def self.default_stream_uri
  "https://stream.launchdarkly.com"
end

Instance Method Details

#offline?Boolean

TODO docs

Returns:

  • (Boolean)


91
92
93
# File 'lib/ldclient-rb/config.rb', line 91

def offline?
  @offline
end

#stream?Boolean

Whether streaming mode should be enabled. Streaming mode asynchronously updates feature flags in real-time using server-sent events.

Returns:

  • (Boolean)

    True if streaming mode should be enabled



86
87
88
# File 'lib/ldclient-rb/config.rb', line 86

def stream?
  @stream
end