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.
Updated by byroot (Jean Boussier) over 2 years ago
Pull Request: https://github.com/ruby/ruby/pull/6207
Updated by matz (Yukihiro Matsumoto) over 2 years ago
It looks good to me.
Matz.
Updated by byroot (Jean Boussier) over 2 years ago
- Status changed from Open to Closed
Applied in changeset git|fe61cad7490da8a879597f851f4a89856d44838e.
Implement SizedQueue#push(timeout: sec)
[Feature #18944]
If both non_block=true
and timeout:
are supplied, ArgumentError
is raised.