Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Rack::GarbageCollector middleware

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...
commit 4f2f4364de76de2e41c9c25c7f31f1414f79978a 1 parent b5218fe
@rtomayko rtomayko authored
View
1  lib/rack/contrib.rb
@@ -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"
View
14 lib/rack/garbagecollector.rb
@@ -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
View
12 test/spec_rack_garbagecollector.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.