Skip to content

Commit

Permalink
support nil timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
dpep committed Nov 17, 2023
1 parent 2ebfb42 commit 2ccb90e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/network_resiliency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def record(adapter:, action:, destination:, duration:, error:, timeout:, attempt
adapter: adapter,
destination: destination,
},
)
) if timeout && timeout > 0

if error
NetworkResiliency.statsd&.distribution(
Expand All @@ -145,7 +145,7 @@ def record(adapter:, action:, destination:, duration:, error:, timeout:, attempt
adapter: adapter,
destination: destination,
},
) if timeout
) if timeout && timeout > 0
else
# track successful retries
NetworkResiliency.statsd&.increment(
Expand Down
2 changes: 1 addition & 1 deletion lib/network_resiliency/adapter/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def connect
destination: address,
error: error,
duration: ts,
timeout: self.open_timeout * 1_000,
timeout: self.open_timeout.to_f * 1_000,
attempts: attempts,
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/network_resiliency/adapter/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def connect(_, _, host, *args)
destination: host,
error: e&.class,
duration: ts,
timeout: query_options[:connect_timeout] * 1_000,
timeout: query_options[:connect_timeout].to_f * 1_000,
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/network_resiliency/adapter/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def establish_connection
destination: host,
duration: ts,
error: error,
timeout: @options[:connect_timeout] * 1_000,
timeout: @options[:connect_timeout].to_f * 1_000,
attempts: attempts,
)
end
Expand Down
24 changes: 22 additions & 2 deletions spec/mysql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

before do
described_class.patch
allow(NetworkResiliency).to receive(:record)
allow(NetworkResiliency).to receive(:record).and_call_original
end

it "can not connect to a mysql server" do
Expand All @@ -60,10 +60,30 @@
destination: host,
duration: be_a(Integer),
error: nil,
timeout: be_a(Integer),
timeout: be_a(Numeric),
)
end

it "logs timeout" do
subject

expect(NetworkResiliency.statsd).to have_received(:gauge).with(
"network_resiliency.connect.timeout",
timeout * 1_000,
anything,
)
end

context "when connect timeout is nil" do
let(:timeout) { nil }

it "does not log timeout" do
subject

expect(NetworkResiliency.statsd).not_to have_received(:gauge)
end
end

context "when server connection times out" do
let(:klass_mock) do
Class.new(Mysql2::Client) do
Expand Down
22 changes: 22 additions & 0 deletions spec/network_resiliency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,28 @@ def expect_enabled
)
end

context "when timeout is nil" do
let(:timeout) { nil }

it "does not track timeout" do
is_expected.not_to have_received(:gauge).with(
"network_resiliency.#{action}.timeout",
any_args,
)
end
end

context "when timeout is 0" do
let(:timeout) { 0 }

it "does not track timeout" do
is_expected.not_to have_received(:gauge).with(
"network_resiliency.#{action}.timeout",
any_args,
)
end
end

it "does not track attempts for first time successes" do
is_expected.to have_received(:distribution).with(
"network_resiliency.#{action}",
Expand Down
2 changes: 1 addition & 1 deletion spec/postgres_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
destination: host,
duration: be_a(Integer),
error: nil,
timeout: be_a(Integer),
timeout: be_a(Numeric),
)
end

Expand Down

0 comments on commit 2ccb90e

Please sign in to comment.