Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeremyanin committed Oct 3, 2012
0 parents commit 6f2e526
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,22 @@
Copyright (c) 2012 Dimko

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
@@ -0,0 +1,37 @@
# Rack::Leakin

Rack middleware that detect and handle memory leaks

## Usage

Add this line to your application's Gemfile:

```ruby
gem 'rack-leakin'
```

Then, if you are using Rails, simply add to initializer:

```ruby
Rails.application.config.middleware.use Rack::Leakin
```

You can set threshold in kilobytes and the handler:

```ruby
Rack::Leakin.threshold = 131072 # default, 128MB
```

For example, it may be useful to send exceptions to Airbrake:

```ruby
Rack::Leakin.handler = lambda do |env, beginning, ending|
Airbrake.notify \
:error_message => "Memory leak detected, from #{from}KB to #{to}KB",
:error_class => 'MemoryLeak',
:parameters => {
:request_uri => env['REQUEST_URI'],
:method => env['REQUEST_METHOD']
}
end
```
1 change: 1 addition & 0 deletions Rakefile
@@ -0,0 +1 @@
require 'bundler/gem_tasks'
1 change: 1 addition & 0 deletions lib/rack-leakin.rb
@@ -0,0 +1 @@
require 'rack/leakin'
46 changes: 46 additions & 0 deletions lib/rack/leakin.rb
@@ -0,0 +1,46 @@
require 'rack/leaking/version'
require 'rack'

module Rack
class Leakin
attr_writer :threshold, :handler, :logger

def initialize(app)
@app = app
end

def threshold
@threshold ||= begin
options[:threshold] || 131072 # 128 Mb by default
end
end

def handler
@handler ||= lambda do |env, beginning, ending|
logger.warn "*** [Memory leak detected] #{env['REQUEST_METHOD']} #{env['REQUEST_URI']}, #{beginning}KB --> #{ending}KB"
end
end

def call(env)
beginning = Process.memory

@app.call(env).tap do
ending = Process.memory

if ending - beginning > threshold
handler.call(env, beginning, ending)
end
end
end

def logger
@logger ||= begin
if defined?(Rails)
::Rails.logger
else
::Logger.new('rack-leaker.log')
end
end
end
end
end
9 changes: 9 additions & 0 deletions lib/rack/leakin/version.rb
@@ -0,0 +1,9 @@
module Rack
module Leakin
VERSION = '0.0.1'

def self.version
VERSION
end
end
end
21 changes: 21 additions & 0 deletions rack-memory.gemspec
@@ -0,0 +1,21 @@
# encoding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'rack/leakin/version'

Gem::Specification.new do |gem|
gem.name = 'rack-leakin'
gem.version = Rack::Leakin::VERSION
gem.authors = ['Dimko']
gem.email = ['deemox@gmail.com']
gem.description = %q{Rack middleware that detect and handle memory leaks}
gem.summary = %q{Rack middleware that detect and handle memory leaks}
gem.homepage = ''

gem.files = `git ls-files`.split($/)
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ['lib']

gem.add_dependency 'rack'
gem.add_dependency 'process_memory'
end

0 comments on commit 6f2e526

Please sign in to comment.