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.
- #_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.
-
#_zoidberg_thread(thread) ⇒ TrueClass
Register a running thread for this instance.
-
#_zoidberg_unthread(thread) ⇒ TrueClass
Deregister a thread once it has completed.
-
#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 @_raw_threads = [] @_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 |
#_raw_threads ⇒ Hash<Integer:Thread> (readonly)
14 15 16 |
# File 'lib/zoidberg/proxy/liberated.rb', line 14 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.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/zoidberg/proxy/liberated.rb', line 111 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
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/zoidberg/proxy/liberated.rb', line 131 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.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/zoidberg/proxy/liberated.rb', line 149 def _zoidberg_destroy!(error=nil) object_string = self.inspect oid = _raw_instance.object_id ::Zoidberg.logger.debug "*** Destroying zoidberg instance #{object_string}" super do _raw_threads.map do |thread| 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 async thread, killing: #{thread.inspect}" thread.kill end end 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 |
#_zoidberg_thread(thread) ⇒ TrueClass
Register a running thread for this instance. Registered threads are tracked and killed on cleanup
93 94 95 96 |
# File 'lib/zoidberg/proxy/liberated.rb', line 93 def _zoidberg_thread(thread) _raw_threads.push(thread) true end |
#_zoidberg_unthread(thread) ⇒ TrueClass
Deregister a thread once it has completed
102 103 104 105 |
# File 'lib/zoidberg/proxy/liberated.rb', line 102 def _zoidberg_unthread(thread) _raw_threads.delete(thread) true end |