Skip to content

Commit 6bbcd46

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

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/client.rs

+18-2
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,8 +241,13 @@ 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

250+
238251
result
239252
}
240253
Err(err) => Err(err),
@@ -258,9 +271,12 @@ pub async fn client_entrypoint(
258271

259272
if !client.is_admin() {
260273
let _ = drain.send(-1).await;
261-
}
262274

263-
result
275+
if result.is_err() {
276+
client.stats.disconnect();
277+
}
278+
}
279+
result
264280
}
265281

266282
Err(err) => Err(err),

tests/ruby/admin_spec.rb

+24-1
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)