diff --git a/lib/ldclient-rb/config.rb b/lib/ldclient-rb/config.rb index bf795218..5ab5091b 100644 --- a/lib/ldclient-rb/config.rb +++ b/lib/ldclient-rb/config.rb @@ -15,7 +15,9 @@ class Config # @option 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. # @option opts [String] :base_uri ("https://app.launchdarkly.com") The base URL for the LaunchDarkly server. Most users should use the default value. # @option opts [Integer] :capacity (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. - # @option opts [Integer] :flush_interval (30) The number of seconds between flushes of the event buffer. + # @option opts [Float] :flush_interval (30) The number of seconds between flushes of the event buffer. + # @option opts [Float] :read_timeout (10) The read timeout for network connections in seconds. + # @option opts [Float] :connect_timeout (2) The connect timeout for network connections in seconds. # @option opts [Object] :store 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. # # @return [type] [description] @@ -25,6 +27,8 @@ def initialize(opts = {}) @logger = opts[:logger] || Config.default_logger @store = opts[:store] || Config.default_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 end # @@ -39,7 +43,7 @@ def base_uri # 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. # - # @return [Integer] The configured number of seconds between flushes of the event buffer. + # @return [Float] The configured number of seconds between flushes of the event buffer. def flush_interval @flush_interval end @@ -70,6 +74,22 @@ def store @store end + # + # The read timeout for network connections in seconds. + # + # @return [Float] The read timeout in seconds. + def read_timeout + @read_timeout + end + + # + # The connect timeout for network connections in seconds. + # + # @return [Float] The connect timeout in seconds. + def connect_timeout + @connect_timeout + end + # # The default LaunchDarkly client configuration. This configuration sets reasonable defaults for most users. # @@ -94,6 +114,14 @@ def self.default_flush_interval 10 end + def self.default_read_timeout + 10 + end + + def self.default_connect_timeout + 2 + end + def self.default_logger defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : ::Logger.new($stdout) end diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index 60cd33e8..420ece6d 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -52,6 +52,8 @@ def create_worker() req.headers['User-Agent'] = 'RubyClient/' + LaunchDarkly::VERSION req.headers['Content-Type'] = 'application/json' req.body = events.to_json + req.options.timeout = @config.read_timeout + req.options.open_timeout = @config.connect_timeout end if res.status != 200 @config.logger.error("[LDClient] Unexpected status code while processing events: #{res.status}") @@ -143,6 +145,8 @@ def get_flag_int(key, user, default) @client.get (@config.base_uri + '/api/eval/features/' + key) do |req| req.headers['Authorization'] = 'api_key ' + @api_key req.headers['User-Agent'] = 'RubyClient/' + LaunchDarkly::VERSION + req.options.timeout = @config.read_timeout + req.options.open_timeout = @config.connect_timeout end if res.status == 401