Class: Concurrent::Actor::Reference

Inherits:
Object
  • Object
show all
Includes:
PublicDelegations, TypeCheck
Defined in:
lib/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)

Instance Method Details

- (undocumented) ==(other)



97
98
99
# File 'lib/concurrent/actor/reference.rb', line 97

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

- (Promises::Future) ask(message, future = Concurrent::Promises.resolvable_future)

Note:

it's a good practice to use tell whenever possible. Ask should be used only for

Note:

it's a good practice to use #tell whenever possible. Results can be send 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.

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.

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:



54
55
56
# File 'lib/concurrent/actor/reference.rb', line 54

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

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

Note:

it's a good practice to use #tell whenever possible. Results can be send 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?



72
73
74
# File 'lib/concurrent/actor/reference.rb', line 72

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

- (undocumented) Child!(value, *types) Originally defined in module TypeCheck

- (Boolean) Child?(value, *types) Originally defined in module TypeCheck

Returns:

  • (Boolean)

- (undocumented) context_class Also known as: actor_class Originally defined in module PublicDelegations

See Also:

- (undocumented) dead_letter_routing



87
88
89
# File 'lib/concurrent/actor/reference.rb', line 87

def dead_letter_routing
  core.dead_letter_routing
end

- (undocumented) executor Originally defined in module PublicDelegations

See Also:

- (undocumented) map(messages)



76
77
78
# File 'lib/concurrent/actor/reference.rb', line 76

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

- (undocumented) Match!(value, *types) Originally defined in module TypeCheck

- (Boolean) Match?(value, *types) Originally defined in module TypeCheck

Returns:

  • (Boolean)

- (undocumented) message(message, future = nil)

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



81
82
83
84
# File 'lib/concurrent/actor/reference.rb', line 81

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

- (undocumented) name Originally defined in module PublicDelegations

See Also:

- (undocumented) parent Originally defined in module PublicDelegations

See Also:

- (undocumented) path Originally defined in module PublicDelegations

See Also:

- (undocumented) reference Also known as: ref Originally defined in module PublicDelegations

See Also:

- (Reference) tell(message) 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:



32
33
34
# File 'lib/concurrent/actor/reference.rb', line 32

def tell(message)
  message message, nil
end

- (undocumented) to_s Also known as: inspect



91
92
93
# File 'lib/concurrent/actor/reference.rb', line 91

def to_s
  "#<#{self.class}:0x#{'%x' % (object_id << 1)} #{path} (#{actor_class})>"
end

- (undocumented) Type!(value, *types) Originally defined in module TypeCheck

- (Boolean) Type?(value, *types) Originally defined in module TypeCheck

Returns:

  • (Boolean)