From 6a973f5db5d50c71e2743db2440c1cf5e9871aef Mon Sep 17 00:00:00 2001 From: Yuri Levenhagen Date: Fri, 4 Feb 2022 11:42:38 -0500 Subject: [PATCH 1/2] Distinguish connection timeouts --- lib/http/errors.rb | 3 +++ lib/http/timeout/global.rb | 2 +- lib/http/timeout/per_operation.rb | 2 +- spec/lib/http/client_spec.rb | 4 ++-- spec/support/http_handling_shared.rb | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/http/errors.rb b/lib/http/errors.rb index 9a685f80..846f5788 100644 --- a/lib/http/errors.rb +++ b/lib/http/errors.rb @@ -19,6 +19,9 @@ class StateError < ResponseError; end # Generic Timeout error class TimeoutError < Error; end + # Timeout when first establishing the conncetion + class ConnectionTimeoutError < TimeoutError; end + # Header value is of unexpected format (similar to Net::HTTPHeaderSyntaxError) class HeaderError < Error; end end diff --git a/lib/http/timeout/global.rb b/lib/http/timeout/global.rb index dd099b97..106f4f3f 100644 --- a/lib/http/timeout/global.rb +++ b/lib/http/timeout/global.rb @@ -21,7 +21,7 @@ def reset_counter def connect(socket_class, host, port, nodelay = false) reset_timer - ::Timeout.timeout(@time_left, TimeoutError) do + ::Timeout.timeout(@time_left, ConnectionTimeoutError) do @socket = socket_class.open(host, port) @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay end diff --git a/lib/http/timeout/per_operation.rb b/lib/http/timeout/per_operation.rb index bc171ee0..fae12f24 100644 --- a/lib/http/timeout/per_operation.rb +++ b/lib/http/timeout/per_operation.rb @@ -20,7 +20,7 @@ def initialize(*args) end def connect(socket_class, host, port, nodelay = false) - ::Timeout.timeout(@connect_timeout, TimeoutError) do + ::Timeout.timeout(@connect_timeout, ConnectionTimeoutError) do @socket = socket_class.open(host, port) @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay end diff --git a/spec/lib/http/client_spec.rb b/spec/lib/http/client_spec.rb index 0f54f12f..f968aeac 100644 --- a/spec/lib/http/client_spec.rb +++ b/spec/lib/http/client_spec.rb @@ -389,8 +389,8 @@ def on_error(request, error) client.use(:test_feature => feature_instance). timeout(0.001). request(:post, sleep_url) - end.to raise_error(HTTP::TimeoutError) - expect(feature_instance.captured_error).to be_a(HTTP::TimeoutError) + end.to raise_error(HTTP::ConnectionTimeoutError) + expect(feature_instance.captured_error).to be_a(HTTP::ConnectionTimeoutError) end end end diff --git a/spec/support/http_handling_shared.rb b/spec/support/http_handling_shared.rb index b01d366d..953a7c85 100644 --- a/spec/support/http_handling_shared.rb +++ b/spec/support/http_handling_shared.rb @@ -77,7 +77,7 @@ sleep 1.25 end - expect { response }.to raise_error(HTTP::TimeoutError, /execution/) + expect { response }.to raise_error(HTTP::ConnectionTimeoutError, /execution/) end it "errors if reading takes too long" do From 6e8b1ac6c37cdad5f72e882f59822601a93ed2e6 Mon Sep 17 00:00:00 2001 From: Yuri Levenhagen Date: Wed, 9 Feb 2022 13:29:18 -0500 Subject: [PATCH 2/2] Rename error as ConnectTimeoutError --- lib/http/errors.rb | 2 +- lib/http/timeout/global.rb | 2 +- lib/http/timeout/per_operation.rb | 2 +- spec/lib/http/client_spec.rb | 4 ++-- spec/support/http_handling_shared.rb | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/http/errors.rb b/lib/http/errors.rb index 846f5788..f722a478 100644 --- a/lib/http/errors.rb +++ b/lib/http/errors.rb @@ -20,7 +20,7 @@ class StateError < ResponseError; end class TimeoutError < Error; end # Timeout when first establishing the conncetion - class ConnectionTimeoutError < TimeoutError; end + class ConnectTimeoutError < TimeoutError; end # Header value is of unexpected format (similar to Net::HTTPHeaderSyntaxError) class HeaderError < Error; end diff --git a/lib/http/timeout/global.rb b/lib/http/timeout/global.rb index 106f4f3f..1148aef9 100644 --- a/lib/http/timeout/global.rb +++ b/lib/http/timeout/global.rb @@ -21,7 +21,7 @@ def reset_counter def connect(socket_class, host, port, nodelay = false) reset_timer - ::Timeout.timeout(@time_left, ConnectionTimeoutError) do + ::Timeout.timeout(@time_left, ConnectTimeoutError) do @socket = socket_class.open(host, port) @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay end diff --git a/lib/http/timeout/per_operation.rb b/lib/http/timeout/per_operation.rb index fae12f24..9274325e 100644 --- a/lib/http/timeout/per_operation.rb +++ b/lib/http/timeout/per_operation.rb @@ -20,7 +20,7 @@ def initialize(*args) end def connect(socket_class, host, port, nodelay = false) - ::Timeout.timeout(@connect_timeout, ConnectionTimeoutError) do + ::Timeout.timeout(@connect_timeout, ConnectTimeoutError) do @socket = socket_class.open(host, port) @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay end diff --git a/spec/lib/http/client_spec.rb b/spec/lib/http/client_spec.rb index f968aeac..31762404 100644 --- a/spec/lib/http/client_spec.rb +++ b/spec/lib/http/client_spec.rb @@ -389,8 +389,8 @@ def on_error(request, error) client.use(:test_feature => feature_instance). timeout(0.001). request(:post, sleep_url) - end.to raise_error(HTTP::ConnectionTimeoutError) - expect(feature_instance.captured_error).to be_a(HTTP::ConnectionTimeoutError) + end.to raise_error(HTTP::ConnectTimeoutError) + expect(feature_instance.captured_error).to be_a(HTTP::ConnectTimeoutError) end end end diff --git a/spec/support/http_handling_shared.rb b/spec/support/http_handling_shared.rb index 953a7c85..ff13b0a3 100644 --- a/spec/support/http_handling_shared.rb +++ b/spec/support/http_handling_shared.rb @@ -77,7 +77,7 @@ sleep 1.25 end - expect { response }.to raise_error(HTTP::ConnectionTimeoutError, /execution/) + expect { response }.to raise_error(HTTP::ConnectTimeoutError, /execution/) end it "errors if reading takes too long" do