Sha256: 9d8fbce47246e815b604b7dc76bb82437ea6e53b5a1564baf55fcfa6348622f4

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../lib", __dir__)
require "celluloid/autostart"

class Ring
  include Celluloid

  class Node
    include Celluloid

    def initialize(link)
      @link = link
    end

    def around(n)
      @link.async.around n
    end
  end

  def initialize(size)
    @node = Node.new_link current_actor

    size.times do
      @node = Node.new_link @node
    end
  end

  # Go around the ring the given number of times
  def run(n)
    raise ArgumentError, "I can't go around a negative number of times" if n < 0

    async.around n
    wait :done
  end

  # Go around the ring the given number of times
  def around(n)
    if n.zero?
      signal :done
    else
      @node.async.around n - 1
    end
  end
end

if $PROGRAM_NAME == __FILE__
  require "benchmark"
  SIZE  = 512
  TIMES = 10
  ring = nil

  puts "*** Creating a #{SIZE} node ring..."
  puts Benchmark.measure {
    ring = Ring.new(SIZE)
  }

  puts "*** Sending a message around #{TIMES} times"
  puts Benchmark.measure {
    ring.run(TIMES)
  }
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
celluloid-0.18.0 examples/ring.rb
celluloid-0.18.0.pre2 examples/ring.rb