puma_backlog_detector 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 390c6ae8719583ea636bb11da329356bdeba00ee
4
+ data.tar.gz: dcaafcf2a062c9851883076cac95061dedb15903
5
+ SHA512:
6
+ metadata.gz: c3f9992807274b5ffd9b06c7f43801c548342d9b6a06981b71cfc05670c8ea2af0a9ffd64097e8d23331974995a39b28b64209fff6eedc6861736e933c816087
7
+ data.tar.gz: 192b569443c39911f844a97b79af605c93e3f3cf0c2d4f10519a777177a1cd3edb011136b0590832a9f99c2a63088ee0baf8b49d7d74ccff913001805a824600
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.4
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in puma_backlog_detector.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # PumaBacklogDetector
2
+
3
+ Periodically checks Puma backlog and creates a flag file if too high.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'puma_backlog_detector'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install puma_backlog_detector
20
+
21
+ ## Usage
22
+
23
+ In rails config:
24
+
25
+ ```ruby
26
+ config.puma_backlog_detector.flag_path = '/run/app/congested.flag'
27
+ config.puma_backlog_detector.max_backlog = 16
28
+ config.puma_backlog_detector.check_interval = 0.01 # seconds
29
+ ```
30
+
31
+ In nginx config:
32
+
33
+ ```
34
+ location / {
35
+ proxy_pass http://localhost:300;
36
+
37
+ if (-f /run/app/congested.flag) {
38
+ return 503;
39
+ }
40
+ }
41
+ ```
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dividedmind/puma_backlog_detector.
52
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "puma_backlog_detector"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,49 @@
1
+ require "puma_backlog_detector/version"
2
+ require "puma_backlog_detector/railtie" if defined? Rails
3
+
4
+ class PumaBacklogDetector
5
+ def initialize flag_path, max_backlog = 16
6
+ @flag = FlagFile.new flag_path
7
+ @max_backlog = max_backlog
8
+ end
9
+
10
+ def check
11
+ if server = Puma::Server.current
12
+ @flag.set server.backlog > @max_backlog
13
+ end
14
+ end
15
+
16
+ def check_periodically interval_s = 0.01
17
+ while true
18
+ check
19
+ sleep interval_s
20
+ end
21
+ end
22
+
23
+ def check_in_background interval_s = 0.01
24
+ @thread = Thread.new { check_periodically interval_s }
25
+ end
26
+
27
+ def stop
28
+ if @thread
29
+ @thread.exit
30
+ @thread.join
31
+ @thread = nil
32
+ end
33
+ end
34
+
35
+ class FlagFile
36
+ def initialize path
37
+ @path = path
38
+ @exists = File.exists? path
39
+ set false
40
+ end
41
+
42
+ def set value
43
+ if @exists != value
44
+ FileUtils.send((value ? :touch : :rm_f), @path)
45
+ @exists = value
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ class PumaBacklogDetector
2
+ class Railtie < Rails::Railtie
3
+ config.puma_backlog_detector = ActiveSupport::OrderedOptions.new
4
+ config.puma_backlog_detector.max_backlog = 16
5
+ config.puma_backlog_detector.check_interval = 0.01
6
+ config.puma_backlog_detector.flag_path = nil
7
+
8
+ initializer "puma_backlog_detector.run" do
9
+ conf = config.puma_backlog_detector
10
+ if conf.flag_path
11
+ @puma_backlog_detector = PumaBacklogDetector.new conf.flag_path, conf.max_backlog
12
+ @puma_backlog_detector.check_in_background conf.check_interval
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ class PumaBacklogDetector
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'puma_backlog_detector/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "puma_backlog_detector"
8
+ spec.version = PumaBacklogDetector::VERSION
9
+ spec.authors = ["Rafał Rzepecki"]
10
+ spec.email = ["rafal@conjur.net"]
11
+
12
+ spec.summary = %q{Periodically check Puma backlog and write a flag file}
13
+ spec.homepage = "https://github.com/dividedmind/puma_backlog_detector"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "puma", "~> 3.6"
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puma_backlog_detector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rafał Rzepecki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: puma
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.6'
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.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - rafal@conjur.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/puma_backlog_detector.rb
85
+ - lib/puma_backlog_detector/railtie.rb
86
+ - lib/puma_backlog_detector/version.rb
87
+ - puma_backlog_detector.gemspec
88
+ homepage: https://github.com/dividedmind/puma_backlog_detector
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.6.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Periodically check Puma backlog and write a flag file
111
+ test_files: []