Class: Zoidberg::Proxy::Liberated

Inherits:
Zoidberg::Proxy show all
Defined in:
lib/zoidberg/proxy/liberated.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Zoidberg::Proxy

#_zoidberg_handle_unexpected_error, #_zoidberg_link, #_zoidberg_link=, #_zoidberg_object, #_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
27
28
29
30
# 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_instance = klass.unshelled_new(*args, &block)
  @_raw_instance._zoidberg_proxy(self)
  @_raw_threads = ::Smash.new{ ::Array.new }
  if(@_raw_instance.class.ancestors.include?(::Zoidberg::Supervise))
    @_supervised = true
  end
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



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
# File 'lib/zoidberg/proxy/liberated.rb', line 33

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

#_lockerThread (readonly)

Returns current owner of lock.

Returns:

  • (Thread)

    current owner of lock



9
10
11
# File 'lib/zoidberg/proxy/liberated.rb', line 9

def _locker
  @_locker
end

#_raw_threadsHash<Integer:Thread> (readonly)

Returns:

  • (Hash<Integer:Thread>)


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.

Returns:

  • (TrueClas)


79
80
81
82
83
84
85
86
87
# File 'lib/zoidberg/proxy/liberated.rb', line 79

def _aquire_lock!
  if(@_lock)
    @_lock.lock unless @_locker == ::Thread.current
    @_locker = ::Thread.current
    @_locker_count += 1
    _zoidberg_signal(:locked)
  end
  true
end

#_release_lock!TrueClass

Release the lock to access real instance

Returns:

  • (TrueClass)


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zoidberg/proxy/liberated.rb', line 92

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.

Returns:

  • (TrueClass, FalseClass)

    currently unlocked



71
72
73
# File 'lib/zoidberg/proxy/liberated.rb', line 71

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.

Returns:

  • (TrueClass)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/zoidberg/proxy/liberated.rb', line 110

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.

Returns:

  • (TrueClass, FalseClass)

    currently locked



66
67
68
# File 'lib/zoidberg/proxy/liberated.rb', line 66

def _zoidberg_locked?
  @_lock && @_lock.locked?
end