Feature #18814


Ractor: add method to query incoming message queue size

Added by phigrofi (Philipp Großelfinger) 8 months ago. Updated 21 days ago.

Target version:



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.


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.


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 = do
  loop { sleep(1) }
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.


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) 8 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

Updated by shyouhei (Shyouhei Urabe) 21 days ago

I heard from @ko1 (Koichi Sasada) in person that he doesn't need this feature to implement his ractor pool. From what I understand he would have a single ruby-level Queue instance for all incoming messages and instead of pushing those messages to each ractors one by one, he would let everyone pull messages from that queue at once.

But I might have missed his details. @ko1 (Koichi Sasada) please explain your detailed reason why golang's len() is not a good idea.


Also available in: Atom PDF