Class: Celluloid::Call::Sync

Inherits:
Celluloid::Call show all
Defined in:
lib/celluloid/call/sync.rb

Overview

Synchronous calls wait for a response

Instance Attribute Summary collapse

Attributes inherited from Celluloid::Call

#arguments, #block, #method

Instance Method Summary collapse

Methods inherited from Celluloid::Call

#check, #execute_block_on_receiver

Constructor Details

#initialize(sender, method, arguments = [], block = nil, task = Thread.current[:celluloid_task], chain_id = Internals::CallChain.current_id) ⇒ Sync

Returns a new instance of Sync


7
8
9
10
11
12
# File 'lib/celluloid/call/sync.rb', line 7

def initialize(sender, method, arguments = [], block = nil, task = Thread.current[:celluloid_task], chain_id = Internals::CallChain.current_id)
  super(method, arguments, block)
  @sender   = sender
  @task     = task
  @chain_id = chain_id || Celluloid.uuid
end

Instance Attribute Details

#chain_idObject (readonly)

Returns the value of attribute chain_id


5
6
7
# File 'lib/celluloid/call/sync.rb', line 5

def chain_id
  @chain_id
end

#senderObject (readonly)

Returns the value of attribute sender


5
6
7
# File 'lib/celluloid/call/sync.rb', line 5

def sender
  @sender
end

#taskObject (readonly)

Returns the value of attribute task


5
6
7
# File 'lib/celluloid/call/sync.rb', line 5

def task
  @task
end

Instance Method Details

#cleanupObject


31
32
33
34
# File 'lib/celluloid/call/sync.rb', line 31

def cleanup
  exception = DeadActorError.new("attempted to call a dead actor: #{self.method}")
  respond Internals::Response::Error.new(self, exception)
end

#dispatch(obj) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/celluloid/call/sync.rb', line 14

def dispatch(obj)
  Internals::CallChain.current_id = @chain_id
  result = super(obj)
  respond Internals::Response::Success.new(self, result)
rescue ::Exception => ex
  # Exceptions that occur during synchronous calls are reraised in the
  # context of the sender
  respond Internals::Response::Error.new(self, ex)

  # Aborting indicates a protocol error on the part of the sender
  # It should crash the sender, but the exception isn't reraised
  # Otherwise, it's a bug in this actor and should be reraised
  raise unless ex.is_a?(AbortError)
ensure
  Internals::CallChain.current_id = nil
end

#respond(message) ⇒ Object


36
37
38
# File 'lib/celluloid/call/sync.rb', line 36

def respond(message)
  @sender << message
end

#responseObject


40
41
42
# File 'lib/celluloid/call/sync.rb', line 40

def response
  Celluloid.suspend(:callwait, self)
end

#valueObject


44
45
46
# File 'lib/celluloid/call/sync.rb', line 44

def value
  response.value
end

#waitObject


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/celluloid/call/sync.rb', line 48

def wait
  while true
    message = Celluloid.mailbox.receive do |msg|
      msg.respond_to?(:call) && msg.call == self
    end

    if message.is_a?(SystemEvent)
      Thread.current[:celluloid_actor].handle_system_event(message)
    else
      # FIXME: add check for receiver block execution
      if message.respond_to?(:value)
        # FIXME: disable block execution if on :sender and (exclusive or outside of task)
        # probably now in Call
        return message
      else
        message.dispatch
      end
    end
  end
end