* [PATCH v3] middleware: reuse inet_diag netlink socket
@ 2023-12-29 17:44 12% ` Eric Wong
0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2023-12-29 17:44 UTC (permalink / raw)
To: Jean Boussier; +Cc: raindrops-public
Eric Wong <bofh@yhbt.net> wrote:
> I'll squash this in for fork+preload safety.
I forget there are multithreaded servers using this :x
------8<-----
Subject: [PATCH v3] middleware: reuse inet_diag netlink socket
No point in constantly allocating and deallocating FDs (and Ruby
IO objects) when reusing them is supported.
However, we must guard it against parallel callers since
linux_inet_diag.c::diag releases the GVL while calling
sendmsg(2) and recvmsg(2), so it's possible two Ruby threads can
end up crossing streams if the middlware is used with a
multi-threaded server.
AFAIK, Raindrops::Middleware isn't a heavily-trafficked endpoint,
so saving FDs and avoiding thread specific data is preferable
for memory-constrained systems I use.
---
lib/raindrops/middleware.rb | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/lib/raindrops/middleware.rb b/lib/raindrops/middleware.rb
index d5e3927..20e573c 100644
--- a/lib/raindrops/middleware.rb
+++ b/lib/raindrops/middleware.rb
@@ -1,5 +1,6 @@
# -*- encoding: binary -*-
require 'raindrops'
+require 'thread'
# Raindrops::Middleware is Rack middleware that allows snapshotting
# current activity from an HTTP request. For all operating systems,
@@ -93,11 +94,12 @@ def initialize(app, opts = {})
@app = app
@stats = opts[:stats] || Stats.new
@path = opts[:path] || "/_raindrops"
+ @mtx = Mutex.new
tmp = opts[:listeners]
if tmp.nil? && defined?(Unicorn) && Unicorn.respond_to?(:listener_names)
tmp = Unicorn.listener_names
end
- @tcp = @unix = nil
+ @nl_sock = @tcp = @unix = nil
if tmp
@tcp = tmp.grep(/\A.+:\d+\z/)
@@ -129,9 +131,12 @@ def stats_response # :nodoc:
"writing: #{@stats.writing}\n"
if defined?(Raindrops::Linux.tcp_listener_stats)
- Raindrops::Linux.tcp_listener_stats(@tcp).each do |addr,stats|
- body << "#{addr} active: #{stats.active}\n" \
- "#{addr} queued: #{stats.queued}\n"
+ @mtx.synchronize do
+ @nl_sock ||= Raindrops::InetDiagSocket.new
+ Raindrops::Linux.tcp_listener_stats(@tcp, @nl_sock).each do |addr,stats|
+ body << "#{addr} active: #{stats.active}\n" \
+ "#{addr} queued: #{stats.queued}\n"
+ end
end if @tcp
Raindrops::Linux.unix_listener_stats(@unix).each do |addr,stats|
body << "#{addr} active: #{stats.active}\n" \
^ permalink raw reply related [relevance 12%]
* [PATCH] middleware: reuse inet_diag netlink socket
@ 2023-09-30 23:13 14% ` Eric Wong
0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2023-09-30 23:13 UTC (permalink / raw)
To: Jean Boussier; +Cc: raindrops-public
> Your code (and some of the Ruby code shipped w/ raindrops)
> should probably be reusing sockets given our API allows it.
IOW, something like this (don't think test cases are worth updating):
------8<-------
Subject: [PATCH] middleware: reuse inet_diag netlink socket
No point in constantly allocating and deallocating FDs (and Ruby
IO objects) when reusing them is supported.
---
lib/raindrops/middleware.rb | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/lib/raindrops/middleware.rb b/lib/raindrops/middleware.rb
index d5e3927..e0781f2 100644
--- a/lib/raindrops/middleware.rb
+++ b/lib/raindrops/middleware.rb
@@ -97,7 +97,9 @@ def initialize(app, opts = {})
if tmp.nil? && defined?(Unicorn) && Unicorn.respond_to?(:listener_names)
tmp = Unicorn.listener_names
end
- @tcp = @unix = nil
+ @nl_sock = @tcp = @unix = nil
+ defined?(Raindrops::Linux.tcp_listener_stats) and
+ @nl_sock = Raindrops::InetDiagSocket.new
if tmp
@tcp = tmp.grep(/\A.+:\d+\z/)
@@ -129,7 +131,7 @@ def stats_response # :nodoc:
"writing: #{@stats.writing}\n"
if defined?(Raindrops::Linux.tcp_listener_stats)
- Raindrops::Linux.tcp_listener_stats(@tcp).each do |addr,stats|
+ Raindrops::Linux.tcp_listener_stats(@tcp, @nl_sock).each do |addr,stats|
body << "#{addr} active: #{stats.active}\n" \
"#{addr} queued: #{stats.queued}\n"
end if @tcp
^ permalink raw reply related [relevance 14%]
Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2023-09-23 9:56 [PATCH] tcp_listener_stats: always eagerly close sockets Jean Boussier
2023-09-26 21:40 ` Eric Wong
2023-09-30 23:13 14% ` [PATCH] middleware: reuse inet_diag netlink socket Eric Wong
2023-09-30 23:35 ` [squash] make reusing inet_diag sock fork+preload safe Eric Wong
2023-12-29 17:44 12% ` [PATCH v3] middleware: reuse inet_diag netlink socket Eric Wong
Code repositories for project(s) associated with this public inbox
https://yhbt.net/raindrops.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).