2020-10-25 15:59:40 -04:00
|
|
|
class ClosedQueueError < StandardError; end
|
|
|
|
module Puma
|
|
|
|
|
|
|
|
# Queue#close was added in Ruby 2.3.
|
|
|
|
# Add a simple implementation for earlier Ruby versions.
|
|
|
|
#
|
|
|
|
module QueueClose
|
|
|
|
def close
|
2021-04-30 18:22:59 -04:00
|
|
|
num_waiting.times {push nil}
|
2020-10-25 15:59:40 -04:00
|
|
|
@closed = true
|
|
|
|
end
|
|
|
|
def closed?
|
2021-04-30 18:22:59 -04:00
|
|
|
@closed ||= false
|
2020-10-25 15:59:40 -04:00
|
|
|
end
|
|
|
|
def push(object)
|
2021-04-30 18:22:59 -04:00
|
|
|
raise ClosedQueueError if closed?
|
2020-10-25 15:59:40 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
alias << push
|
2021-04-30 18:22:59 -04:00
|
|
|
def pop(non_block=false)
|
|
|
|
return nil if !non_block && closed? && empty?
|
|
|
|
super
|
|
|
|
end
|
2020-10-06 09:22:53 -04:00
|
|
|
end
|
2020-10-25 15:59:40 -04:00
|
|
|
::Queue.prepend QueueClose
|
2020-10-06 09:22:53 -04:00
|
|
|
end
|