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

- old
+ new

@@ -437,26 +437,19 @@ 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 + retry_sequence_id = nil + request_id = SecureRandom.urlsafe_base64(10) if add_request_ids 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.params[:request_id] = request_id 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 @@ -464,19 +457,34 @@ options[:headers].map do |key, val| request.headers[key] = val end end end + end.tap do + if retry_count > 0 + logger.warn do + "Ably::Rest::Client - Request SUCCEEDED after #{retry_count} #{retry_count > 1 ? 'retries' : 'retry' } for" \ + " #{method} #{path} #{params} (seq ##{retry_sequence_id}, time elapsed #{(Time.now.to_f - requested_at.to_f).round(2)}s)" + end + end end rescue Faraday::TimeoutError, Faraday::ClientError, Ably::Exceptions::ServerError => error + retry_sequence_id ||= SecureRandom.urlsafe_base64(4) time_passed = Time.now - requested_at + 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}" } + logger.warn { "Ably::Rest::Client - Retry #{retry_count} for #{method} #{path} #{params} as initial attempt failed (seq ##{retry_sequence_id}): #{error}" } retry end + + logger.error do + "Ably::Rest::Client - Request FAILED after #{retry_count} #{retry_count > 1 ? 'retries' : 'retry' } for" \ + " #{method} #{path} #{params} (seq ##{retry_sequence_id}, time elapsed #{(Time.now.to_f - requested_at.to_f).round(2)}s)" + end + case error when Faraday::TimeoutError raise Ably::Exceptions::ConnectionTimeout.new(error.message, nil, 80014, error, { request_id: request_id }) when Faraday::ClientError # request_id is also available in the request context @@ -547,10 +555,10 @@ builder.use Ably::Rest::Middleware::Exceptions setup_incoming_middleware builder, logger, fail_if_unsupported_mime_type: true # Set Faraday's HTTP adapter - builder.adapter Faraday.default_adapter + builder.adapter :excon end end def can_fallback_to_alternate_ably_host? fallback_hosts && !fallback_hosts.empty?