connfuse 0.1.2

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: 278ccc536b65fc29914d15097298404a5aba5687
4
+ data.tar.gz: 17aec67489447d8ef78703231f1c510e8730c7ab
5
+ SHA512:
6
+ metadata.gz: c8820717202931f1886a309d20f8c417a620232ae64a89fc4438fb1e7f0ac55e350e3b0cc684812b1d4e968a10643fa3f176ce0b8f102ae72b803e909697f181
7
+ data.tar.gz: c7e80362b34381c511c512de3f77ba35aa82addaacf3d106866d0e5a6c193f0c5eea5cc04dcb4bbd6db908ac296b9814a3e89ae50ff31ea0ee0e5b1accb79e02
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,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0
4
+ - 2.1
5
+ - 2.2
6
+ before_install: gem install bundler -v 1.11.2
7
+ addons:
8
+ code_climate:
9
+ repo_token:
10
+ secure: Fk7M9a95/0h2NcJ0rzencYnLwWjPjT5Uz5YC9nPaNC4Uho6/AlaGldqKnMxR/VeDyKgNxYJH4w5XOSP4xKrOmZAwNsQaU1u5pQJeZv6qbiD4BMC+c2VFzxfjnCjbf9jBhCY1ZUHhXCEr/mOLwPvplJnrkwhAVT5+LAK0qyQHDjeCPumTyO6N+E4jjNfobQRSlWwVK5JBZjDf5U++uDzYRKP2ih91ppzSdI59cTfwJ2MDvaDfobTO6388hZpPihRRgdcuE9/yIGA+JglNcJZHK4/S6xZBzkw874uZvRKHBGGTI4AxILzkMUpT5HsA6FNGotRFGDT04fFAJhpfZWzNQpoG5SMxxJidKt6s9C4qgc+D0R9Fuy8Tnuyrqtzg0yrc+I53xDAL+rw1FEA/GUMMVa5Q/Vsh/tH0Ylv5Nw0cfvrtQ5B7OQ0IWPY7+F1IlOcZPfYhFDwtnAaqh0tAamghL0/yrceR9plh+7P64zn2mRm9epay2SmFUnu0Iu8hY2VoRGr2CLsWuTVVc4IU1Ekkh4y2I6PmZ/GgNkCwEeBvOgRBqWfUru34LgXgf2cj7JMqDI9bpKTlW5FhJonhmZsMOV2R1d9A92jJEs4GUNj5wsIdb29MiN9JyfV2URWzgUdFOlBHraVJtD0Wzr11KqG5kfiHjBqZCvcHO5vDwSig9VU=
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at v.promzelev@fun-box.ru. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in connfuse.gemspec
4
+ gemspec
5
+
6
+ gem 'codeclimate-test-reporter', group: :test, require: nil
7
+ gem 'timecop', group: :test
8
+ gem 'webmock', group: :test
9
+ gem 'pry', group: :development
10
+ gem 'yard', group: :development
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 v.promzelev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # Connfuse
2
+
3
+ [![Build Status](https://travis-ci.org/rutaka-n/connfuse.svg?branch=master)](https://travis-ci.org/rutaka-n/connfuse) [![Code Climate](https://codeclimate.com/github/rutaka-n/connfuse/badges/gpa.svg)](https://codeclimate.com/github/rutaka-n/connfuse) [![Test Coverage](https://codeclimate.com/github/rutaka-n/connfuse/badges/coverage.svg)](https://codeclimate.com/github/rutaka-n/connfuse/coverage) [![Issue Count](https://codeclimate.com/github/rutaka-n/connfuse/badges/issue_count.svg)](https://codeclimate.com/github/rutaka-n/connfuse)
4
+
5
+ Connfuse is a Ruby circuit breaker gem. It lets your application fail fast from failures of its service dependencies. It wraps calls to external services and monitors for failures.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'connfuse'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install connfuse
22
+
23
+ ## Usage
24
+
25
+ You can use Circuit class directly:
26
+ ```ruby
27
+ circuit = Connfuse::Circuit.new
28
+ circuit.pass_thru do
29
+ some_transport.data.get(params)
30
+ end
31
+ ```
32
+ Or you can include Connfuse module in your class and specify methods:
33
+ ```ruby
34
+ class Foo
35
+ include Connfuse
36
+
37
+ def bar
38
+ # do something
39
+ end
40
+
41
+ fuse_for :bar
42
+ end
43
+ ```
44
+ Circuit will be initialized for your class automatically
45
+ or you can specify parameters:
46
+ ```ruby
47
+ class Foo
48
+ include Connfuse
49
+ circuit(limit: 5, timeout: 15, expected_errors: [SomeErrorClass])
50
+
51
+ # ...
52
+ end
53
+ ```
54
+ - limit - count of attempts to execute method before circuit will be broken.
55
+ - timeout - specify time until the next attempt to execute methods when circuit is broken.
56
+ - expected_errors - array of error classes which do not count as errors of circuit.
57
+
58
+ ## Development
59
+
60
+ 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.
61
+
62
+ 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).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rutaka-n/connfuse. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
67
+
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
72
+
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 'connfuse'
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
data/connfuse.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'connfuse/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'connfuse'
8
+ spec.version = Connfuse::VERSION
9
+ spec.authors = ['rutaka-n']
10
+ spec.email = ['rutaka.nashimo@yandex.ru']
11
+
12
+ spec.summary = 'Simple circuit breaker pattern in ruby.'
13
+ spec.description = 'Connfuse is a Ruby circuit breaker gem.' \
14
+ "It lest your application fail fast of its service dependencies." \
15
+ 'It wraps calls to external services and monitors for failures.'
16
+ spec.homepage = 'https://github.com/rutaka-n/connfuse'
17
+ spec.license = 'MIT'
18
+
19
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
20
+ # delete this section to allow pushing this gem to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
23
+ else
24
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
25
+ end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.required_ruby_version = '>= 2.0'
33
+
34
+ spec.add_development_dependency 'bundler', '~> 1.11'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.5'
37
+ end
@@ -0,0 +1,69 @@
1
+ module Connfuse
2
+ class CircuitError < StandardError; end
3
+
4
+ class Circuit
5
+ attr_reader :status,
6
+ :failure_count,
7
+ :timeout,
8
+ :limit,
9
+ :expected_errors,
10
+ :last_failed_at,
11
+ :last_error
12
+
13
+ def initialize(timeout: 15, limit: 5, expected_errors: [])
14
+ @timeout = timeout
15
+ @expected_errors = expected_errors
16
+ @limit = limit
17
+ @failure_count = 0
18
+ @status = :loaded
19
+ @mutex = Mutex.new
20
+
21
+ @expected_errors << CircuitError
22
+ end
23
+
24
+ def loaded?
25
+ :loaded == status
26
+ end
27
+
28
+ def broken?
29
+ :broken == status
30
+ end
31
+
32
+ def break!
33
+ @mutex.lock
34
+ @status = :broken
35
+ @mutex.unlock
36
+ end
37
+
38
+ def load!
39
+ @mutex.lock
40
+ @status = :loaded
41
+ @failure_count = 0
42
+ @mutex.unlock
43
+ end
44
+
45
+ def register_failure(error)
46
+ return if expected_errors.include? error
47
+ @mutex.lock
48
+ @failure_count += 1
49
+ @last_error = error
50
+ @last_failed_at = Time.now
51
+ @mutex.unlock
52
+ end
53
+
54
+ def pass_thru
55
+ raise CircuitError if broken? && too_soon?
56
+ yield
57
+ rescue => e
58
+ register_failure(e)
59
+ break! if failure_count > limit
60
+ raise e
61
+ end
62
+
63
+ private
64
+
65
+ def too_soon?
66
+ Time.now < (last_failed_at + limit)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module Connfuse
2
+ VERSION = '0.1.2'.freeze
3
+ end
data/lib/connfuse.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'connfuse/version'
2
+
3
+ module Connfuse
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ def base.circuit(expected_errors: [], limit: 5, timeout: 15)
7
+ @circuit ||= Circuit.new(expected_errors: expected_errors, limit: limit, timeout: timeout)
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def fuse_for(*methods)
13
+ methods.each do |m|
14
+ alias_method "#{m}_old".to_sym, m.to_sym
15
+ define_method(m.to_sym) do |*args|
16
+ self.class.circuit.pass_thru { send("#{m}_old".to_sym, *args) }
17
+ end
18
+ private "#{m}_old".to_sym
19
+ end
20
+ end
21
+
22
+ protected :fuse_for
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: connfuse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - rutaka-n
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ description: Connfuse is a Ruby circuit breaker gem.It lest your application fail
56
+ fast of its service dependencies.It wraps calls to external services and monitors
57
+ for failures.
58
+ email:
59
+ - rutaka.nashimo@yandex.ru
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - connfuse.gemspec
75
+ - lib/connfuse.rb
76
+ - lib/connfuse/circuit.rb
77
+ - lib/connfuse/version.rb
78
+ homepage: https://github.com/rutaka-n/connfuse
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ allowed_push_host: https://rubygems.org
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '2.0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.2.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Simple circuit breaker pattern in ruby.
103
+ test_files: []