shift-circuit-breaker 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +11 -0
  3. data/.gitignore +13 -0
  4. data/.reek +21 -0
  5. data/.rspec +3 -0
  6. data/.rubocop.yml +21 -0
  7. data/CONTRIBUTING.md +80 -0
  8. data/Gemfile +2 -0
  9. data/LICENSE +21 -0
  10. data/README.md +81 -0
  11. data/Rakefile +9 -0
  12. data/bin/console +7 -0
  13. data/bin/setup +7 -0
  14. data/config/newrelic.yml +28 -0
  15. data/config/rubocop/.layout_rubocop.yml +2 -0
  16. data/config/rubocop/.lint_rubocop.yml +47 -0
  17. data/config/rubocop/.metrics_rubocop.yml +39 -0
  18. data/config/rubocop/.naming_rubocop.yml +11 -0
  19. data/config/rubocop/.performance_rubocop.yml +60 -0
  20. data/config/rubocop/.style_rubocop.yml +154 -0
  21. data/lib/shift/circuit_breaker.rb +57 -0
  22. data/lib/shift/circuit_breaker/adapters/base_adapter.rb +13 -0
  23. data/lib/shift/circuit_breaker/adapters/newrelic_adapter.rb +13 -0
  24. data/lib/shift/circuit_breaker/adapters/sentry_adapter.rb +13 -0
  25. data/lib/shift/circuit_breaker/circuit_handler.rb +107 -0
  26. data/lib/shift/circuit_breaker/circuit_logger.rb +36 -0
  27. data/lib/shift/circuit_breaker/circuit_monitor.rb +32 -0
  28. data/lib/shift/circuit_breaker/config.rb +48 -0
  29. data/lib/shift/circuit_breaker/version.rb +7 -0
  30. data/shift-circuit-breaker.gemspec +40 -0
  31. data/spec/shift/adapters/base_adapter_spec.rb +21 -0
  32. data/spec/shift/adapters/newrelic_adapter_spec.rb +44 -0
  33. data/spec/shift/adapters/sentry_adapter_spec.rb +42 -0
  34. data/spec/shift/circuit_breaker/circuit_handler_exception_handling_spec.rb +142 -0
  35. data/spec/shift/circuit_breaker/circuit_handler_monitoring_spec.rb +72 -0
  36. data/spec/shift/circuit_breaker/circuit_handler_spec.rb +165 -0
  37. data/spec/shift/circuit_breaker/circuit_logger_spec.rb +67 -0
  38. data/spec/shift/circuit_breaker/circuit_monitor_spec.rb +52 -0
  39. data/spec/shift/circuit_breaker_spec.rb +73 -0
  40. data/spec/spec_helper.rb +25 -0
  41. metadata +230 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 651e358b83decf16c453e4a4e8acb9c93361b9fb
