Skip to content

Commit ba032e8

Browse files
committed
stats: Decrease cl_idle when idle socket disconnects
1 parent b63274b commit ba032e8

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/client.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ pub async fn client_entrypoint(
135135

136136
if !client.is_admin() {
137137
let _ = drain.send(-1).await;
138+
139+
if result.is_err() {
140+
client.stats.disconnect();
141+
}
138142
}
139143

140144
result
@@ -183,6 +187,10 @@ pub async fn client_entrypoint(
183187

184188
if !client.is_admin() {
185189
let _ = drain.send(-1).await;
190+
191+
if result.is_err() {
192+
client.stats.disconnect();
193+
}
186194
}
187195

188196
result
@@ -233,6 +241,10 @@ pub async fn client_entrypoint(
233241

234242
if !client.is_admin() {
235243
let _ = drain.send(-1).await;
244+
245+
if result.is_err() {
246+
client.stats.disconnect();
247+
}
236248
}
237249

238250
result
@@ -258,8 +270,11 @@ pub async fn client_entrypoint(
258270

259271
if !client.is_admin() {
260272
let _ = drain.send(-1).await;
261-
}
262273

274+
if result.is_err() {
275+
client.stats.disconnect();
276+
}
277+
}
263278
result
264279
}
265280

tests/ruby/admin_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
context "clients connects and disconnect normally" do
180180
let(:processes) { Helpers::Pgcat.single_instance_setup("sharded_db", 2) }
181181

182-
it 'shows the number same number of clients before and after' do
182+
it 'shows the same number of clients before and after' do
183183
clients_before = clients_connected_to_pool(processes: processes)
184184
threads = []
185185
connections = Array.new(4) { PG::connect("#{pgcat_conn_str}?application_name=one_query") }
@@ -194,6 +194,29 @@
194194
end
195195
end
196196

197+
context "clients connects and disconnect abruptly" do
198+
let(:processes) { Helpers::Pgcat.single_instance_setup("sharded_db", 10) }
199+
200+
it 'shows the same number of clients before and after' do
201+
threads = []
202+
connections = Array.new(2) { PG::connect("#{pgcat_conn_str}?application_name=one_query") }
203+
connections.each do |c|
204+
threads << Thread.new { c.async_exec("SELECT 1") }
205+
end
206+
clients_before = clients_connected_to_pool(processes: processes)
207+
random_string = (0...8).map { (65 + rand(26)).chr }.join
208+
connection_string = "#{pgcat_conn_str}?application_name=#{random_string}"
209+
faulty_client = Process.spawn("psql -Atx #{connection_string} >/dev/null")
210+
sleep(1)
211+
# psql starts two processes, we only know the pid of the parent, this
212+
# ensure both are killed
213+
`pkill -9 -f '#{random_string}'`
214+
Process.wait(faulty_client)
215+
clients_after = clients_connected_to_pool(processes: processes)
216+
expect(clients_before).to eq(clients_after)
217+
end
218+
end
219+
197220
context "clients overwhelm server pools" do
198221
let(:processes) { Helpers::Pgcat.single_instance_setup("sharded_db", 2) }
199222

0 commit comments

Comments
 (0)