Project

General

Profile

Actions

Feature #18944

closed

Add SizedQueue#push(timeout:)

Added by byroot (Jean Boussier) over 2 years ago. Updated over 2 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:109348]

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0