Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace concurrent-ruby with MonitorMixin #114

Merged
merged 1 commit into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions lib/stoplight/data_store/memory.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,53 @@
# coding: utf-8

require 'concurrent'
require 'monitor'

module Stoplight
module DataStore
# @see Base
class Memory < Base
include MonitorMixin

def initialize
@failures = Concurrent::Map.new { [] }
@states = Concurrent::Map.new { State::UNLOCKED }
@lock = Monitor.new
@failures = Hash.new { |h, k| h[k] = [] }
@states = Hash.new { |h, k| h[k] = State::UNLOCKED }
super() # MonitorMixin
end

def names
(all_failures.keys + all_states.keys).uniq
synchronize { @failures.keys | @states.keys }
end

def get_all(light)
[get_failures(light), get_state(light)]
synchronize { [@failures[light.name], @states[light.name]] }
end

def get_failures(light)
all_failures[light.name]
synchronize { @failures[light.name] }
end

def record_failure(light, failure)
@lock.synchronize do
synchronize do
n = light.threshold - 1
failures = get_failures(light).first(n).unshift(failure)
all_failures[light.name] = failures
failures.size
@failures[light.name] = @failures[light.name].first(n)
@failures[light.name].unshift(failure).size
end
end

def clear_failures(light)
all_failures.delete(light.name)
synchronize { @failures.delete(light.name) }
end

def get_state(light)
all_states[light.name]
synchronize { @states[light.name] }
end

def set_state(light, state)
all_states[light.name] = state
synchronize { @states[light.name] = state }
end

def clear_state(light)
all_states.delete(light.name)
end

private

def all_failures
@failures
end

def all_states
@states
synchronize { @states.delete(light.name) }
end
end
end
Expand Down
6 changes: 0 additions & 6 deletions stoplight.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ Gem::Specification.new do |gem|

gem.required_ruby_version = '>= 2.1'

{
'concurrent-ruby' => '1.0'
}.each do |name, version|
gem.add_dependency(name, "~> #{version}")
end

{
'benchmark-ips' => '2.3',
'bugsnag' => '4.0',
Expand Down