lib/ably/rest/client.rb in ably-1.0.4 vs lib/ably/rest/client.rb in ably-1.0.5

- old
+ new

@@ -81,10 +81,14 @@ # The list of fallback hosts to be used by this client # if empty or nil then fallback host functionality is disabled attr_reader :fallback_hosts + # Whethere the {Client} has to add a random identifier to the path of a request + # @return [Boolean] + attr_reader :add_request_ids + # Creates a {Ably::Rest::Client Rest Client} and configures the {Ably::Auth} object for the connection. # # @param [Hash,String] options an options Hash used to configure the client and the authentication, or String with an API key or Token ID # @option options [Boolean] :tls (true) When false, TLS is disabled. Please note Basic Auth is disallowed without TLS as secrets cannot be transmitted over unsecured connections. # @option options [String] :key API key comprising the key name and key secret in a single string @@ -144,10 +148,11 @@ @log_level = options.delete(:log_level) || ::Logger::WARN @custom_logger = options.delete(:logger) @custom_host = options.delete(:rest_host) @custom_port = options.delete(:port) @custom_tls_port = options.delete(:tls_port) + @add_request_ids = options.delete(:add_request_ids) if options[:fallback_hosts_use_default] && options[:fallback_jhosts] raise ArgumentError, "fallback_hosts_use_default cannot be set to trye when fallback_jhosts is also provided" end @fallback_hosts = case @@ -432,15 +437,29 @@ def send_request(method, path, params, options) max_retry_count = http_defaults.fetch(:max_retry_count) max_retry_duration = http_defaults.fetch(:max_retry_duration) requested_at = Time.now retry_count = 0 + request_id = nil + if add_request_ids + params = if params.nil? + {} + else + params.dup + end + request_id = SecureRandom.urlsafe_base64(10) + params[:request_id] = request_id + end begin use_fallback = can_fallback_to_alternate_ably_host? && retry_count > 0 connection(use_fallback: use_fallback).send(method, path, params) do |request| + if add_request_ids + request.options.context = {} if request.options.context.nil? + request.options.context[:request_id] = request_id + end unless options[:send_auth_header] == false request.headers[:authorization] = auth.auth_header if options[:headers] options[:headers].map do |key, val| request.headers[key] = val @@ -454,15 +473,15 @@ if can_fallback_to_alternate_ably_host? && retry_count < max_retry_count && time_passed <= max_retry_duration retry_count += 1 logger.warn { "Ably::Rest::Client - Retry #{retry_count} for #{method} #{path} #{params} as initial attempt failed: #{error}" } retry end - case error when Faraday::TimeoutError - raise Ably::Exceptions::ConnectionTimeout.new(error.message, nil, 80014, error) + raise Ably::Exceptions::ConnectionTimeout.new(error.message, nil, 80014, error, { request_id: request_id }) when Faraday::ClientError - raise Ably::Exceptions::ConnectionError.new(error.message, nil, 80000, error) + # request_id is also available in the request context + raise Ably::Exceptions::ConnectionError.new(error.message, nil, 80000, error, { request_id: request_id }) else raise error end end end