Skip to content

Commit

Permalink
Merge 7150c29 into e8f6e1d
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasWurm committed Feb 12, 2016
2 parents e8f6e1d + 7150c29 commit ad51fc8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/stoplight/data_store/memory.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# coding: utf-8

require 'concurrent/map'
require 'monitor'

module Stoplight
module DataStore
# @see Base
class Memory < Base
def initialize
@data = {}
@failures = Concurrent::Map.new { [] }
@states = Concurrent::Map.new { State::UNLOCKED }
@lock = Monitor.new
end

def names
Expand All @@ -17,43 +22,41 @@ def get_all(light)
end

def get_failures(light)
all_failures[light.name] || []
all_failures[light.name]
end

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

def clear_failures(light)
failures = get_failures(light)
all_failures.delete(light.name)
failures
end

def get_state(light)
all_states[light.name] || State::UNLOCKED
all_states[light.name]
end

def set_state(light, state)
all_states[light.name] = state
end

def clear_state(light)
state = get_state(light)
all_states.delete(light.name)
state
end

private

def all_failures
@data['failures'] ||= {}
@failures
end

def all_states
@data['states'] ||= {}
@states
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions stoplight.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Gem::Specification.new do |gem|

gem.required_ruby_version = '>= 2'

gem.add_dependency 'concurrent-ruby'

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

0 comments on commit ad51fc8

Please sign in to comment.