Issue #21721 has been updated by jeremyevans0 (Jeremy Evans). I'm in favor of a LIFO-alternative to `Thread::Queue` and `Thread::SizedQueue` in core. However, it should not have queue in the name, as queue implies FIFO behavior. It should have stack in the name, as stack implies LIFO behavior. `Thread::Stack` and `Thread::SizedStack` would work. byroot (Jean Boussier) wrote:
However for connection pools you ideally want a LIFO queue because it's preferable to checkout the most recently used connection.
This isn't really related to this feature request, but I disagree that a stack is always desired for connection pools. It can result in situations where the bottom of the stack is not reached for a long time, and the connection ends up idle for too long and becomes invalid. Using a queue avoids that situation. A stack does have some advantages for connection pools, but it's a trade-off, a stack is not universally better than a queue for connection pooling in all respects. ---------------------------------------- Feature #21721: Allow `Queue` and `SizedQueue` to be used as LIFO queues https://bugs.ruby-lang.org/issues/21721#change-115411 * Author: byroot (Jean Boussier) * Status: Open ---------------------------------------- ### Context Since `Queue` and `SizedQueue` gained a proper timeout mechanism, I've been wanting to use them to implement simpler and more efficient connection pools. However for connection pools you ideally want a LIFO queue because it's preferable to checkout the most recently used connection. ### Problem Both `Queue` and `SizedQueue` only support FIFO because you can only enqueue elements at the beginning of the backing array, and only dequeue at the end. `Queue#push` (aliased as `Queue#<<` and `Queue#enq`) calls `Array#unshift` on the backing array and `Queue#pop` (aliased as `Queue#deq` and `Queue#shift`) calls `Array#pop`. Hence it is impossible to use these two classes for anything other than FIFO queues. I tried to use `git blame` to see if there was a justification for this, but I ended up in a git blame loop around May 2000 https://github.com/ruby/ruby/commit/9da4f78db46764be6dae5e7e83ff48cbecb3fb23 ### Feature I'd like to introduce either of two new methods (or both) to allow for LIFO: - A method to dequeue from the beginning of the backing array (`array_shift`). - A method to enqueue from the end of the backing array (`array_push`). A difficulty I have however is that since the common `shift/pop/push` terms are already used with uncommon meaning, it's hard to come up with good names. Ideas welcome. ### Possible alternatives - Define different classes for LIFO queues - But I find that less flexible, and it means exposing new constant names in the global namespace. - Add an initializer argument to define the queue ordering, e.g. `Queue.new([], lifo: true)` - Similarly, I find this less flexible than to decide the order during enqueue/dequeue - Add an argument to `Queue#pop` and `Queue#push`, e.g. `queue.push(element, front: true)`. -- https://bugs.ruby-lang.org/