Feature #18944
closedAdd SizedQueue#push(timeout:)
Description
While implementing [Feature #18774] It appeared to me that adding this timeout was quite obvious.
And @matz (Yukihiro Matsumoto) explicitly asked for a separate ticket to be opened for that feature if someone wanted it: https://github.com/ruby/dev-meeting-log/blob/master/DevMeeting-2022-05-19.md#feature-18774-add-queuepoptimeout-eregon
SizedQueue#push
already has a non_block
argument like pop
, so mirroring the change makes sense, and the implementations share a lot of code so it's really isn't that much maintainance.
As for the use case, I'd like to use a SizedQueue
in our StatsD
library, the code would look a bit like this (simplified code):
@event_queue = SizedQueue.new(MAX_BUFFER_SIZE)
@dispatcher_thread = Thread.new do
while event = event_queue.pop
send_event(event)
end
end
def emit_event(event)
unless @event_queue.push(event, timeout: 0.1)
if dispatcher_thread.alive?
StatsD.logger.warn("Dropped event")
else
StatsD.logger.warn("Dispatcher thread died, restarting it")
# ...
end
end
end
But more generally, any action that may block forever (or for very long) should offer a timeout even if it's only used to be able to log that something is not supposed to happen an then retry.