Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Share one open timeout across the checked addresses

Guido Boyke9a618b7b1950
diff --git a/app/services/lalabase/llm/public_endpoint_adapter.rb b/app/services/lalabase/llm/public_endpoint_adapter.rb
index 48de633..bac2971 100644
--- a/app/services/lalabase/llm/public_endpoint_adapter.rb
+++ b/app/services/lalabase/llm/public_endpoint_adapter.rb
@@ -36,6 +36,14 @@ module Lalabase
# sent twice. On failure #connect closes the socket it opened. #connect is
# private, but it is the seam both the net-http gem in the bundle (0.6.0) and the
# one in Ruby 4.0 share; the helper below it differs between the two.
+ #
+ # All attempts share ONE open timeout, as all addresses of a name did before.
+ # Restarting it per address would turn the client's budget (120 seconds from
+ # ruby-openai) into one per address, and a worker would hang N times as long on
+ # an endpoint that drops packets. Each attempt gets what is left; the configured
+ # value is restored afterwards for any later reconnect. Since #connect also does
+ # the TLS handshake, the handshake now draws on the same budget: stricter than
+ # before, never longer.
module PinnedAddresses
CONNECT_ERRORS = [SystemCallError, IO::TimeoutError, Net::OpenTimeout].freeze
@@ -44,15 +52,28 @@ module Lalabase
private
def connect
- *earlier, last = pinned_addresses
- earlier.each do |address|
+ budget = open_timeout
+ deadline = budget && (monotonic_now + budget)
+ pinned_addresses.each_with_index do |address, index|
self.ipaddr = address
+ self.open_timeout = remaining_until(deadline) if deadline
return super
rescue *CONNECT_ERRORS
- next
+ raise if index == pinned_addresses.size - 1
end
- self.ipaddr = last
- super
+ ensure
+ self.open_timeout = budget
+ end
+
+ def remaining_until(deadline)
+ remaining = deadline - monotonic_now
+ raise Net::OpenTimeout, 'the open timeout is spent' unless remaining.positive?
+
+ remaining
+ end
+
+ def monotonic_now
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
end
diff --git a/spec/services/lalabase/llm/public_endpoint_adapter_spec.rb b/spec/services/lalabase/llm/public_endpoint_adapter_spec.rb
index 2d10454..27488e3 100644
--- a/spec/services/lalabase/llm/public_endpoint_adapter_spec.rb
+++ b/spec/services/lalabase/llm/public_endpoint_adapter_spec.rb
@@ -103,6 +103,25 @@ RSpec.describe Lalabase::Llm::PublicEndpointAdapter, :ci_guard do
expect(attempts).to eq(['2001:db8::1', '203.0.113.20'])
end
+ # With the name, every address shared one open timeout. Restarting it for each
+ # checked address would turn the client's budget into one per address, and a
+ # worker thread would hang N times as long on an endpoint that drops packets.
+ it 'shares one open timeout across all checked addresses' do
+ resolve_names('dual.provider.example' => ['203.0.113.20', '203.0.113.21', '203.0.113.22'])
+ allow(TCPSocket).to receive(:open) do |address, *|
+ attempts << address
+ sleep 5
+ end
+
+ http = connection_for("http://dual.provider.example:#{server.addr[1]}/v1")
+ http.open_timeout = 0.3
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+
+ expect { http.start(&:started?) }.to raise_error(Net::OpenTimeout)
+ expect(Process.clock_gettime(Process::CLOCK_MONOTONIC) - started).to be < 0.6
+ expect(http.open_timeout).to eq(0.3)
+ end
+
# Once connected, the request may have been written. A failure from there on must
# not go to the next address: a chat request would run, and be billed, twice.
it 'does not try the next address when the request fails after the connect' do