Class: Ratomic::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/ratomic/queue.rb

Overview

A Ractor-shareable multi-producer, multi-consumer queue.

Queue stores Ruby objects in a fixed-size native ring buffer. Push blocks when the queue is full; pop blocks when the queue is empty.

Examples:

Push and pop work

queue = Ratomic::Queue.new(128)
queue << "job"
queue.pop # => "job"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(capacity) ⇒ Ratomic::Queue

Create a queue with a fixed capacity.

Parameters:

  • capacity (Integer)

    maximum number of items the queue can hold

Returns:

Raises:

  • (ArgumentError)

    if capacity is outside the supported range

  • (TypeError)

    if capacity cannot be converted to an Integer



61
62
63
64
65
66
67
68
69
70
# File 'lib/ratomic/queue.rb', line 61

class Queue
  # Push an item and return the queue for chaining.
  #
  # @param item [Object] the item to append to the queue
  # @return [Ratomic::Queue] self
  def <<(item)
    push(item)
    self
  end
end

Instance Method Details

#<<(item) ⇒ Ratomic::Queue

Push an item and return the queue for chaining.

Parameters:

  • item (Object)

    the item to append to the queue

Returns:



66
67
68
69
# File 'lib/ratomic/queue.rb', line 66

def <<(item)
  push(item)
  self
end

#empty?Boolean

Check whether the queue currently appears empty.

Since this is a concurrent queue, the value is a moment-in-time observation.

Returns:

  • (Boolean)

    true when the queue currently has no items



61
62
63
64
65
66
67
68
69
70
# File 'lib/ratomic/queue.rb', line 61

class Queue
  # Push an item and return the queue for chaining.
  #
  # @param item [Object] the item to append to the queue
  # @return [Ratomic::Queue] self
  def <<(item)
    push(item)
    self
  end
end

#lengthInteger

Alias for #size.

Returns:

  • (Integer)

    the current number of queued items



61
62
63
64
65
66
67
68
69
70
# File 'lib/ratomic/queue.rb', line 61

class Queue
  # Push an item and return the queue for chaining.
  #
  # @param item [Object] the item to append to the queue
  # @return [Ratomic::Queue] self
  def <<(item)
    push(item)
    self
  end
end

#peekObject?

Return the next item without removing it.

Since this is a concurrent queue, the value is a moment-in-time observation.

Returns:

  • (Object, nil)

    the next item, or nil if the queue is empty



61
62
63
64
65
66
67
68
69
70
# File 'lib/ratomic/queue.rb', line 61

class Queue
  # Push an item and return the queue for chaining.
  #
  # @param item [Object] the item to append to the queue
  # @return [Ratomic::Queue] self
  def <<(item)
    push(item)
    self
  end
end

#popObject

Pop an item, blocking until one is available.

Returns:

  • (Object)

    the next queued item



61
62
63
64
65
66
67
68
69
70
# File 'lib/ratomic/queue.rb', line 61

class Queue
  # Push an item and return the queue for chaining.
  #
  # @param item [Object] the item to append to the queue
  # @return [Ratomic::Queue] self
  def <<(item)
    push(item)
    self
  end
end

#push(item) ⇒ Ratomic::Queue

Push an item, blocking until space is available.

Parameters:

  • item (Object)

    the item to append to the queue

Returns:



61
62
63
64
65
66
67
68
69
70
# File 'lib/ratomic/queue.rb', line 61

class Queue
  # Push an item and return the queue for chaining.
  #
  # @param item [Object] the item to append to the queue
  # @return [Ratomic::Queue] self
  def <<(item)
    push(item)
    self
  end
end

#sizeInteger

Return the current queue size.

Since this is a concurrent queue, the value is a moment-in-time observation.

Returns:

  • (Integer)

    the current number of queued items



61
62
63
64
65
66
67
68
69
70
# File 'lib/ratomic/queue.rb', line 61

class Queue
  # Push an item and return the queue for chaining.
  #
  # @param item [Object] the item to append to the queue
  # @return [Ratomic::Queue] self
  def <<(item)
    push(item)
    self
  end
end