Class: MaxApiClient::Polling

Inherits:
Object
  • Object
show all
Defined in:
lib/max_api_client/polling.rb,
sig/max_api_client.rbs

Overview

Long-polling helper over GET /updates with marker tracking and retries.

Constant Summary collapse

DEFAULT_TIMEOUT =

Returns:

  • (Integer)
20
DEFAULT_RETRY_INTERVAL =

Returns:

  • (Integer)
5
READ_TIMEOUT_PADDING =

Returns:

  • (Integer)
5
RETRYABLE_ERRORS =
[
  Net::OpenTimeout,
  Net::ReadTimeout,
  EOFError,
  IOError,
  SocketError,
  Errno::ECONNRESET
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(api, types: [], marker: nil, timeout: DEFAULT_TIMEOUT, retry_interval: DEFAULT_RETRY_INTERVAL, read_timeout: nil) ⇒ Polling

rubocop:disable Metrics/ParameterLists

Parameters:

  • api (Api)
  • types: (Array[String], String) (defaults to: [])
  • marker: (Integer, nil) (defaults to: nil)
  • timeout: (Integer) (defaults to: DEFAULT_TIMEOUT)
  • retry_interval: (Integer) (defaults to: DEFAULT_RETRY_INTERVAL)
  • read_timeout: (Numeric, nil) (defaults to: nil)


19
20
21
22
23
24
25
26
27
28
# File 'lib/max_api_client/polling.rb', line 19

def initialize(api, types: [], marker: nil, timeout: DEFAULT_TIMEOUT, retry_interval: DEFAULT_RETRY_INTERVAL,
               read_timeout: nil)
  @api = api
  @types = types
  @marker = marker
  @timeout = timeout
  @retry_interval = retry_interval
  @read_timeout = read_timeout || (timeout.to_i + READ_TIMEOUT_PADDING)
  @stopped = false
end

Instance Method Details

#each {|update| ... } ⇒ self

rubocop:enable Metrics/ParameterLists

Yields:

Yield Parameters:

  • update (Object)

Yield Returns:

  • (void)

Returns:

  • (self)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/max_api_client/polling.rb', line 31

def each
  return enum_for(:each) unless block_given?

  until stopped?
    begin
      response = fetch_updates
      @marker = fetch_value(response, :marker)

      Array(fetch_value(response, :updates)).each do |update|
        break if stopped?

        yield update
      end
    rescue *RETRYABLE_ERRORS
      retry_later
    rescue ApiError => e
      raise unless retryable_status?(e.status)

      retry_later
    end
  end

  self
end

#stopBoolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/max_api_client/polling.rb', line 56

def stop
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/max_api_client/polling.rb', line 60

def stopped?
  @stopped
end