def linking_request(receiver, type)
      Celluloid.exclusive do
        receiver.mailbox << LinkingRequest.new(Actor.current, type)
        system_events = []

        Timers::Wait.for(LINKING_TIMEOUT) do |remaining|
          begin
            message = @mailbox.receive(remaining) do |msg|
              msg.is_a?(LinkingResponse) &&
                msg.actor.mailbox.address == receiver.mailbox.address &&
                msg.type == type
            end
          rescue TaskTimeout
            next # IO reactor did something, no message in queue yet.
          end

          if message.instance_of? LinkingResponse
            # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
            # rubocop:disable Style/GlobalVars
            Celluloid::Probe.actors_linked(self, receiver) if $CELLULOID_MONITORING
            # rubocop:enable Style/GlobalVars
            system_events.each { |ev| @mailbox << ev }
            return
          elsif message.is_a? SystemEvent
            # Queue up pending system events to be processed after we've successfully linked
            system_events << message
          else raise "Unexpected message type: #{message.class}. Expected LinkingResponse, NilClass, SystemEvent."
          end
        end

        raise TaskTimeout, "linking timeout of #{LINKING_TIMEOUT} seconds exceeded with receiver: #{receiver}"
      end
    end
def linking_request(receiver, type)
      Celluloid.exclusive do
        receiver.mailbox << LinkingRequest.new(Actor.current, type)
        system_events = []

        Timers::Wait.for(LINKING_TIMEOUT) do |remaining|
          begin
            message = @mailbox.receive(remaining) do |msg|
              msg.is_a?(LinkingResponse) &&
                msg.actor.mailbox.address == receiver.mailbox.address &&
                msg.type == type
            end
          rescue TaskTimeout
            next # IO reactor did something, no message in queue yet.
          end

          if message.instance_of? LinkingResponse
            # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
            # rubocop:disable Style/GlobalVars
            Celluloid::Probe.actors_linked(self, receiver) if $CELLULOID_MONITORING
            # rubocop:enable Style/GlobalVars
            system_events.each { |ev| @mailbox << ev }
            return
          elsif message.is_a? SystemEvent
            # Queue up pending system events to be processed after we've successfully linked
            system_events << message
          else raise "Unexpected message type: #{message.class}. Expected LinkingResponse, NilClass, SystemEvent."
          end
        end

        raise TaskTimeout, "linking timeout of #{LINKING_TIMEOUT} seconds exceeded with receiver: #{receiver}"
      end
    end
Perform a linking request with another actor