Class: Zoidberg::Proxy::Liberated
- Inherits:
-
Zoidberg::Proxy
- Object
- Zoidberg::Proxy
- Zoidberg::Proxy::Liberated
- Defined in:
- lib/zoidberg/proxy/liberated.rb
Instance Attribute Summary collapse
-
#_locker ⇒ Thread
readonly
Current owner of lock.
- #_raw_threads ⇒ Hash<Integer:Thread> readonly
Instance Method Summary collapse
-
#_aquire_lock! ⇒ TrueClas
Aquire the lock to access real instance.
-
#_release_lock! ⇒ TrueClass
Release the lock to access real instance.
-
#_zoidberg_available? ⇒ TrueClass, FalseClass
Currently unlocked.
-
#_zoidberg_destroy!(error = nil) ⇒ TrueClass
Ensure any async threads are killed and accessing threads are forced into error state.
-
#_zoidberg_locked? ⇒ TrueClass, FalseClass
Currently locked.
-
#initialize(klass, *args, &block) ⇒ self
constructor
Create a new proxy instance, new real instance, and link them.
-
#method_missing(*args, &block) ⇒ Object
Used to proxy request to real instance.
Methods inherited from Zoidberg::Proxy
#_zoidberg_handle_unexpected_error, #_zoidberg_link, #_zoidberg_link=, #_zoidberg_object, #_zoidberg_set_instance, #_zoidberg_signal, #_zoidberg_signal=, #_zoidberg_unexpected_error, inherited, register, registry, scrub!
Constructor Details
#initialize(klass, *args, &block) ⇒ self
Create a new proxy instance, new real instance, and link them
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/zoidberg/proxy/liberated.rb', line 16 def initialize(klass, *args, &block) @_build_args = [klass, args, block] @_lock = ::Mutex.new @_count_lock = ::Mutex.new @_accessing_threads = [] @_locker = nil @_locker_count = 0 @_zoidberg_signal = nil @_raw_threads = ::Smash.new{ ::Array.new } @_supervised = klass.ancestors.include?(::Zoidberg::Supervise) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
Used to proxy request to real instance
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/zoidberg/proxy/liberated.rb', line 29 def method_missing(*args, &block) res = nil @_accessing_threads << ::Thread.current begin _aquire_lock! if(::ENV['ZOIDBERG_TESTING']) ::Kernel.require 'timeout' ::Timeout.timeout(::ENV.fetch('ZOIDBERG_TESTING_TIMEOUT', 5).to_i) do res = @_raw_instance.__send__(*args, &block) end else res = @_raw_instance.__send__(*args, &block) end rescue ::Zoidberg::Supervise::AbortException => e ::Kernel.raise e.original_exception rescue ::Exception => e ::Zoidberg.logger.debug "Exception on: #{_raw_instance.class.name}##{args.first}(#{args.slice(1, args.size).map(&:inspect).join(', ')})" _zoidberg_unexpected_error(e) if(e.class.to_s == 'fatal' && !@_fatal_retry) @_fatal_retry = true retry else ::Kernel.raise e end ensure _release_lock! t_idx = @_accessing_threads.index(::Thread.current) @_accessing_threads.delete_at(t_idx) if t_idx end res end |
Instance Attribute Details
#_locker ⇒ Thread (readonly)
Returns current owner of lock.
9 10 11 |
# File 'lib/zoidberg/proxy/liberated.rb', line 9 def _locker @_locker end |
#_raw_threads ⇒ Hash<Integer:Thread> (readonly)
11 12 13 |
# File 'lib/zoidberg/proxy/liberated.rb', line 11 def _raw_threads @_raw_threads end |
Instance Method Details
#_aquire_lock! ⇒ TrueClas
Aquire the lock to access real instance. If already locked, will wait until lock can be aquired.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/zoidberg/proxy/liberated.rb', line 75 def _aquire_lock! if(@_lock) if(::ENV['ZOIDBERG_DEBUG'] == 'true') ::Timeout.timeout(::ENV.fetch('ZOIDBERG_DEBUG_TIMEOUT', 10).to_i) do @_lock.lock unless @_locker == ::Thread.current end else @_lock.lock unless @_locker == ::Thread.current end @_locker = ::Thread.current @_locker_count += 1 _zoidberg_signal(:locked) end true end |
#_release_lock! ⇒ TrueClass
Release the lock to access real instance
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/zoidberg/proxy/liberated.rb', line 94 def _release_lock! if(@_lock) if(@_locker == ::Thread.current) @_locker_count -= 1 if(@_locker_count < 1) @_locker = nil @_lock.unlock if @_lock.locked? end end _zoidberg_signal(:unlocked) end true end |
#_zoidberg_available? ⇒ TrueClass, FalseClass
Returns currently unlocked.
67 68 69 |
# File 'lib/zoidberg/proxy/liberated.rb', line 67 def _zoidberg_available? !_zoidberg_locked? end |
#_zoidberg_destroy!(error = nil) ⇒ TrueClass
Ensure any async threads are killed and accessing threads are forced into error state.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/zoidberg/proxy/liberated.rb', line 112 def _zoidberg_destroy!(error=nil) super do _raw_threads[_raw_instance.object_id].map do |thread| thread.raise ::Zoidberg::DeadException.new('Instance in terminated state!') end.map do |thread| thread.join(2) end.find_all(&:alive?).map(&:kill) _raw_threads.delete(_raw_instance.object_id) @_accessing_threads.each do |thr| if(thr.alive?) begin thr.raise ::Zoidberg::DeadException.new('Instance in terminated state!') rescue end end end @_accessing_threads.clear end end |
#_zoidberg_locked? ⇒ TrueClass, FalseClass
Returns currently locked.
62 63 64 |
# File 'lib/zoidberg/proxy/liberated.rb', line 62 def _zoidberg_locked? @_lock && @_lock.locked? end |