Skip to content

Commit af064ef

Browse files
authored
Set client state to idle after error (#179)
* Set client state to idle after error * fmt * spelling * clean up
1 parent e84a6f8 commit af064ef

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/stats.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,10 @@ impl Collector {
740740
address_id,
741741
} => {
742742
match client_states.get_mut(&client_id) {
743-
Some(client_info) => client_info.error_count += stat.value as u64,
743+
Some(client_info) => {
744+
client_info.state = ClientState::Idle;
745+
client_info.error_count += stat.value as u64;
746+
}
744747
None => warn!("Got event {:?} for unregistered client", stat.name),
745748
}
746749

@@ -757,7 +760,10 @@ impl Collector {
757760
address_id,
758761
} => {
759762
match client_states.get_mut(&client_id) {
760-
Some(client_info) => client_info.error_count += stat.value as u64,
763+
Some(client_info) => {
764+
client_info.state = ClientState::Idle;
765+
client_info.error_count += stat.value as u64;
766+
}
761767
None => warn!("Got event {:?} for unregistered client", stat.name),
762768
}
763769

tests/ruby/admin_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,36 @@
146146
end
147147
end
148148

149+
context "client fail to checkout connection from the pool" do
150+
it "counts clients as idle" do
151+
new_configs = processes.pgcat.current_config
152+
new_configs["general"]["connect_timeout"] = 500
153+
new_configs["general"]["ban_time"] = 1
154+
new_configs["general"]["shutdown_timeout"] = 1
155+
new_configs["pools"]["sharded_db"]["users"]["0"]["pool_size"] = 1
156+
processes.pgcat.update_config(new_configs)
157+
processes.pgcat.reload_config
158+
159+
threads = []
160+
connections = Array.new(5) { PG::connect("#{pgcat_conn_str}?application_name=one_query") }
161+
connections.each do |c|
162+
threads << Thread.new { c.async_exec("SELECT pg_sleep(1)") rescue PG::SystemError }
163+
end
164+
165+
sleep(2)
166+
admin_conn = PG::connect(processes.pgcat.admin_connection_string)
167+
results = admin_conn.async_exec("SHOW POOLS")[0]
168+
%w[cl_active cl_waiting cl_cancel_req sv_active sv_used sv_tested sv_login maxwait].each do |s|
169+
raise StandardError, "Field #{s} was expected to be 0 but found to be #{results[s]}" if results[s] != "0"
170+
end
171+
expect(results["cl_idle"]).to eq("5")
172+
expect(results["sv_idle"]).to eq("1")
173+
174+
threads.map(&:join)
175+
connections.map(&:close)
176+
end
177+
end
178+
149179
context "clients overwhelm server pools" do
150180
let(:processes) { Helpers::Pgcat.single_instance_setup("sharded_db", 2) }
151181

0 commit comments

Comments
 (0)