Skip to content

Commit

Permalink
Merge b795146 into 856d03a
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaeffer committed Sep 5, 2017
2 parents 856d03a + b795146 commit d14fd6c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
38 changes: 15 additions & 23 deletions lib/stoplight/data_store/memory.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,54 @@
# 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 { [get_failures(light), get_state(light)] }
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[light.name] = failures
failures.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

0 comments on commit d14fd6c

Please sign in to comment.