-
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.
Merge pull request #1 from sportngin/inital-spike
Inital spike for setting up the gem. It includes a rename because I realized I didn't need to depend on dalli
- Loading branch information
Showing
18 changed files
with
300 additions
and
41 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github_repo: sportngin/breaker-rails-cache-repo | ||
semantic_versioning: true | ||
branches_to_keep: | ||
- master |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defaults: | ||
deploy_cmds: | ||
- gem build breaker.gemspec | ||
- fury push *.gem | ||
before_deploy_cmds: | ||
- /usr/local/bin/op tag-release | ||
- sed -i "" -e "s/\".*/\"$(git tag| sort -n -t. -k1,1 -k2,2 -k3,3 | tail -1 | sed s/v//)\"/" lib/breaker/rails_cache/version.rb | ||
- git add lib/breaker/rails_cache/version.rb | ||
- git commit -m "Version Bump" && git push | ||
after_deploy_cmds: | ||
- rm *.gem | ||
environments: | ||
- | ||
gemfury: {} |
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,10 @@ | ||
sudo: false | ||
branches: | ||
only: | ||
- master | ||
language: ruby | ||
rvm: | ||
- 1.9.3 | ||
- 2.1 | ||
- 2.2 | ||
- 1.9.3 | ||
script: bundle exec rspec |
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,4 +1,5 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in breaker-dalli-repo.gemspec | ||
# Specify your gem's dependencies in breaker-rails-cache-repo.gemspec | ||
gem 'breaker', :git => 'https://github.com/sportngin/breaker.git' | ||
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
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
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,11 +1,11 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'breaker/dalli/repo/version' | ||
require 'breaker/rails_cache/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "breaker-dalli-repo" | ||
spec.version = Breaker::Dalli::Repo::VERSION | ||
spec.name = "breaker-rails-cache-repo" | ||
spec.version = Breaker::RailsCache::VERSION | ||
spec.authors = ["Andy Fleener"] | ||
spec.email = ["[email protected]"] | ||
|
||
|
@@ -14,16 +14,16 @@ Gem::Specification.new do |spec| | |
end | ||
|
||
spec.summary = %q{This gem is a memcached implementation of the repo pattern used by the breaker gem} | ||
spec.description = %q{Dalli based repo for the breaker gem} | ||
spec.description = %q{Rails Cache backed repo for the breaker gem} | ||
spec.license = "MIT" | ||
|
||
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
spec.add_dependency "dalli" | ||
spec.add_dependency "breaker" | ||
spec.add_dependency "breaker", "~>0.2" | ||
spec.add_dependency "activesupport" | ||
|
||
spec.add_development_dependency "bundler", "~> 1.8" | ||
spec.add_development_dependency "rake", "~> 10.0" | ||
spec.add_development_dependency "bundler" | ||
spec.add_development_dependency "rake" | ||
spec.add_development_dependency "rspec" | ||
end |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
module Breaker | ||
module RailsCache | ||
class Fuse | ||
attr_accessor :name, :failure_threshold, :retry_threshold, :retry_timeout, :timeout, :breaker_error_class, :failure_count_ttl | ||
|
||
def initialize(name, options={}) | ||
self.name = name | ||
options = defaults.dup.merge(options) | ||
self.failure_threshold = options[:failure_threshold] | ||
self.retry_timeout = options[:retry_timeout] | ||
self.timeout = options[:timeout] | ||
self.breaker_error_class = options[:breaker_error_class] | ||
self.failure_count_ttl = options[:failure_count_ttl] | ||
self.state || set_value(:state, options[:state]) | ||
end | ||
|
||
def defaults | ||
Repo.config | ||
end | ||
|
||
def update(attributes) | ||
attributes.each do |attr, value| | ||
send("#{attr}=",value) | ||
end | ||
end | ||
|
||
def ==(other) | ||
other.instance_of?(self.class) && name == other.name | ||
end | ||
|
||
def eql?(other) | ||
self == other | ||
end | ||
|
||
def hash | ||
[self.class, self.name].hash | ||
end | ||
|
||
def set_value(key, value, options={}) | ||
Rails.cache.write(key_name(key), value, options) | ||
end | ||
|
||
def get_value(key) | ||
Rails.cache.read(key_name(key)) | ||
end | ||
def inc_value(key, value) | ||
Rails.cache.increment(key_name(key), value, expires_in: failure_count_ttl, initial: 1) | ||
end | ||
|
||
def key_name(key) | ||
"BREAKER_#{self.name}_#{key}" | ||
end | ||
|
||
def state | ||
@state ||= get_value(:state) | ||
end | ||
|
||
def state=(state) | ||
set_value(:state, state) | ||
@state = state | ||
end | ||
|
||
def retry_threshold | ||
@retry_threshold ||= get_value(:retry_threshold) | ||
end | ||
|
||
def retry_threshold=(retry_threshold) | ||
set_value(:retry_threshold, retry_threshold) | ||
@retry_threshold = retry_threshold | ||
end | ||
|
||
def failure_count | ||
@failure_count ||= get_value(:failure_count).to_i | ||
end | ||
|
||
def failure_count=(value) | ||
if @failure_count.nil? || @failure_count.zero? || value.zero? | ||
Rails.cache.write(key_name(:failure_count), 0, raw: true) | ||
end | ||
@failure_count = inc_value(:failure_count, 1) | ||
@failure_count | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "breaker/rails_cache/version" | ||
require "breaker/rails_cache/fuse" | ||
require "breaker/rails_cache/store" | ||
|
||
module Breaker | ||
module RailsCache | ||
class Repo | ||
|
||
attr_reader :store | ||
|
||
def initialize | ||
@store = Store.new | ||
end | ||
|
||
def self.config | ||
{ | ||
failure_threshold: 10, | ||
retry_timeout: 60, | ||
timeout: 5, | ||
breaker_error_class: Timeout::Error, | ||
failure_count: 0, | ||
failure_count_ttl: 300, | ||
state: :closed, | ||
} | ||
end | ||
|
||
def upsert(attributes) | ||
fuse = store.find attributes.fetch(:name) | ||
fuse.update attributes | ||
fuse | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Breaker | ||
module RailsCache | ||
class Store | ||
def initialize | ||
@store = Set.new | ||
end | ||
|
||
def find(name) | ||
Fuse.new(name) | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Breaker | ||
module RailsCache | ||
VERSION = "0.1.0" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'spec_helper' | ||
|
||
describe Breaker::RailsCache::Fuse do | ||
subject { Breaker::RailsCache::Fuse.new(:test) } | ||
|
||
it 'uses the Repo config has defaults' do | ||
expect(subject.defaults).to eq(Breaker::RailsCache::Repo.config) | ||
end | ||
|
||
it 'sets default values' do | ||
expect(subject.state).to eq(:closed) | ||
expect(subject.failure_threshold).to eq(10) | ||
expect(subject.timeout).to eq(5) | ||
expect(subject.failure_count).to eq(0) | ||
expect(subject.retry_timeout).to eq(60) | ||
end | ||
|
||
it 'can be updated' do | ||
subject.update(state: :open) | ||
expect(subject.state).to eq(:open) | ||
subject.state = :closed | ||
end | ||
|
||
it 'can detect equality based on fuse name' do | ||
expect(subject).to eq(Breaker::RailsCache::Fuse.new(:test)) | ||
end | ||
|
||
it 'sets a value using rails cache' do | ||
subject | ||
expect(Rails.cache).to receive(:write).with("BREAKER_test_state", :closed, {}) | ||
subject.set_value(:state, :closed) | ||
end | ||
|
||
it 'gets a value using rails cache' do | ||
subject | ||
expect(Rails.cache).to receive(:read).with("BREAKER_test_state") | ||
subject.get_value(:state) | ||
end | ||
|
||
it 'increment a value in rails cache' do | ||
subject | ||
expect(Rails.cache).to receive(:increment).with("BREAKER_test_failure_count", 1, { :expires_in => 300, :initial => 1}) | ||
subject.inc_value(:failure_count, 1) | ||
end | ||
|
||
it 'generates a key name' do | ||
expect(subject.key_name(:test)).to eq("BREAKER_test_test") | ||
end | ||
|
||
it 'can set reset the failure count to the initial value' do | ||
subject.failure_count = 1 | ||
subject.failure_count = 1 | ||
expect(subject.failure_count).to eq(2) | ||
subject.failure_count = 0 | ||
expect(subject.failure_count).to eq(1) | ||
end | ||
|
||
it 'can increment the failure count' do | ||
subject.failure_count = 2 | ||
expect(subject.failure_count).to eq(1) | ||
subject.failure_count = 0 | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'spec_helper' | ||
|
||
describe Breaker::RailsCache::Repo do | ||
let(:fuse) { double(:fuse) } | ||
|
||
it 'has a version number' do | ||
expect(Breaker::RailsCache::VERSION).not_to be nil | ||
end | ||
|
||
it 'has a store' do | ||
expect(subject.store).to be_a Breaker::RailsCache::Store | ||
end | ||
|
||
it 'can create/update a fuse' do | ||
attrs = double(:attrs) | ||
allow(attrs).to receive(:fetch).with(:name) { fuse } | ||
expect(subject.store).to receive(:find) { fuse } | ||
expect(fuse).to receive(:update).with(attrs) | ||
subject.upsert(attrs) | ||
end | ||
end | ||
|
||
describe Breaker::RailsCache::Repo do | ||
subject { Breaker::RailsCache::Repo } | ||
|
||
it 'has a config' do | ||
expect(subject.config).to be_a Hash | ||
end | ||
end | ||
|
||
describe Breaker do | ||
it 'has a rails cache repo' do | ||
expect(subject.repo).to be_a Breaker::RailsCache::Repo | ||
end | ||
end |
Oops, something went wrong.