Feature #18814
openRactor: add method to query incoming message queue size
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¶
- Avoid queue overflow by checking queue size from outside before sending further messages.
- 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) over 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 phigrofi (Philipp Großelfinger) over 2 years ago
I created a PR for this issue: https://github.com/ruby/ruby/pull/5973
Updated by shyouhei (Shyouhei Urabe) almost 2 years 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.
Updated by jeremyevans0 (Jeremy Evans) about 1 year ago
- Related to Feature #17679: Ractor incoming channel can consume unlimited resources added
Updated by hsbt (Hiroshi SHIBATA) 2 months ago
- Status changed from Open to Assigned
- Assignee set to ko1 (Koichi Sasada)