diff options
author | Eric Wong <bofh@yhbt.net> | 2023-12-29 17:44:18 +0000 |
---|---|---|
committer | Eric Wong <bofh@yhbt.net> | 2024-01-15 02:57:56 +0000 |
commit | 4dcd2caa9d7691d6a4f1d3fc0a0a4b54ac6fad6b (patch) | |
tree | 91d02013d44889dcd71607c653b87baf3d6b5d0d /lib | |
parent | e2dfea8b2c74d94b66efc00c2ccab3df7af750a5 (diff) | |
download | raindrops-4dcd2caa9d7691d6a4f1d3fc0a0a4b54ac6fad6b.tar.gz |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/raindrops/middleware.rb | 13 |
1 files 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 @@ class Raindrops::Middleware @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 @@ class Raindrops::Middleware "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" \ |