4
+ data.tar.gz: 179821b9d09dab46c7a00d1dfbaa3433b01bee41
5
+ SHA512:
6
+ metadata.gz: cd5f81d3de621ba2497bea816fae35594d63a078c94c0b6791377c99da4d27492df3abb0fddb3abf7228d6d1da120e02a433903584dd057fd8fc6927642f6fb7
7
+ data.tar.gz: 87c4cc0637dbe2bc66a5b0a5be972283c8d8110f9bbf3596f7241a9cf231a4d8dc3ee5a878afc59f59ada23456796489798f8b9c7fcf36f4423f3303ef73b75e
data/.codeclimate.yml ADDED
@@ -0,0 +1,11 @@
1
+ version: "2"
2
+ plugins:
3
+ duplication:
4
+ enabled: true
5
+ config:
6
+ languages:
7
+ - ruby
8
+ reek:
9
+ enabled: true
10
+ rubocop:
11
+ enabled: true
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ **.DS_Store
2
+ /.bundle
3
+ /coverage/*
4
+ /doc/*
5
+ .env
6
+ *.gem
7
+ Gemfile.lock
8
+ .idea
9
+ /log/*
10
+ /pkg/*
11
+ /tmp/*
12
+ /.yardoc
13
+ /_yardoc/*
data/.reek ADDED
@@ -0,0 +1,21 @@
1
+ Attribute:
2
+ enabled: false
3
+
4
+ LongParameterList:
5
+ enabled: true
6
+ max_statements: 5
7
+ exclude:
8
+ - initialize
9
+ - "self.call"
10
+
11
+ ManualDispatch:
12
+ enabled: false
13
+
14
+ IrresponsibleModule:
15
+ enabled: false
16
+
17
+ UtilityFunction:
18
+ public_methods_only: true
19
+
20
+ exclude_paths:
21
+ - spec
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ # To find more info about this,
2
+ # please read the following line
3
+ # http://www.rubydoc.info/github/bbatsov/rubocop/Rubocop
4
+
5
+ inherit_from:
6
+ - config/rubocop/.layout_rubocop.yml
7
+ - config/rubocop/.lint_rubocop.yml
8
+ - config/rubocop/.metrics_rubocop.yml
9
+ - config/rubocop/.naming_rubocop.yml
10
+ - config/rubocop/.performance_rubocop.yml
11
+ - config/rubocop/.style_rubocop.yml
12
+
13
+ AllCops:
14
+ Exclude:
15
+ - "bin/*"
16
+ - "config/*"
17
+ - "Gemfile"
18
+ - "Gemfile.lock"
19
+ - "spec/*"
20
+ - "tmp/*"
21
+ TargetRubyVersion: 2.3
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,80 @@
1
+ Contributing to Shift Circuit Breaker
2
+ =====================
3
+
4
+ Feel free to contribute to the project by submitting a [pull request](https://github.com/shiftcommerce/shift-circuit-breaker/pulls) or [proposing features and discuss issues](https://github.com/shiftcommerce/shift-circuit-breaker/issues).
5
+
6
+ #### Fork the Project
7
+
8
+ Fork the [project on Github](https://github.com/shiftcommerce/shift-circuit-breaker) and check out your copy.
9
+
10
+ ```
11
+ $ git https://github.com/shiftcommerce/shift-circuit-breaker.git
12
+ $ cd shift-circuit-breaker
13
+ $ git remote add upstream https://github.com/shiftcommerce/shift-circuit-breaker.git
14
+ ```
15
+
16
+ #### Create a Branch
17
+
18
+ Make sure your fork is up-to-date and create a branch for your feature or bug fix.
19
+
20
+ ```
21
+ git checkout master
22
+ git pull upstream master
23
+ git checkout -b my-feature-branch
24
+ ```
25
+
26
+ #### Bundle Install and Run Specs
27
+
28
+ Ensure that you can build the project and run specs successfully before starting.
29
+
30
+ ```
31
+ $ ./bin/setup
32
+ $ bundle exec rspec
33
+ ```
34
+
35
+ Run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ #### Write Tests
38
+
39
+ Please make sure that there is test coverage for the changes made /or issue fixed.
40
+
41
+ #### Write Code
42
+
43
+ Implement your feature or bug fix and make sure that specs complete without errors.
44
+
45
+ #### Write Documentation
46
+
47
+ Update the documentation as per the changes made in the [README](README.md).
48
+
49
+ #### Commit Changes
50
+
51
+ Please write good commit logs. A commit log should describe what changed and why.
52
+
53
+ ```
54
+ $ git add ...
55
+ $ git commit
56
+ ```
57
+
58
+ #### Push Changes
59
+
60
+ ```
61
+ $ git push origin my-feature-branch
62
+ ```
63
+
64
+ #### Make a Pull Request
65
+
66
+ Visit your forked repo and click the 'New pull request' button. Select your feature branch, fill out the form, and click the 'Create pull request' button.
67
+
68
+ #### Rebase
69
+
70
+ If you have been working on a change for a while, rebase with upstream/master.
71
+
72
+ ```
73
+ $ git fetch upstream
74
+ $ git rebase upstream/master
75
+ $ git push origin my-feature-branch -f
76
+ ```
77
+
78
+ #### Pull Request Review
79
+
80
+ The team will review the pull request. In the case that a change request has been made, please update please update the PR in order for the feature or bug fix to be merged.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Shift Commerce
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ [ ![Codeship Status for shiftcommerce/shift-circuit-breaker](https://app.codeship.com/projects/76d6b9e0-ecaa-0135-f112-7a88d47e1dcf/status?branch=master)](https://app.codeship.com/projects/270371) [![Maintainability](https://api.codeclimate.com/v1/badges/7d52af723c1579961280/maintainability)](https://codeclimate.com/github/shiftcommerce/shift-circuit-breaker/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/7d52af723c1579961280/test_coverage)](https://codeclimate.com/github/shiftcommerce/shift-circuit-breaker/test_coverage)
2
+
3
+ # Shift Circuit Breaker
4
+
5
+ The Shift Circuit Breaker library implements a generic mechanism for detecting, monitoring and controlling external service calls that will most-likely fail at some point (e.g. timeout) and cause request queuing.
6
+
7
+ Although a plethora of Ruby circuit breaker libraries exist, those that are frequently updated have a dependency on Redis for persistence. We required a solution that did not depend on persisting to a shared data store, i.e. a library that stores counters in local memory. As a result, the Shift Circuit Breaker was born.
8
+
9
+ Similar to a conventional circuit breaker, when a circuit is closed it allows operations to flow through. When the number of sequential errors exceeds the `error_threshold`, the circuit is then opened for the defined `skip_duration` – no operations are executed and the provided `fallback` is called.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```
16
+ $ gem "shift-circuit-breaker"
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```
22
+ $ bundle
23
+ ```
24
+
25
+ Or install it yourself as:
26
+
27
+ ```
28
+ $ gem install shift-circuit-breaker
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Example usage is as follows -
34
+
35
+ ```ruby
36
+ class MyClass
37
+ CIRCUIT_BREAKER = Shift::CircuitBreaker.new(:some_identifier,
38
+ error_threshold: 10,
39
+ skip_duration: 60,
40
+ additional_exception_classes: [
41
+ ::Faraday::ClientError
42
+ ]
43
+ )
44
+
45
+ def do_something
46
+ CIRCUIT_BREAKER.call
47
+ operation: -> { SomeService.new(name: 'test').perform_task },
48
+ fallback: -> { [ 1, 2, 3, 4, 5 ].sum }
49
+ end
50
+ end
51
+ ```
52
+
53
+ ***Note:*** the `operation` and `fallback` should implement the public method `#call` or wrapped in a `Proc/Lambda`.
54
+
55
+ With regards to monitoring and logging, integration with New Relic and Sentry is included. To enable any of these features, please set the relevant configurations in an initializer (eg. `shift_circuit_breaker.rb`) as follows -
56
+
57
+ ```ruby
58
+ require "shift/circuit_breaker"
59
+
60
+ Shift::CircuitBreaker.configure do |config|
61
+ config.new_relic_license_key = ENV["NEW_RELIC_LICENSE_KEY"]
62
+ config.new_relic_app_name = ENV["NEW_RELIC_APP_NAME"]
63
+ config.sentry_dsn = ENV["SENTRY_DSN"]
64
+ config.sentry_environments = %w[ production ]
65
+ end
66
+ ```
67
+
68
+ ***Note:*** both integrations can be overriden when instantiating the `Shift::CircuitMonitor` and `Shift::CircuitLogger` services, eg.
69
+
70
+ ```ruby
71
+ CIRCUIT_BREAKER = Shift::CircuitBreaker.new(:some_identifier,
72
+ error_threshold: 10,
73
+ skip_duration: 60,
74
+ logger: Shift::CircuitBreaker::CircuitLogger.new(remote_logger: CUSTOM_LOGGER),
75
+ monitor: Shift::CircuitBreaker::CircuitMonitor.new(monitor: CUSTOM_MONITOR)
76
+ )
77
+ ```
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome. Please read our documentation on [CONTRIBUTING](CONTRIBUTING.md).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+ # -*- encoding: utf-8 -*-
3
+
4
+ require "bundler/gem_tasks"
5
+ require "rspec/core/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new
8
+
9
+ task default: %w(spec)
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "shift/circuit_breaker"
5
+ require "irb"
6
+
7
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+
3
+ set -euo pipefail
4
+
5
+ IFS=$'\n\t'
6
+
7
+ bundle install
@@ -0,0 +1,28 @@
1
+ # For full documentation of agent configuration options, please refer to
2
+ # https://docs.newrelic.com/docs/agents/ruby-agent/installation-configuration/ruby-agent-configuration
3
+ common: &default_settings
4
+ license_key: <%= Shift::CircuitBreaker.config.new_relic_license_key %>
5
+ app_name: <%= Shift::CircuitBreaker.config.new_relic_app_name %>
6
+ agent_enabled: <%= Shift::CircuitBreaker.config.new_relic_license_key.present? %>
7
+
8
+ log_level: info
9
+
10
+ browser_monitoring:
11
+ auto_instrument: true
12
+
13
+ development:
14
+ <<: *default_settings
15
+
16
+ developer_mode: true
17
+ agent_enabled: true
18
+
19
+ test:
20
+ <<: *default_settings
21
+
22
+ monitor_mode: false
23
+
24
+ staging:
25
+ <<: *default_settings
26
+
27
+ production:
28
+ <<: *default_settings
@@ -0,0 +1,2 @@
1
+ Layout/DotPosition:
2
+ EnforcedStyle: trailing
@@ -0,0 +1,47 @@
1
+ Lint/AssignmentInCondition:
2
+ Enabled: false
3
+
4
+ Lint/HandleExceptions:
5
+ Enabled: false
6
+
7
+ Lint/EndAlignment:
8
+ Enabled: false
9
+
10
+ Lint/AmbiguousOperator:
11
+ Enabled: false
12
+
13
+ Lint/AmbiguousRegexpLiteral:
14
+ Enabled: false
15
+
16
+ Lint/AssignmentInCondition:
17
+ Enabled: false
18
+
19
+ Lint/ConditionPosition:
20
+ Enabled: false
21
+
22
+ Lint/DeprecatedClassMethods:
23
+ Enabled: false
24
+
25
+ Lint/ElseLayout:
26
+ Enabled: false
27
+
28
+ Lint/HandleExceptions:
29
+ Enabled: false
30
+
31
+ Lint/LiteralInInterpolation:
32
+ Enabled: false
33
+
34
+ Lint/Loop:
35
+ Enabled: false
36
+
37
+ Lint/ParenthesesAsGroupedExpression:
38
+ Enabled: false
39
+
40
+ Lint/RequireParentheses:
41
+ Enabled: false
42
+
43
+ Lint/UnderscorePrefixedVariableName:
44
+ Enabled: false
45
+
46
+ Lint/Void:
47
+ Enabled: false
@@ -0,0 +1,39 @@
1
+ Metrics/BlockLength:
2
+ Exclude:
3
+ - "Rakefile"
4
+ - "**/*.rake"
5
+ - "spec/**/*"
6
+ - "shift-circuit-breaker.gemspec"
7
+
8
+ Metrics/BlockNesting:
9
+ Enabled: false
10
+
11
+ Metrics/ClassLength:
12
+ Enabled: false
13
+
14
+ Metrics/CyclomaticComplexity:
15
+ Enabled: false
16
+
17
+ Metrics/MethodLength:
18
+ Enabled: false
19
+
20
+ Metrics/ParameterLists:
21
+ Enabled: false
22
+
23
+ Metrics/PerceivedComplexity:
24
+ Enabled: false
25
+
26
+ Metrics/LineLength:
27
+ Max: 150
28
+
29
+ Metrics/MethodLength:
30
+ Max: 40
31
+
32
+ Metrics/ClassLength:
33
+ Max: 500
34
+
35
+ Metrics/AbcSize:
36
+ Max: 50
37
+
38
+ Metrics/CyclomaticComplexity:
39
+ Max: 10
@@ -0,0 +1,11 @@
1
+ Naming/AccessorMethodName:
2
+ Enabled: false
3
+
4
+ Naming/AsciiIdentifiers:
5
+ Enabled: false
6
+
7
+ Naming/FileName:
8
+ Enabled: false
9
+
10
+ Naming/HeredocDelimiterNaming:
11
+ Enabled: false
@@ -0,0 +1,60 @@
1
+ Performance/CaseWhenSplat:
2
+ Enabled: true
3
+
4
+ Performance/Casecmp:
5
+ Enabled: true
6
+
7
+ Performance/CompareWithBlock:
8
+ Enabled: true
9
+
10
+ Performance/Count:
11
+ Enabled: true
12
+
13
+ Performance/Detect:
14
+ Enabled: true
15
+
16
+ Performance/DoubleStartEndWith:
17
+ Enabled: true
18
+
19
+ Performance/FlatMap:
20
+ Enabled: true
21
+
22
+ Performance/HashEachMethods:
23
+ Enabled: true
24
+
25
+ Performance/LstripRstrip:
26
+ Enabled: true
27
+
28
+ Performance/RangeInclude:
29
+ Enabled: true
30
+
31
+ Performance/RedundantBlockCall:
32
+ Enabled: true
33
+
34
+ Performance/RedundantMatch:
35
+ Enabled: true
36
+
37
+ Performance/RedundantMerge:
38
+ Enabled: false
39
+
40
+ Performance/RedundantSortBy:
41
+ Enabled: true
42
+
43
+ Performance/ReverseEach:
44
+ Enabled: true
45
+
46
+ Performance/Sample:
47
+ Enabled: true
48
+
49
+ Performance/Size:
50
+ Enabled: true
51
+
52
+ Performance/StartWith:
53
+ Enabled: true
54
+
55
+ Performance/StringReplacement:
56
+ Enabled: true
57
+
58
+ Performance/TimesMap:
59
+ Enabled: true
60
+