Project

General

Profile

Actions

Feature #18814

open

Ractor: add method to query incoming message queue size

Added by phigrofi (Philipp Großelfinger) almost 2 years ago. Updated about 1 year ago.

Status:
Open
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.


Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #17679: Ractor incoming channel can consume unlimited resourcesAssignedko1 (Koichi Sasada)Actions

Updated by phigrofi (Philipp Großelfinger) almost 2 years 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) about 1 year 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.

Actions #4

Updated by jeremyevans0 (Jeremy Evans) 7 months ago

  • Related to Feature #17679: Ractor incoming channel can consume unlimited resources added
Actions

Also available in: Atom PDF

Like1
Like0Like0Like0Like0