Created
October 17, 2017 20:52
-
-
Save jjb/7c12952b23b94cb64fc7fb8334670135 to your computer and use it in GitHub Desktop.
How to log file descriptor count in a rails or rack application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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