From: phigrofi Date: 2022-06-02T10:57:24+00:00 Subject: [ruby-core:108754] [Ruby master Feature#18814] Ractor: add method to query incoming message queue size Issue #18814 has been reported by phigrofi (Philipp Gro��elfinger). ---------------------------------------- Feature #18814: Ractor: add method to query incoming message queue size https://bugs.ruby-lang.org/issues/18814 * Author: phigrofi (Philipp Gro��elfinger) * Status: Open * Priority: Normal ---------------------------------------- ## 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: ``` ruby 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](https://bugs.ruby-lang.org/issues/17679) a middleman solution was proposed which keeps track of a separate queue count. -- https://bugs.ruby-lang.org/ Unsubscribe: