[ruby-core:109411] [Ruby master Feature#18944] Add SizedQueue#push(timeout:)
From:
"byroot (Jean Boussier)" <noreply@...>
Date:
2022-08-02 09:12:42 UTC
List:
ruby-core #109411
Issue #18944 has been updated by byroot (Jean Boussier).
Pull Request: https://github.com/ruby/ruby/pull/6207
----------------------------------------
Feature #18944: Add SizedQueue#push(timeout:)
https://bugs.ruby-lang.org/issues/18944#change-98561
* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
----------------------------------------
While implementing [Feature #18774] It appeared to me that adding this timeout was quite obvious.
And @matz 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](https://github.com/Shopify/statsd-instrument/blob/da8a74c7fbe8fdd421aa1df7eda2a996bc7c3f11/lib/statsd/instrument/batched_udp_sink.rb), the code would look a bit like this (simplified code):
```ruby
@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.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>