Project

General

Profile

Actions

Feature #18814

open

Ractor: add method to query incoming message queue size

Added by phigrofi (Philipp Großelfinger) 2 months ago. Updated about 1 month ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:108754]

Description

Abstract

A simple method to query the current size of a Ractor's incoming queue from outside. Can be used to decide on the sender's side if a message is sent or postponed.

Background

Ractors have an infinite incoming message queue. When messages are sent to a Ractor it is not possible to check the current count of elements in the queue. A workaround would be: The receiving Ractor could immediately accept each message and put them into a separate queue and keep track of their count. Then the sending Ractor could query the count from the receiving Ractor as a message.
While this message exchange would be short and simple, it still requires the receiving Ractor to process the "queue-count" message and respond to it.

Proposal

The Ractor implementation already keeps track of the current incoming message fill level in the field sync.incoming_queue.cnt. A simple method in the ruby code of Ractor could expose this number so that it is simple to query the queue size from outside. This works without any interaction of the queried Ractor.

The code would work as follows:

ractor = Ractor.new do
  loop { sleep(1) }
end
ractor.queue_size #=> 0
ractor << "message"
ractor.queue_size #=> 1
ractor << "message"
ractor.queue_size #=> 2

Use cases

  1. Avoid queue overflow by checking queue size from outside before sending further messages.
  2. Incoming queue sizes can be monitored.

Discussion

The proposal makes it much easier to prevent overflow of a message queue than managing a separate queue inside of a Ractor and keeping track of its element count. I think also having a separate queue where the count needs to communicated, ignores the concept of a Ractor's incoming message queue and makes it quite complicated.

See also

In this issue a middleman solution was proposed which keeps track of a separate queue count.

Updated by phigrofi (Philipp Großelfinger) 2 months ago

For instance in GO it is possible to query the current size of a channel:

c := make(chan int, 100)
for i := 0; i < 34; i++ {
  c <- 0
}
fmt.Println(len(c)) // => 34
Actions

Also available in: Atom PDF