unicorn-autoscaling 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +1 -0
- data/lib/unicorn/auto_scaling/configurator.rb +62 -0
- data/lib/unicorn/auto_scaling/http_server.rb +117 -0
- data/lib/unicorn/auto_scaling/version.rb +8 -0
- data/lib/unicorn/autoscaling.rb +11 -0
- data/unicorn-autoscaling.gemspec +25 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 984c9f0d1cc794ad3f397499b1425a3f53e8b97a
|
4
|
+
data.tar.gz: 78695c9d01b61ba36bb744e9862aae09535fe392
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8025bcffccb6507cd98638b3844c76d8ef8caece803db25ded94c85c42a49165e44961033aa6a8d1962da5a74bc08b924f2d86fb6c2935d0859db6761ac6755c
|
7
|
+
data.tar.gz: 4543cdb539e2493519382647936b8f836b6936ad36cb677f37b980bd3335c9277603ff5a7d572fa7e5af3244062722b21dbc8e3217ac2bc1dc606f59dadab51d
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Florian Schwab
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Unicorn::AutoScaling
|
2
|
+
|
3
|
+
Minimalistic auto-scaling for Unicorn.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'unicorn-autoscaling', require: false
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install unicorn-autoscaling
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Add the following `require` to your unicorn configuration file:
|
22
|
+
|
23
|
+
require 'unicorn/autoscaling'
|
24
|
+
|
25
|
+
And enable autoscaling by adding:
|
26
|
+
|
27
|
+
autoscaling true
|
28
|
+
|
29
|
+
|
30
|
+
## Configuration options
|
31
|
+
|
32
|
+
autoscaling true
|
33
|
+
|
34
|
+
autoscale_idle_time_decrement 30
|
35
|
+
|
36
|
+
autoscale_idle_time_increment 10
|
37
|
+
|
38
|
+
autoscale_idle_time_samples 20
|
39
|
+
|
40
|
+
autoscale_check_interval 10
|
41
|
+
|
42
|
+
autoscale_min_workers 4
|
43
|
+
|
44
|
+
autoscale_max_workers 16
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it ( https://github.com/ydkn/unicorn-autoscaling/fork )
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Unicorn
|
2
|
+
module AutoScaling
|
3
|
+
# Auto scaling options for the Unicorn::Configurator
|
4
|
+
module Configurator
|
5
|
+
DEFAULTS = {
|
6
|
+
autoscaling: false,
|
7
|
+
autoscale_idle_time_decrement: 60,
|
8
|
+
autoscale_idle_time_increment: 10,
|
9
|
+
autoscale_idle_time_samples: 50,
|
10
|
+
autoscale_check_interval: 30,
|
11
|
+
autoscale_min_workers: 1,
|
12
|
+
autoscale_max_workers: nil
|
13
|
+
}
|
14
|
+
Unicorn::Configurator::DEFAULTS.merge!(DEFAULTS)
|
15
|
+
|
16
|
+
# enables autoscaling if set to true or disables it otherwise
|
17
|
+
def autoscaling(value)
|
18
|
+
set_bool(:autoscaling, value)
|
19
|
+
end
|
20
|
+
|
21
|
+
# sets the time minimum average idle time
|
22
|
+
# before a worker decrement is performed
|
23
|
+
def autoscale_idle_time_decrement(value)
|
24
|
+
set_int(:autoscale_idle_time_decrement, value, 1)
|
25
|
+
end
|
26
|
+
|
27
|
+
# sets the time maximum average idle time
|
28
|
+
# before a worker increment is performed
|
29
|
+
def autoscale_idle_time_increment(value)
|
30
|
+
set_int(:autoscale_idle_time_increment, value, 1)
|
31
|
+
end
|
32
|
+
|
33
|
+
# sets the amount of requests used to calculate
|
34
|
+
# the average idle time
|
35
|
+
def autoscale_idle_time_samples(value)
|
36
|
+
set_int(:autoscale_idle_time_samples, value, 1)
|
37
|
+
end
|
38
|
+
|
39
|
+
# sets the interval for checking if scaling
|
40
|
+
# should be performed
|
41
|
+
def autoscale_check_interval(value)
|
42
|
+
set_int(:autoscale_check_interval, value, 1)
|
43
|
+
end
|
44
|
+
|
45
|
+
# sets the minimum number of worker processes
|
46
|
+
def autoscale_min_workers(value)
|
47
|
+
set_int(:autoscale_min_workers, value, 1)
|
48
|
+
end
|
49
|
+
|
50
|
+
# sets the maximum number of worker processes
|
51
|
+
def autoscale_max_workers(value)
|
52
|
+
set_int(:autoscale_max_workers, value, 1)
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.extend_instance!(c)
|
56
|
+
c.extend(self)
|
57
|
+
|
58
|
+
c.set.merge!(DEFAULTS)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module Unicorn
|
2
|
+
module AutoScaling
|
3
|
+
# Auto scaling extensions for the Unicorn http server
|
4
|
+
module HttpServer
|
5
|
+
RAINDROP_OFFSET = Unicorn::Worker::PER_DROP - 1
|
6
|
+
|
7
|
+
NONE = 0
|
8
|
+
INCREMENT = 1
|
9
|
+
DECREMENT = 2
|
10
|
+
|
11
|
+
attr_accessor :autoscaling, :autoscale_idle_time_samples,
|
12
|
+
:autoscale_check_interval, :autoscale_idle_time_decrement,
|
13
|
+
:autoscale_idle_time_increment, :autoscale_min_workers,
|
14
|
+
:autoscale_max_workers
|
15
|
+
|
16
|
+
def process_client(client)
|
17
|
+
autoscale! if self.autoscaling
|
18
|
+
|
19
|
+
super(client)
|
20
|
+
|
21
|
+
@_autoscale_time_of_last_request = Time.now.to_f if self.autoscaling
|
22
|
+
end
|
23
|
+
|
24
|
+
def reap_all_workers
|
25
|
+
if @_autoscale_last_scaling < Time.now.to_f - 10
|
26
|
+
if @_autoscale_raindrop[RAINDROP_OFFSET] == INCREMENT
|
27
|
+
if self.worker_processes < self.autoscale_max_workers
|
28
|
+
logger.info("Auto scaling: UP")
|
29
|
+
|
30
|
+
self.worker_processes += 1
|
31
|
+
else
|
32
|
+
logger.warn('Auto scaling: Unable to scale up since maximum number of workers already reached!')
|
33
|
+
end
|
34
|
+
elsif @_autoscale_raindrop[RAINDROP_OFFSET] == DECREMENT
|
35
|
+
if self.worker_processes > self.autoscale_min_workers
|
36
|
+
logger.info('Auto scaling: DOWN')
|
37
|
+
|
38
|
+
self.worker_processes -= 1
|
39
|
+
else
|
40
|
+
logger.warn('Auto scaling: Unable to scale down since minimum number of workers already reached!')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
@_autoscale_raindrop[RAINDROP_OFFSET] = NONE
|
45
|
+
|
46
|
+
@_autoscale_last_scaling = Time.now.to_f
|
47
|
+
end
|
48
|
+
|
49
|
+
super
|
50
|
+
end
|
51
|
+
|
52
|
+
def init_worker_process(worker)
|
53
|
+
@_autoscale_time_of_last_request = Time.now.to_f
|
54
|
+
@_autoscale_last_check = Time.now.to_f
|
55
|
+
@_autoscale_idle_times = []
|
56
|
+
|
57
|
+
super(worker)
|
58
|
+
end
|
59
|
+
|
60
|
+
def start
|
61
|
+
self.autoscale_max_workers ||= self.worker_processes
|
62
|
+
|
63
|
+
super
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def autoscale!
|
69
|
+
# Only clear idle times if scaling is requested
|
70
|
+
unless @_autoscale_raindrop[RAINDROP_OFFSET] == NONE
|
71
|
+
@_autoscale_idle_times.clear
|
72
|
+
|
73
|
+
return
|
74
|
+
end
|
75
|
+
|
76
|
+
average_idle_time = autoscale_average_idle_time
|
77
|
+
|
78
|
+
return unless @_autoscale_last_check < Time.now.to_f - self.autoscale_check_interval
|
79
|
+
|
80
|
+
update_autoscale_action_if_necessary(average_idle_time)
|
81
|
+
|
82
|
+
@_autoscale_last_check = Time.now.to_f
|
83
|
+
end
|
84
|
+
|
85
|
+
def autoscale_average_idle_time
|
86
|
+
idle_time = Time.now.to_f - @_autoscale_time_of_last_request
|
87
|
+
|
88
|
+
@_autoscale_idle_times.unshift(idle_time)
|
89
|
+
|
90
|
+
@_autoscale_idle_times = @_autoscale_idle_times[0..self.autoscale_idle_time_samples]
|
91
|
+
|
92
|
+
@_autoscale_idle_times.inject(:+) / @_autoscale_idle_times.count
|
93
|
+
end
|
94
|
+
|
95
|
+
def update_autoscale_action_if_necessary(average_idle_time)
|
96
|
+
if average_idle_time < self.autoscale_idle_time_decrement
|
97
|
+
update_autoscale_action(INCREMENT)
|
98
|
+
elsif average_idle_time > self.autoscale_idle_time_increment
|
99
|
+
update_autoscale_action(DECREMENT)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def update_autoscale_action(action)
|
104
|
+
@_autoscale_raindrop[RAINDROP_OFFSET] = action
|
105
|
+
|
106
|
+
@_autoscale_idle_times.clear
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.extend_instance!(s)
|
110
|
+
s.extend(self)
|
111
|
+
|
112
|
+
s.instance_variable_set(:@_autoscale_raindrop, Raindrops.new(Unicorn::Worker::PER_DROP))
|
113
|
+
s.instance_variable_set(:@_autoscale_last_scaling, Time.now.to_f)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'unicorn/configurator'
|
2
|
+
require 'unicorn/auto_scaling/configurator'
|
3
|
+
require 'unicorn/auto_scaling/http_server'
|
4
|
+
|
5
|
+
ObjectSpace.each_object(Unicorn::Configurator) do |c|
|
6
|
+
Unicorn::AutoScaling::Configurator.extend_instance!(c)
|
7
|
+
end
|
8
|
+
|
9
|
+
ObjectSpace.each_object(Unicorn::HttpServer) do |s|
|
10
|
+
Unicorn::AutoScaling::HttpServer.extend_instance!(s)
|
11
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'unicorn/auto_scaling/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'unicorn-autoscaling'
|
8
|
+
spec.version = Unicorn::AutoScaling::VERSION
|
9
|
+
spec.authors = ['Florian Schwab']
|
10
|
+
spec.email = ['me@ydkn.de']
|
11
|
+
spec.summary = %q(Auto-scaling for the Unicorn HTTP server)
|
12
|
+
spec.description = %q(Auto-scaling for the Unicorn HTTP server)
|
13
|
+
spec.homepage = 'https://github.com/ydkn/unicorn-autoscaling'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'unicorn'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unicorn-autoscaling
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Florian Schwab
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: unicorn
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Auto-scaling for the Unicorn HTTP server
|
56
|
+
email:
|
57
|
+
- me@ydkn.de
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/unicorn/auto_scaling/configurator.rb
|
68
|
+
- lib/unicorn/auto_scaling/http_server.rb
|
69
|
+
- lib/unicorn/auto_scaling/version.rb
|
70
|
+
- lib/unicorn/autoscaling.rb
|
71
|
+
- unicorn-autoscaling.gemspec
|
72
|
+
homepage: https://github.com/ydkn/unicorn-autoscaling
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Auto-scaling for the Unicorn HTTP server
|
96
|
+
test_files: []
|