Skip to content

Commit

Permalink
Change to State from Enum, updated to match merged PR, removed Mutex …
Browse files Browse the repository at this point in the history
…code
  • Loading branch information
kyewei committed Nov 12, 2015
1 parent 7ab2f36 commit 2e9cd7a
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 261 deletions.
29 changes: 2 additions & 27 deletions lib/semian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,6 @@ def destroy(name)
def resources
@resources ||= {}
end

module ReentrantMutex
attr_reader :mutex

def call_with_mutex
@mutex ||= Monitor.new
@mutex.synchronize do
yield if block_given?
end
end

def self.included(base)
def base.surround_with_mutex(*names)
names.each do |name|
new_name = "#{name}_inner".freeze
alias_method new_name, name
define_method(name) do |*args, &block|
call_with_mutex do
method(new_name).call(*args, &block)
end
end
end
end
end
end
end

require 'semian/resource'
Expand All @@ -185,10 +160,10 @@ def base.surround_with_mutex(*names)
require 'semian/shared_memory_object'
require 'semian/simple_sliding_window'
require 'semian/simple_integer'
require 'semian/simple_enum'
require 'semian/simple_state'
require 'semian/sysv_sliding_window'
require 'semian/sysv_integer'
require 'semian/sysv_enum'
require 'semian/sysv_state'
if Semian.semaphores_enabled?
require 'semian/semian'
else
Expand Down
28 changes: 10 additions & 18 deletions lib/semian/circuit_breaker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Semian
class CircuitBreaker #:nodoc:
extend Forwardable

def initialize(name, exceptions:, success_threshold:, error_threshold:, error_timeout:, permissions:, implementation:)
@name = name.to_s
@success_count_threshold = success_threshold
Expand All @@ -12,9 +14,8 @@ def initialize(name, exceptions:, success_threshold:, error_threshold:, error_ti
permissions: permissions)
@successes = implementation::Integer.new(name: "#{name}_sysv_integer",
permissions: permissions)
@state = implementation::Enum.new(symbol_list: [:closed, :half_open, :open],
name: "#{name}_sysv_enum",
permissions: permissions)
@state = implementation::State.new(name: "#{name}_sysv_state",
permissions: permissions)
end

def acquire
Expand Down Expand Up @@ -67,33 +68,24 @@ def destroy

private

def closed?
@state.value == :closed
end
def_delegators :@state, :closed?, :open?, :half_open?
private :closed?, :open?, :half_open?

def close
log_state_transition(:closed)
@state.value = :closed
@state.close
@errors.clear
end

def open?
@state.value == :open
end

def open
log_state_transition(:open)
@state.value = :open
end

def half_open?
@state.value == :half_open
@state.open
end

def half_open
log_state_transition(:half_open)
@state.value = :half_open
@successes.value = 0
@state.half_open
@successes.reset
end

def success_threshold_reached?
Expand Down
2 changes: 0 additions & 2 deletions lib/semian/shared_memory_object.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Semian
class SharedMemoryObject #:nodoc:
include ReentrantMutex

@type_size = {}
def self.sizeof(type)
size = (@type_size[type.to_sym] ||= (respond_to?(:_sizeof) ? _sizeof(type.to_sym) : 0))
Expand Down
42 changes: 0 additions & 42 deletions lib/semian/simple_enum.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/semian/simple_integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ def destroy
@value = 0
end
end

surround_with_mutex :value, :value=, :increment
end
end
end
3 changes: 0 additions & 3 deletions lib/semian/simple_sliding_window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ def destroy
clear
end
end

surround_with_mutex :size, :pop, :shift, :first, :last, :max_size,
:resize_to, :<<, :push, :clear
end
end
end
45 changes: 45 additions & 0 deletions lib/semian/simple_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'forwardable'

module Semian
module Simple
class State < SharedMemoryObject #:nodoc:
def initialize
reset
end

attr_reader :value

def open?
value == :open
end

def closed?
value == :closed
end

def half_open?
value == :half_open
end

def open
@value = :open
end

def close
@value = :closed
end

def half_open
@value = :half_open
end

def reset
close
end

def destroy
reset
end
end
end
end
10 changes: 0 additions & 10 deletions lib/semian/sysv_enum.rb

This file was deleted.

56 changes: 56 additions & 0 deletions lib/semian/sysv_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Semian
module SysV
class State < Semian::Simple::State #:nodoc:
extend Forwardable

def_delegators :@integer, :semid, :shmid, :execute_atomically, :transaction,
:shared?, :acquire_memory_object, :bind_init_fn
private :shared?, :acquire_memory_object, :bind_init_fn

def initialize(name:, permissions:)
@integer = Semian::SysV::Integer.new(name: name, permissions: permissions)
initialize_lookup([:closed, :open, :half_open])
end

def open
self.value = :open
end

def close
self.value = :closed
end

def half_open
self.value = :half_open
end

def reset
close
end

def destroy
reset
@integer.destroy
end

def value
@num_to_sym.fetch(@integer.value) { raise ArgumentError }
end

private

def value=(sym)
@integer.value = @sym_to_num.fetch(sym) { raise ArgumentError }
end

def initialize_lookup(symbol_list)
# Assume symbol_list[0] is mapped to 0
# Cannot just use #object_id since #object_id for symbols is different in every run
# For now, implement a C-style enum type backed by integers

@sym_to_num = Hash[symbol_list.each_with_index.to_a]
@num_to_sym = @sym_to_num.invert
end
end
end
end
2 changes: 1 addition & 1 deletion test/circuit_breaker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_shared_fresh_worker_killed_should_not_reset_circuit_breaker_data
Process.waitall
fork do
Semian.register(:unique_res, tickets: 1, exceptions: [SomeError], error_threshold: 2, error_timeout: 5, success_threshold: 1)
assert_circuit_opened
assert_circuit_opened Semian[:unique_res]
end

Process.waitall
Expand Down
47 changes: 0 additions & 47 deletions test/simple_enum_test.rb

This file was deleted.

45 changes: 45 additions & 0 deletions test/simple_state_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'test_helper'

class TestSimpleState < MiniTest::Unit::TestCase
CLASS = ::Semian::Simple::State

def setup
@state = CLASS.new
end

def teardown
@state.destroy
end

module StateTestCases
def test_start_closed?
assert @state.closed?
end

def test_open
@state.open
assert @state.open?
assert_equal @state.value, :open
end

def test_close
@state.close
assert @state.closed?
assert_equal @state.value, :closed
end

def test_half_open
@state.half_open
assert @state.half_open?
assert_equal @state.value, :half_open
end

def test_reset
@state.reset
assert @state.closed?
assert_equal @state.value, :closed
end
end

include StateTestCases
end
Loading

0 comments on commit 2e9cd7a

Please sign in to comment.