Changeset

10829:67a09706e56e

mod_csi_simple: Record stats of how long buffers are held Telnet command `stats:show("buffer_hold"):histogram()` looks nice!
author Kim Alvefur <zash@zash.se>
date Sat, 09 May 2020 17:45:45 +0200
parents 10828:c12ed21f877e
children 10830:8889d5037aca
files plugins/mod_csi_simple.lua
diffstat 1 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_csi_simple.lua	Sat May 09 17:38:13 2020 +0200
+++ b/plugins/mod_csi_simple.lua	Sat May 09 17:45:45 2020 +0200
@@ -101,10 +101,16 @@
 	return stanza;
 end
 
+local measure_buffer_hold = module:measure("buffer_hold", "times");
+
 local function manage_buffer(stanza, session)
 	local ctr = session.csi_counter or 0;
 	local flush, why = should_flush(stanza, session, ctr);
 	if flush then
+		if session.csi_measure_buffer_hold then
+			session.csi_measure_buffer_hold();
+			session.csi_measure_buffer_hold = nil;
+		end
 		session.log("debug", "Flushing buffer (%s; queue size is %d)", why or "important", session.csi_counter);
 		session.conn:resume_writes();
 	else
@@ -117,6 +123,10 @@
 
 local function flush_buffer(data, session)
 	session.log("debug", "Flushing buffer (%s; queue size is %d)", "client activity", session.csi_counter);
+	if session.csi_measure_buffer_hold then
+		session.csi_measure_buffer_hold();
+		session.csi_measure_buffer_hold = nil;
+	end
 	session.conn:resume_writes();
 	return data;
 end
@@ -124,6 +134,7 @@
 function enable_optimizations(session)
 	if session.conn and session.conn.pause_writes then
 		session.conn:pause_writes();
+		session.csi_measure_buffer_hold = measure_buffer_hold();
 		session.csi_counter = 0;
 		filters.add_filter(session, "stanzas/out", manage_buffer);
 		filters.add_filter(session, "bytes/in", flush_buffer);
@@ -136,6 +147,10 @@
 	filters.remove_filter(session, "stanzas/out", manage_buffer);
 	filters.remove_filter(session, "bytes/in", flush_buffer);
 	session.csi_counter = nil;
+	if session.csi_measure_buffer_hold then
+		session.csi_measure_buffer_hold();
+		session.csi_measure_buffer_hold = nil;
+	end
 	if session.conn and session.conn.resume_writes then
 		session.conn:resume_writes();
 	end
@@ -160,6 +175,7 @@
 	local session = event.session;
 	if session.state == "inactive" and session.conn and session.conn.pause_writes then
 		session.conn:pause_writes();
+		session.csi_measure_buffer_hold = measure_buffer_hold();
 		session.log("debug", "Buffer flushed, resuming inactive mode (queue size was %d)", session.csi_counter);
 		session.csi_counter = 0;
 	end