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
13
14
15
16
17
18
19
# 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


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

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

#dispatch(obj) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/celluloid/call/sync.rb', line 21

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


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

def respond(message)
  @sender << message
end

#responseObject


47
48
49
# File 'lib/celluloid/call/sync.rb', line 47

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

#valueObject


51
52
53
# File 'lib/celluloid/call/sync.rb', line 51

def value
  response.value
end

#waitObject


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/celluloid/call/sync.rb', line 55

def wait
  loop do
    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