Is it possible to be notified if a SourceQueue has overflowed?

In my application, I have an Actor whose behavior involves offering an element into a SourceQueueWithCompletion for each message received by that Actor. If I use backpressure as the overflow strategy in the SourceQueue, it is my understanding that attempts to offer an item will result in a failed Future if there is already another offer pending (waiting for space in the queue). I should add that maxConcurrentOffers on the queue is set to 1.

If I were to choose a different overflow strategy, such as dropHead or dropTail, I believe that my application would have no way of knowing that any items in the queue have been dropped. Am I correct about that?

Would it be a reasonable enhancement request for some kind of overflow notification mechanism to be added to SourceQueue? For example, there could me a method watchForOverflow that returns a Future[OverflowNotification] that would complete if any item is ever dropped from the queue (or if the queue is closed).

As an alternative to that, would it be reasonable for SourceQueue to have a method to inquire about the current number of elements in the queue, so that my application could know that a subsequent call to offer might trigger an overflow? I realize that it would be impossible for this inquiry to guarantee anything about what a subsequent offer would do, but it would at least allow me to monitor whether I have configured my SourceQueue size appropriately.

offer returns a Future[QueueOfferResult] that you can check for hints about whether an element was enqueued or not.

If you need a dropping queue make sure to use the constructor returning a BoundedSourceQueue which has the best performance. The underlying data structures often don’t offer a way to query the size of the queue because maintaining an accurate counter would introduce contention between producers and consumers hampering performance.

1 Like

Thanks for the reply, @jrudolph. Your suggestion to use Source.queue, which returns a BoundedSourceQueue instead of a SourceQueue seems like it should work. The former does indeed support a size() method, while the latter does not.

@jrudolph - I understand the rationale for not offering a way to query the size of a SourceQueue. But I still think it would be useful if SourceQueue provided a method that would enable an application to receive a call-back if that queue ever drops any items that have been accepted into the queue.