-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
ba758af
commit a08476a
Showing
5 changed files
with
148 additions
and
28 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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
source "https://rubygems.org" | ||
|
||
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | ||
|
||
# Specify your gem's dependencies in circuit.gemspec | ||
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 |
---|---|---|
@@ -1,33 +1,18 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path("../lib", __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require "circuit/version" | ||
require "circuit" | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "circuit" | ||
spec.version = Circuit::VERSION | ||
spec.authors = ["Ilya Vassilevsky"] | ||
spec.email = ["[email protected]"] | ||
|
||
spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.} | ||
spec.description = %q{TODO: Write a longer description or delete this line.} | ||
spec.homepage = "TODO: Put your gem's website or public repo URL here." | ||
spec.summary = "Allows a service to fail fast to prevent overload" | ||
spec.license = "MIT" | ||
|
||
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' | ||
# to allow pushing to a single host or delete this section to allow pushing to any host. | ||
if spec.respond_to?(:metadata) | ||
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'" | ||
else | ||
raise "RubyGems 2.0 or newer is required to protect against " \ | ||
"public gem pushes." | ||
end | ||
|
||
spec.files = `git ls-files -z`.split("\x0").reject do |f| | ||
f.match(%r{^(test|spec|features)/}) | ||
end | ||
spec.bindir = "exe" | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.files = `git ls-files -z`.split("\x0") | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_development_dependency "bundler", "~> 1.15" | ||
|
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 |
---|---|---|
@@ -1,5 +1,53 @@ | ||
require "circuit/version" | ||
class Circuit | ||
VERSION = "0.1.0" | ||
|
||
module Circuit | ||
# Your code goes here... | ||
def initialize(payload:, max_failures: 100, retry_in: 60) | ||
@payload = payload | ||
@max_failures = max_failures | ||
@retry_in = retry_in | ||
|
||
@mutex = Mutex.new | ||
|
||
close | ||
end | ||
|
||
def pass(message, *args) | ||
fail @e if open? && !time_to_retry? | ||
result = payload.public_send(message, *args) | ||
close if open? | ||
result | ||
rescue => e | ||
@e = e | ||
@mutex.synchronize{ @failures[e.class] += 1 } | ||
break! if @failures[e.class] > max_failures | ||
raise e | ||
end | ||
|
||
def open? | ||
!closed? | ||
end | ||
|
||
def closed? | ||
@closed | ||
end | ||
|
||
private | ||
|
||
attr_reader :payload | ||
attr_reader :max_failures | ||
attr_reader :retry_in | ||
|
||
def break! | ||
@closed = false | ||
@broken_at = Time.now | ||
end | ||
|
||
def close | ||
@closed = true | ||
@failures = Hash.new(0) | ||
end | ||
|
||
def time_to_retry? | ||
@broken_at + retry_in < Time.now | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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