Class: SemanticLogger::QueueProcessor::Inbox
- Inherits:
-
Object
- Object
- SemanticLogger::QueueProcessor::Inbox
- Defined in:
- lib/semantic_logger/queue_processor/inbox.rb
Overview
Internal: the queue on which log messages, and requests such as a flush, wait for the worker thread of a queue processor to take them off.
It knows the queue, whether its size is capped, and the process that created it, and adds to it, waiting for room on a full queue or not. After a fork, the child's copy of the queue holds the entries of the parent, which the parent's worker thread writes, so the child replaces it.
Also used in a signal trap, where Ruby allows a Queue to wait, but not a Mutex to be locked.
Constant Summary collapse
- WAIT_CHECK_SECONDS =
How often, in seconds, a thread waiting for room on the full queue asks whether to keep waiting (see #push), so that it does not wait for ever once nothing makes room.
1
Instance Attribute Summary collapse
-
#max_queue_size ⇒ Object
readonly
Returns [Integer] the most entries the queue holds, or -1 for no limit.
-
#queue ⇒ Object
readonly
Returns [Queue|SizedQueue] the queue.
Instance Method Summary collapse
-
#capped? ⇒ Boolean
Returns [true|false] whether the queue has a capped size.
-
#forked? ⇒ Boolean
Whether this is a child of the process that created the queue, whose copy of the queue then holds that process's entries.
-
#full? ⇒ Boolean
Returns [true|false] whether the queue is capped, and holds as many entries as it can.
-
#initialize(max_queue_size) ⇒ Inbox
constructor
A new instance of Inbox.
-
#push(item, target = queue, wait:) ⇒ Object
Add to the
targetqueue, the current one by default. -
#push_aside(items, otherwise, &adding) ⇒ Object
Add the items, in order, without making the calling thread wait for room on a full queue, while the block returns true.
-
#recreate ⇒ Object
Closes the queue, and creates an empty one, which belongs to this process.
-
#take(kind) ⇒ Object
Take the entries of the given class off the queue, and return them, in order.
Constructor Details
#initialize(max_queue_size) ⇒ Inbox
Returns a new instance of Inbox.
22 23 24 25 |
# File 'lib/semantic_logger/queue_processor/inbox.rb', line 22 def initialize(max_queue_size) @max_queue_size = max_queue_size create end |
Instance Attribute Details
#max_queue_size ⇒ Object (readonly)
Returns [Integer] the most entries the queue holds, or -1 for no limit.
20 21 22 |
# File 'lib/semantic_logger/queue_processor/inbox.rb', line 20 def max_queue_size @max_queue_size end |
#queue ⇒ Object (readonly)
Returns [Queue|SizedQueue] the queue.
17 18 19 |
# File 'lib/semantic_logger/queue_processor/inbox.rb', line 17 def queue @queue end |
Instance Method Details
#capped? ⇒ Boolean
Returns [true|false] whether the queue has a capped size.
28 |
# File 'lib/semantic_logger/queue_processor/inbox.rb', line 28 def capped? = @capped |
#forked? ⇒ Boolean
Whether this is a child of the process that created the queue, whose copy of the queue then holds that process's entries.
35 |
# File 'lib/semantic_logger/queue_processor/inbox.rb', line 35 def forked? = @pid != ::Process.pid |
#full? ⇒ Boolean
Returns [true|false] whether the queue is capped, and holds as many entries as it can.
31 |
# File 'lib/semantic_logger/queue_processor/inbox.rb', line 31 def full? = @capped && @queue.size >= max_queue_size |
#push(item, target = queue, wait:) ⇒ Object
Add to the target queue, the current one by default. Returns whether it was added.
A queue that is not capped is never full, and its #push takes no timeout. On a full queue,
without wait, returns false at once. With wait, waits for room while the block returns
true, which it is asked as soon as the queue is found full, then every WAIT_CHECK_SECONDS, and
returns false once it returns false, such as once the worker thread has stopped for good,
since then nothing makes room. The block is not asked when the queue has room.
50 51 52 53 54 55 56 57 58 |
# File 'lib/semantic_logger/queue_processor/inbox.rb', line 50 def push(item, target = queue, wait:) return target.push(item) && true unless capped? return true if target.push(item, timeout: 0) loop do return false unless wait && yield return true if target.push(item, timeout: WAIT_CHECK_SECONDS) end end |
#push_aside(items, otherwise, &adding) ⇒ Object
Add the items, in order, without making the calling thread wait for room on a full queue,
while the block returns true. Once the queue is full, one new thread adds the rest, in order,
waiting for room while the block returns true. Once the block returns false, at once or while
that thread waits, the items not yet added are passed to otherwise instead, on that thread.
Returns true, without waiting. Takes no lock, so that it can be called from a signal trap.
65 66 67 68 69 70 71 |
# File 'lib/semantic_logger/queue_processor/inbox.rb', line 65 def push_aside(items, otherwise, &adding) rest = items.drop_while { |item| adding.call && push(item, wait: false) } return true if rest.empty? Thread.new { otherwise.call(rest.drop_while { |item| adding.call && push(item, wait: true, &adding) }) } true end |
#recreate ⇒ Object
Closes the queue, and creates an empty one, which belongs to this process.
38 39 40 41 |
# File 'lib/semantic_logger/queue_processor/inbox.rb', line 38 def recreate @queue.close create end |
#take(kind) ⇒ Object
Take the entries of the given class off the queue, and return them, in order. The others are added back, in order, behind any added meanwhile, without waiting for room; yields each one that a thread waiting for room took the place of, so that it is not lost unseen. Takes no lock.
76 77 78 79 80 81 |
# File 'lib/semantic_logger/queue_processor/inbox.rb', line 76 def take(kind) entries = Array.new(@queue.size) { @queue.pop(timeout: 0) }.compact taken, others = entries.partition { |entry| entry.is_a?(kind) } others.each { |entry| push(entry, wait: false) || (yield(entry) if block_given?) } taken end |