Class: Concurrent::Actor::Reference

Inherits:
Object
  • Object
show all
Includes:
PublicDelegations, TypeCheck
Defined in:
lib/concurrent-ruby-edge/concurrent/actor/reference.rb

Overview

Reference is public interface of Actor instances. It is used for sending messages and can be freely passed around the application. It also provides some basic information about the actor, see PublicDelegations.

AdHoc.spawn('printer') { -> message { puts message } }
# => #<Concurrent::Actor::Reference:0x7fd0d2883218 /printer (Concurrent::Actor::Utils::AdHoc)>
#                                   ^object_id     ^path     ^context class

Instance Method Summary collapse

Methods included from PublicDelegations

#context_class, #executor, #name, #parent, #path, #reference

Methods included from TypeCheck

#Child!, #Child?, #Match!, #Match?, #Type!, #Type?

Instance Method Details

#==(other) ⇒ Object



98
99
100
# File 'lib/concurrent-ruby-edge/concurrent/actor/reference.rb', line 98

def ==(other)
  Type? other, self.class and other.send(:core) == core
end

#ask(message, future = Concurrent::Promises.resolvable_future) ⇒ Promises::Future Also known as: ask_op

Note:

it’s a good practice to use #tell whenever possible. Results can be sent back with other messages. Ask should be used only for testing and when it returns very shortly. It can lead to deadlock if all threads in global_io_executor will block on while asking. It’s fine to use it form outside of actors and global_io_executor.

Returns supplied future.

Examples:

adder = AdHoc.spawn('adder') { -> message { message + 1 } }
adder.ask(1).value # => 2
adder.ask(nil).wait.reason # => #<NoMethodError: undefined method `+' for nil:NilClass>

Parameters:

  • message (Object)
  • future (Promises::Future) (defaults to: Concurrent::Promises.resolvable_future)

    to be fulfilled be message’s processing result

Returns:



52
53
54
# File 'lib/concurrent-ruby-edge/concurrent/actor/reference.rb', line 52

def ask(message, future = Concurrent::Promises.resolvable_future)
  message message, future
end

#ask!(message, future = Concurrent::Promises.resolvable_future) ⇒ Object

Note:

it’s a good practice to use #tell whenever possible. Results can be sent back with other messages. Ask should be used only for testing and when it returns very shortly. It can lead to deadlock if all threads in global_io_executor will block on while asking. It’s fine to use it form outside of actors and global_io_executor.

Sends the message synchronously and blocks until the message is processed. Raises on error.

Examples:

adder = AdHoc.spawn('adder') { -> message { message + 1 } }
adder.ask!(1) # => 2

Parameters:

  • message (Object)
  • future (Promises::Future) (defaults to: Concurrent::Promises.resolvable_future)

    to be fulfilled be message’s processing result

Returns:

  • (Object)

    message’s processing result

Raises:

  • (Exception)

    future.reason if future is #rejected?



73
74
75
# File 'lib/concurrent-ruby-edge/concurrent/actor/reference.rb', line 73

def ask!(message, future = Concurrent::Promises.resolvable_future)
  ask(message, future).value!
end

#dead_letter_routingObject



88
89
90
# File 'lib/concurrent-ruby-edge/concurrent/actor/reference.rb', line 88

def dead_letter_routing
  core.dead_letter_routing
end

#map(messages) ⇒ Object



77
78
79
# File 'lib/concurrent-ruby-edge/concurrent/actor/reference.rb', line 77

def map(messages)
  messages.map { |m| self.ask(m) }
end

#message(message, future = nil) ⇒ Object

behaves as #tell when no future and as #ask when future



82
83
84
85
# File 'lib/concurrent-ruby-edge/concurrent/actor/reference.rb', line 82

def message(message, future = nil)
  core.on_envelope Envelope.new(message, future, Actor.current || Thread.current, self)
  return future ? future.with_hidden_resolvable : self
end

#tell(message) ⇒ Reference Also known as: <<

Sends the message asynchronously to the actor and immediately returns ‘self` (the reference) allowing to chain message telling.

Examples:

printer = AdHoc.spawn('printer') { -> message { puts message } }
printer.tell('ping').tell('pong')
printer << 'ping' << 'pong'
# => 'ping'\n'pong'\n'ping'\n'pong'\n

Parameters:

  • message (Object)

Returns:



35
36
37
# File 'lib/concurrent-ruby-edge/concurrent/actor/reference.rb', line 35

def tell(message)
  message message, nil
end

#to_sObject Also known as: inspect



92
93
94
# File 'lib/concurrent-ruby-edge/concurrent/actor/reference.rb', line 92

def to_s
  format '%s %s (%s)>', super[0..-2], path, actor_class
end