Class: Pindo::SimpleRetryMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/pindo/client/httpclient.rb

Overview

简单的 Faraday 重试中间件(替代 faraday-retry)

Constant Summary collapse

IDEMPOTENT_METHODS =

默认幂等的 HTTP 方法(可安全重试)

[:get, :head, :put, :delete, :options, :trace].freeze
RETRIABLE_STATUS_CODES =

可重试的 HTTP 状态码(5xx 服务器错误)

(500..599).freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ SimpleRetryMiddleware

Returns a new instance of SimpleRetryMiddleware.



18
19
20
21
22
23
24
25
26
27
# File 'lib/pindo/client/httpclient.rb', line 18

def initialize(app, options = {})
  super(app)
  @max = options[:max] || 3  # 最大重试次数(不包括首次尝试)
  @interval = options[:interval] || 0.5
  @backoff_factor = options[:backoff_factor] || 2
  @interval_randomness = options[:interval_randomness] || 0.5
  @exceptions = options[:exceptions] || [Faraday::TimeoutError, Faraday::ConnectionFailed]
  @methods = options[:methods] || IDEMPOTENT_METHODS  # 允许重试的 HTTP 方法
  @retry_statuses = options[:retry_statuses] || RETRIABLE_STATUS_CODES
end

Instance Method Details

#call(env) ⇒ Object



29
30
31
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
# File 'lib/pindo/client/httpclient.rb', line 29

def call(env)
  retries = 0

  loop do
    begin
      response = @app.call(env)

      # 检查 HTTP 状态码是否需要重试(5xx 错误)
      # 同时必须满足幂等性要求,避免非幂等请求(如 POST)被意外重试
      if retriable_response?(response) && retriable_request?(env) && retries < @max
        retries += 1
        sleep_with_jitter(retries)
        next  # 继续下一次循环(重试)
      end

      return response  # 成功,返回响应

    rescue *@exceptions => e
      # 检查是否为幂等方法,非幂等方法不自动重试
      if retriable_request?(env) && retries < @max
        retries += 1
        sleep_with_jitter(retries)
        next  # 继续下一次循环(重试)
      else
        raise e  # 不能重试,抛出异常
      end
    end
  end
end