Class: OpenAI::Internal::Poller Private
- Inherits:
-
Object
- Object
- OpenAI::Internal::Poller
- 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.
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.
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.
Class Method Summary collapse
-
.validate!(poll_interval: nil, timeout: DEFAULT_TIMEOUT) ⇒ Array(Float, nil)
private
Validate polling configuration without starting the deadline used by the eventual polling operation.
Instance Method Summary collapse
-
#check_deadline!(resource = nil) ⇒ Float?
private
Raise when the overall polling deadline has elapsed and otherwise return the time remaining.
-
#initialize(operation:, poll_interval: nil, timeout: DEFAULT_TIMEOUT) ⇒ Poller
constructor
private
A new instance of Poller.
-
#request(request_options, extra_headers: {}, resource: nil) {|options| ... } ⇒ Object
private
Run one retrieval within the remaining polling deadline.
-
#wait(resource) ⇒ void
private
Sleep until the next request, respecting both the server's polling hint and the overall polling deadline.
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.
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.
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.
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.
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(, extra_headers: {}, resource: nil, &block) remaining = check_deadline!(resource) = (, extra_headers: extra_headers, remaining: remaining) result = if remaining.nil? block.call() else Timeout.timeout(remaining) { block.call() } 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.
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 |