Class: OpenAI::Internal::Poller Private

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/internal/poller.rb,
sig/openai/internal/poller.rbs

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Shared polling behavior for long-running API resources.

Constant Summary collapse

DEFAULT_INTERVAL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (Float)
5.0
DEFAULT_TIMEOUT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (Float)
30 * 60.0
OpenAI =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (:request_opts? request_options,)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation:, poll_interval: nil, timeout: DEFAULT_TIMEOUT) ⇒ Poller

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Poller.

Parameters:

  • operation (String)
  • poll_interval (Integer, Float, nil) (defaults to: nil)
  • timeout (Integer, Float, nil) (defaults to: DEFAULT_TIMEOUT)


19
20
21
22
23
# File 'lib/openai/internal/poller.rb', line 19

def initialize(operation:, poll_interval: nil, timeout: DEFAULT_TIMEOUT)
  @operation = operation
  @poll_interval, @timeout = self.class.validate!(poll_interval: poll_interval, timeout: timeout)
  @deadline = monotonic_time + @timeout unless @timeout.nil?
end

Class Method Details

.validate!(poll_interval: nil, timeout: DEFAULT_TIMEOUT) ⇒ Array(Float, nil)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validate polling configuration without starting the deadline used by the eventual polling operation.

Composite helpers call this before making any API requests so invalid local arguments cannot leave partially-created remote resources behind.

Parameters:

  • poll_interval (Integer, Float, nil) (defaults to: nil)
  • timeout (Integer, Float, nil) (defaults to: DEFAULT_TIMEOUT)

Returns:

  • (Array(Float, nil))

    the normalized poll interval and timeout



36
37
38
39
40
41
# File 'lib/openai/internal/poller.rb', line 36

def self.validate!(poll_interval: nil, timeout: DEFAULT_TIMEOUT)
  [
    duration(poll_interval, name: :poll_interval, allow_zero: false),
    duration(timeout, name: :timeout, allow_zero: true)
  ]
end

Instance Method Details

#check_deadline!(resource = nil) ⇒ Float?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raise when the overall polling deadline has elapsed and otherwise return the time remaining. Resource helpers also call this after request timeouts to distinguish an exhausted polling deadline from a shorter caller timeout.

Parameters:

  • resource (Object, nil) (defaults to: nil)

    the last resource returned by the API

Returns:

  • (Float, nil)


107
108
109
110
111
112
# File 'lib/openai/internal/poller.rb', line 107

def check_deadline!(resource = nil)
  remaining = @deadline - monotonic_time unless @deadline.nil?
  raise_timeout(resource) if !remaining.nil? && remaining <= 0

  remaining
end

#request(request_options, extra_headers: {}, resource: nil) {|options| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run one retrieval within the remaining polling deadline. The block-level timeout covers work outside the HTTP transport, including authentication and request replays, while the postcondition catches a result that arrives as the deadline expires.

Parameters:

  • request_options (OpenAI::RequestOptions, Hash{Symbol=>Object}, nil)
  • extra_headers (Hash{String=>String, nil}) (defaults to: {})

    endpoint headers to preserve

  • resource (Object, nil) (defaults to: nil)

    the last resource returned by the API

Yield Parameters:

  • options (Hash{Symbol=>Object})

Yield Returns:

  • (Object)

Returns:

  • (Object)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/openai/internal/poller.rb', line 56

def request(request_options, extra_headers: {}, resource: nil, &block)
  remaining = check_deadline!(resource)
  options = bounded_request_options(request_options, extra_headers: extra_headers, remaining: remaining)

  result = if remaining.nil?
    block.call(options)
  else
    Timeout.timeout(remaining) { block.call(options) }
  end

  check_deadline!(result)
  result
rescue Timeout::Error
  check_deadline!(resource)
  raise
end

#wait(resource) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Sleep until the next request, respecting both the server's polling hint and the overall polling deadline.

Parameters:



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/openai/internal/poller.rb', line 121

def wait(resource)
  remaining = check_deadline!(resource)

  interval = @poll_interval || server_interval(resource) || DEFAULT_INTERVAL
  if !remaining.nil? && interval >= remaining
    sleep(remaining)
    raise_timeout(resource)
  end

  sleep(interval)
end