-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 73cd437
Showing
9 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Kazuki Ohta <kazuki.ohta _at_ gmail.com> | ||
Sadayuki FURUHASHI <frsyuki _at_ gmail.com> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Release 0.1 - 2012/11/17 | ||
|
||
* First release |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source "http://rubygems.org" | ||
|
||
gemspec |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
Unicorn is copyrighted free software by all contributors, see logs in | ||
revision control for names and email addresses of all of them. | ||
|
||
You can redistribute it and/or modify it under either the terms of the | ||
GNU General Public License (GPL) as published by the Free Software | ||
Foundation (FSF), version {3.0}[http://www.gnu.org/licenses/gpl-3.0.txt] | ||
or version {2.0}[http://www.gnu.org/licenses/gpl-2.0.txt] | ||
or the Ruby-specific license terms (see below). | ||
|
||
The unicorn project leader (Eric Wong) reserves the right to add future | ||
versions of the GPL (and no other licenses) as published by the FSF to | ||
the licensing terms. | ||
|
||
=== Ruby-specific terms (if you're not using the GPLv2 or GPLv3) | ||
|
||
1. You may make and give away verbatim copies of the source form of the | ||
software without restriction, provided that you duplicate all of the | ||
original copyright notices and associated disclaimers. | ||
|
||
2. You may modify your copy of the software in any way, provided that | ||
you do at least ONE of the following: | ||
|
||
a) place your modifications in the Public Domain or otherwise make them | ||
Freely Available, such as by posting said modifications to Usenet or an | ||
equivalent medium, or by allowing the author to include your | ||
modifications in the software. | ||
|
||
b) use the modified software only within your corporation or | ||
organization. | ||
|
||
c) rename any non-standard executables so the names do not conflict with | ||
standard executables, which must also be provided. | ||
|
||
d) make other distribution arrangements with the author. | ||
|
||
3. You may distribute the software in object code or executable | ||
form, provided that you do at least ONE of the following: | ||
|
||
a) distribute the executables and library files of the software, | ||
together with instructions (in the manual page or equivalent) on where | ||
to get the original distribution. | ||
|
||
b) accompany the distribution with the machine-readable source of the | ||
software. | ||
|
||
c) give non-standard executables non-standard names, with | ||
instructions on where to get the original software distribution. | ||
|
||
d) make other distribution arrangements with the author. | ||
|
||
4. You may modify and include the part of the software into any other | ||
software (possibly commercial). But some files in the distribution | ||
are not written by the author, so that they are not under this terms. | ||
|
||
5. The scripts and library files supplied as input to or produced as | ||
output from the software do not automatically fall under the | ||
copyright of the software, but belong to whomever generated them, | ||
and may be sold commercially, and may be aggregated with this | ||
software. | ||
|
||
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR | ||
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
PURPOSE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# unicorn-worker-killer | ||
|
||
Killing Unicorn worker based on 1) Max number of requests and 2) Process memory size (RSS), without affecting the request. | ||
|
||
# Install | ||
|
||
gem 'unicorn-worker-killer' | ||
|
||
# Usage | ||
|
||
add these lines to your config.ru. | ||
|
||
# Unicorn self-process killer | ||
require 'unicorn/worker_killer' | ||
|
||
# Max requests per worker | ||
use UnicornWorkerKiller::MaxRequests, 10240 + Random.rand(10240) | ||
|
||
# Max memory size (RSS) per worker | ||
use UnicornWorkerKiller::Oom, (96 + Random.rand(32)) * 1024**2 | ||
|
||
# TODO | ||
- Get RSS (Resident Set Size) without forking the child process at Mac OS and Windows |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'bundler' | ||
Bundler::GemHelper.install_tasks | ||
|
||
require 'rake/testtask' | ||
task :default => [:build] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.1.0 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
module UnicornWorkerKiller | ||
# Self-destruction by sending the signals to myself. The process sometimes | ||
# doesn't terminate by SIGTERM, so this tries to send SIGQUIT and SIGKILL | ||
# if it doesn't finish immediately. | ||
def self.kill_self(logger, start_time) | ||
alive_sec = (Time.now - start_time).to_i | ||
|
||
i = 0 | ||
while true | ||
i += 1 | ||
sig = :TERM | ||
if i > 10 # TODO configurable TERM MAX | ||
sig = :QUIT | ||
elsif i > 15 # TODO configurable QUIT MAX | ||
sig = :KILL | ||
end | ||
|
||
logger.warn "#{self} send SIGTERM (pid: #{Process.pid})\talive: #{alive_sec} sec (trial #{i})" | ||
Process.kill sig, Process.pid | ||
|
||
sleep 1 # TODO configurable sleep | ||
end | ||
end | ||
|
||
module Oom | ||
# Killing the process must be occurred at the outside of the request. We're | ||
# using similar techniques used by OobGC, to ensure actual killing doesn't | ||
# affect the request. | ||
# | ||
# @see https://github.com/defunkt/unicorn/blob/master/lib/unicorn/oob_gc.rb#L40 | ||
def self.new(app, memory_size = (1024**3), check_cycle = 16) | ||
ObjectSpace.each_object(Unicorn::HttpServer) do |s| | ||
s.extend(self) | ||
s.instance_variable_set(:@_worker_memory_size, memory_size) | ||
s.instance_variable_set(:@_worker_check_cycle, check_cycle) | ||
s.instance_variable_set(:@_worker_check_count, 0) | ||
end | ||
app # pretend to be Rack middleware since it was in the past | ||
end | ||
|
||
def process_client(client) | ||
@_worker_process_start ||= Time.now | ||
super(client) # Unicorn::HttpServer#process_client | ||
|
||
c = @_worker_check_count + 1 | ||
if c % @_worker_check_cycle == 0 | ||
@_worker_check_count = 0 | ||
if _worker_rss() > @_worker_memory_size | ||
UnicornWorkerKiller.kill_self(logger, @_worker_process_start) | ||
end | ||
else | ||
@_worker_check_count = c | ||
end | ||
end | ||
|
||
private | ||
def _worker_rss | ||
proc_status = "/proc/#{Process.pid}/status" | ||
if File.exists? proc_status | ||
open(proc_status).each_line { |l| | ||
if l.include? 'VmRSS' | ||
ls = l.split | ||
if ls.length == 3 | ||
value = ls[1].to_i | ||
unit = ls[2] | ||
case unit.downcase | ||
when 'kb' | ||
return value*(1024**1) | ||
when 'mb' | ||
return value*(1024**2) | ||
when 'gb' | ||
return value*(1024**3) | ||
end | ||
end | ||
end | ||
} | ||
end | ||
|
||
# Forking the child process sometimes fails under low memory condition. | ||
# It would be ideal for not forking the process to get RSS. For Linux, | ||
# this module reads '/proc/<pid>/status' to get RSS, but not for other | ||
# environments (e.g. MacOS and Windows). | ||
return `ps -o rss= -p #{Process.pid}`.to_i | ||
end | ||
end | ||
|
||
module MaxRequests | ||
# Killing the process must be occurred at the outside of the request. We're | ||
# using similar techniques used by OobGC, to ensure actual killing doesn't | ||
# affect the request. | ||
# | ||
# @see https://github.com/defunkt/unicorn/blob/master/lib/unicorn/oob_gc.rb#L40 | ||
def self.new(app, max_requests = 1024) | ||
ObjectSpace.each_object(Unicorn::HttpServer) do |s| | ||
s.extend(self) | ||
s.instance_variable_set(:@_worker_max_requests, max_requests) | ||
end | ||
app # pretend to be Rack middleware since it was in the past | ||
end | ||
|
||
def process_client(client) | ||
@_worker_process_start ||= Time.now | ||
super(client) # Unicorn::HttpServer#process_client | ||
|
||
if (@_worker_max_requests -= 1) <= 0 | ||
UnicornWorkerKiller.kill_self(logger, @_worker_process_start) | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# encoding: utf-8 | ||
$:.push File.expand_path('../lib', __FILE__) | ||
|
||
Gem::Specification.new do |gem| | ||
gem.name = "unicorn-worker-killer" | ||
gem.description = "Kill unicorn workers by memory and request counts" | ||
gem.homepage = "https://github.com/treasure-data/unicorn-worker-killer" | ||
gem.summary = gem.description | ||
gem.version = File.read("VERSION").strip | ||
gem.authors = ["Kazuki Ohta", "Sadayuki Furuhashi"] | ||
gem.email = ["kazuki.ohta@gmail.com", "frsyuki@gmail.com"] | ||
gem.has_rdoc = false | ||
#gem.platform = Gem::Platform::RUBY | ||
gem.files = `git ls-files`.split("\n") | ||
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") | ||
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } | ||
gem.require_paths = ['lib'] | ||
gem.add_dependency "unicorn", "~> 4.2.0" | ||
gem.add_development_dependency "rake", ">= 0.9.2" | ||
end |