Class: Concurrent::Actor::Utils::Balancer — Concurrent

Class List Method List File List

# Class: Concurrent::Actor::Utils::Balancer

Inherits:
  RestartingContext
  - Object
  - AbstractContext
  - RestartingContext
  - Concurrent::Actor::Utils::Balancer
  show all
Defined in:
  lib/concurrent/actor/utils/balancer.rb

## Overview

Distributes messages between subscribed actors. Each actor'll get only one message then it's unsubscribed. The actor needs to resubscribe when it's ready to receive next message. It will buffer the messages if there is no worker registered.

See Also:

- Pool

## Instance Attribute Summary (collapse)

- - (undocumented) core inherited from AbstractContext readonly

## Instance Method Summary (collapse)

- - (undocumented) distribute
- - (Balancer) initialize constructor

  A new instance of Balancer.

- - (undocumented) on_message(message)

## Constructor Details

### - (`Balancer`) initialize

Returns a new instance of Balancer

    11
    12
    13
    14
    # File 'lib/concurrent/actor/utils/balancer.rb', line 11

    def initialize
      @receivers = []
      @buffer    = []
    end

## Instance Attribute Details

### - (`undocumented`) core (readonly) Originally defined in class AbstractContext

## Instance Method Details

### - (`undocumented`) distribute

    35
    36
    37
    38
    39
    # File 'lib/concurrent/actor/utils/balancer.rb', line 35

    def distribute
      while !@receivers.empty? && !@buffer.empty?
        redirect @receivers.shift, @buffer.shift
      end
    end

### - (`undocumented`) on_message(message)

    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    # File 'lib/concurrent/actor/utils/balancer.rb', line 16

    def on_message(message)
      command, who = message
      case command
      when :subscribe
        @receivers << (who || envelope.sender)
        distribute
        true
      when :unsubscribe
        @receivers.delete(who || envelope.sender)
        true
      when :subscribed?
        @receivers.include?(who || envelope.sender)
      else
        @buffer << envelope
        distribute
        Behaviour::MESSAGE_PROCESSED
      end
    end