From f3c2a917c3ac9118fbcfdd7e0e6628df4e951274 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Wed, 20 Aug 2025 15:48:39 -0400 Subject: [PATCH 1/2] feat: Add ability to disable retries --- lib/ld-eventsource/client.rb | 8 +++- spec/client_spec.rb | 86 ++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/lib/ld-eventsource/client.rb b/lib/ld-eventsource/client.rb index 328c8cf..939a6ca 100644 --- a/lib/ld-eventsource/client.rb +++ b/lib/ld-eventsource/client.rb @@ -89,6 +89,8 @@ class Client # an Array, it will be converted to JSON and sent as the request body. A string will be sent as a non-JSON # request body. If payload responds to #call, it will be invoked on each # request to generate the payload dynamically. + # @param retry_enabled [Boolean] (true) whether to retry connections after failures. If false, the client + # will exit after the first connection failure instead of attempting to reconnect. # @yieldparam [Client] client the new client instance, before opening the connection # def initialize(uri, @@ -102,9 +104,11 @@ def initialize(uri, logger: nil, socket_factory: nil, method: "GET", - payload: nil) + payload: nil, + retry_enabled: true) @uri = URI(uri) @stopped = Concurrent::AtomicBoolean.new(false) + @retry_enabled = retry_enabled @headers = headers.clone @connect_timeout = connect_timeout @@ -256,6 +260,8 @@ def run_stream rescue StandardError => e log_and_dispatch_error(e, "Unexpected error while closing stream") end + + return unless @retry_enabled end end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 636b1dc..12fde0d 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -737,4 +737,90 @@ def test_object.to_s end end end + + describe "retry parameter" do + it "defaults to true (retries enabled)" do + events_body = simple_event_1_text + with_server do |server| + attempt = 0 + server.setup_response("/") do |req,res| + attempt += 1 + if attempt == 1 + res.status = 500 + res.body = "server error" + res.keep_alive = false + else + send_stream_content(res, events_body, keep_open: true) + end + end + + event_sink = Queue.new + error_sink = Queue.new + client = subject.new(server.base_uri, reconnect_time: reconnect_asap) do |c| + c.on_event { |event| event_sink << event } + c.on_error { |error| error_sink << error } + end + + with_client(client) do |c| + expect(event_sink.pop).to eq(simple_event_1) + expect(error_sink.pop).to eq(SSE::Errors::HTTPStatusError.new(500, "server error")) + expect(attempt).to eq 2 # Should have retried + end + end + end + + it "allows retries when retry_enabled: true" do + events_body = simple_event_1_text + with_server do |server| + attempt = 0 + server.setup_response("/") do |req,res| + attempt += 1 + if attempt == 1 + res.status = 500 + res.body = "server error" + res.keep_alive = false + else + send_stream_content(res, events_body, keep_open: true) + end + end + + event_sink = Queue.new + error_sink = Queue.new + client = subject.new(server.base_uri, reconnect_time: reconnect_asap, retry_enabled: true) do |c| + c.on_event { |event| event_sink << event } + c.on_error { |error| error_sink << error } + end + + with_client(client) do |c| + expect(event_sink.pop).to eq(simple_event_1) + expect(error_sink.pop).to eq(SSE::Errors::HTTPStatusError.new(500, "server error")) + expect(attempt).to eq 2 # Should have retried + end + end + end + + it "disables retries when retry_enabled: false" do + with_server do |server| + attempt = 0 + server.setup_response("/") do |req,res| + attempt += 1 + res.status = 500 + res.body = "server error" + res.keep_alive = false + end + + error_sink = Queue.new + client = subject.new(server.base_uri, retry_enabled: false) do |c| + c.on_error { |error| error_sink << error } + end + + # Give the client some time to attempt connection and fail + sleep(0.5) + client.close + + expect(error_sink.pop).to eq(SSE::Errors::HTTPStatusError.new(500, "server error")) + expect(attempt).to eq 1 # Should not have retried + end + end + end end From 25262b829669e754d32dff5033f2d91e290853a3 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Wed, 20 Aug 2025 16:30:56 -0400 Subject: [PATCH 2/2] Shutdown client if retry is disabled --- lib/ld-eventsource/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ld-eventsource/client.rb b/lib/ld-eventsource/client.rb index 939a6ca..2e388fe 100644 --- a/lib/ld-eventsource/client.rb +++ b/lib/ld-eventsource/client.rb @@ -261,7 +261,7 @@ def run_stream log_and_dispatch_error(e, "Unexpected error while closing stream") end - return unless @retry_enabled + return close unless @retry_enabled end end