Class: Zoidberg::Proxy::Liberated
- Inherits:
-
Zoidberg::Proxy
- Object
- Zoidberg::Proxy
- Zoidberg::Proxy::Liberated
- Defined in:
- lib/zoidberg/proxy/liberated.rb
Constant Summary collapse
- THREAD_KILL_AFTER =
Time allowed for threads to gracefully die
5
Instance Attribute Summary collapse
-
#_locker ⇒ Thread
readonly
Current owner of lock.
- #_thread_pool ⇒ Concurrent::CachedThreadPool 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_signal_interface, #_zoidberg_timer, #_zoidberg_unexpected_error, #_zoidberg_unsupervise, #async, inherited, #inspect, register, registry, scrub!, #signal, #terminate, #to_s
Constructor Details
#initialize(klass, *args, &block) ⇒ self
Create a new proxy instance, new real instance, and link them
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/zoidberg/proxy/liberated.rb', line 19 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 @_thread_pool = ::Concurrent::CachedThreadPool.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
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/zoidberg/proxy/liberated.rb', line 32 def method_missing(*args, &block) res = nil @_accessing_threads << ::Thread.current begin _aquire_lock! @got_lock = true if(::ENV['ZOIDBERG_TESTING']) timer = ::Thread.new(::Thread.current) do |origin| begin time = ::ENV.fetch('ZOIDBERG_TESTING_TIMEOUT', 5).to_i ::Timeout.timeout(time) do ::Kernel.sleep(time) end nil rescue => error error end end res = @_raw_instance.__send__(*args, &block) if(timer.alive?) timer.kill else val = timer.value if(val.is_a?(Exception)) raise val end end else res = @_raw_instance.__send__(*args, &block) end rescue ::Zoidberg::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) ::Kernel.raise e ensure if(@got_lock) _release_lock! t_idx = @_accessing_threads.index(::Thread.current) @_accessing_threads.delete_at(t_idx) if t_idx end end res end |
Instance Attribute Details
#_locker ⇒ Thread (readonly)
Returns current owner of lock.
12 13 14 |
# File 'lib/zoidberg/proxy/liberated.rb', line 12 def _locker @_locker end |
#_thread_pool ⇒ Concurrent::CachedThreadPool (readonly)
14 15 16 |
# File 'lib/zoidberg/proxy/liberated.rb', line 14 def _thread_pool @_thread_pool end |
Instance Method Details
#_aquire_lock! ⇒ TrueClas
Aquire the lock to access real instance. If already locked, will wait until lock can be aquired.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/zoidberg/proxy/liberated.rb', line 92 def _aquire_lock! super do 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 end true end end |
#_release_lock! ⇒ TrueClass
Release the lock to access real instance
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/zoidberg/proxy/liberated.rb', line 112 def _release_lock! super do if(@_lock && @_locker == ::Thread.current) @_locker_count -= 1 if(@_locker_count < 1) @_locker = nil @_lock.unlock if @_lock.locked? end else false end end end |
#_zoidberg_available? ⇒ TrueClass, FalseClass
Returns currently unlocked.
84 85 86 |
# File 'lib/zoidberg/proxy/liberated.rb', line 84 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.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/zoidberg/proxy/liberated.rb', line 130 def _zoidberg_destroy!(error=nil) object_string = self.inspect oid = _raw_instance.object_id ::Zoidberg.logger.debug "*** Destroying zoidberg instance #{object_string}" super do _thread_pool.shutdown unless(_thread_pool.wait_for_termination(2)) _thread_pool.kill end @_accessing_threads.each do |thread| if(thread.alive?) begin thread.raise ::Zoidberg::DeadException.new('Instance in terminated state!', oid) ::Thread.new(thread) do |thread| next if thread == ::Thread.current thread.join(::Zoidberg::Proxy::Liberated::THREAD_KILL_AFTER) if(thread.alive?) ::Zoidberg.logger.error "Failed to halt accessing thread, killing: #{thread.inspect}" thread.kill end end rescue end end end @_accessing_threads.clear end ::Zoidberg.logger.debug "!!! Destroyed zoidberg instance #{object_string}" end |
#_zoidberg_locked? ⇒ TrueClass, FalseClass
Returns currently locked.
79 80 81 |
# File 'lib/zoidberg/proxy/liberated.rb', line 79 def _zoidberg_locked? @_lock && @_lock.locked? end |