Skip to content

Instantly share code, notes, and snippets.

@jjb
Created October 17, 2017 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjb/7c12952b23b94cb64fc7fb8334670135 to your computer and use it in GitHub Desktop.
Save jjb/7c12952b23b94cb64fc7fb8334670135 to your computer and use it in GitHub Desktop.
How to log file descriptor count in a rails or rack application
require_relative 'config/environment'
class FileDescriptorLogger
def initialize(app)
@app = app
end
def call(env)
if 1000==rand(1001)
unclosed_files = 0
ObjectSpace.each_object(IO) do |f|
begin
unclosed_files +=1 unless f.closed?
rescue IOError
puts "encountered IOError when checking if a file was closed"
end
end
puts "file descriptors - unclosed ruby files: #{unclosed_files}"
puts "file descriptors - linux: #{`cat /proc/sys/fs/file-nr`.strip}"
end
@app.call(env)
end
end
use FileDescriptorLogger
run Rails.application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment