Skip to content

Commit

Permalink
Add Rack::GarbageCollector middleware
Browse files Browse the repository at this point in the history
Calls GC.start after each request. This is a really simplistic
implementation but I've found it useful in debugging potential
memory leaks.
  • Loading branch information
rtomayko committed Jan 11, 2009
1 parent b5218fe commit 4f2f436
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rack/contrib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def self.release

autoload :BounceFavicon, "rack/bounce_favicon"
autoload :ETag, "rack/etag"
autoload :GarbageCollector, "rack/garbagecollector"
autoload :JSONP, "rack/jsonp"
autoload :LighttpdScriptNameFix, "rack/lighttpd_script_name_fix"
autoload :Locale, "rack/locale"
Expand Down
14 changes: 14 additions & 0 deletions lib/rack/garbagecollector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Rack
# Forces garbage collection after each request.
class GarbageCollector
def initialize(app)
@app = app
end

def call(env)
res = @app.call(env)
GC.start
res
end
end
end
12 changes: 12 additions & 0 deletions test/spec_rack_garbagecollector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rack/mock'
require 'rack/garbagecollector'

context 'Rack::GarbageCollector' do

specify 'starts the garbage collector after each request' do
app = lambda { |env|
[200, {'Content-Type'=>'text/plain'}, ['Hello World']] }
Rack::GarbageCollector.new(app).call({})
end

end

0 comments on commit 4f2f436

Please sign in to comment.