1
0
mirror of https://github.com/meineerde/rackstash.git synced 2026-01-31 17:27:13 +00:00

Replace spiked BufferStack#with_buffer with BufferStack#current

Using a block here is unnecessary and doesn't help us with any
thread-safty guarantees on deeply nested fields or tags. Thus, we can
just remove this and replace it with a simpler method returning the
top-most buffer.

When using this buffer, we still have to ensure that only a single
Thread can access it.
This commit is contained in:
Holger Just 2017-02-04 00:18:23 +01:00
parent 5abf10af2b
commit 83d82fada4
4 changed files with 30 additions and 20 deletions

View File

@ -16,11 +16,18 @@ module Rackstash
def initialize(sink)
@sink = sink
@stack = []
end
# TODO: this is only a spike for now
def with_buffer
yield Buffer.new(@sink)
# Get the current, i.e., latest, top-most, {Buffer} on the internal stack.
# If no Buffer was pushed yet with {#push}, this will be an implicit
# non-buffering Buffer and add it to the stack.
#
# @return [Buffer]
def current
@stack.last || Buffer.new(@sink, buffering: false).tap do |buffer|
@stack.push buffer
end
end
end
end

View File

@ -224,16 +224,13 @@ module Rackstash
end
end
now = Time.now.utc.freeze
buffer_stack.with_buffer do |buffer|
buffer.add_message Message.new(
msg,
time: now,
progname: progname,
severity: severity,
formatter: formatter
)
end
buffer.add_message Message.new(
msg,
time: Time.now.utc.freeze,
progname: progname,
severity: severity,
formatter: formatter
)
msg
end
@ -244,5 +241,9 @@ module Rackstash
def buffer_stack
@buffer_stack ||= Rackstash::BufferStack.new(@sink)
end
def buffer
buffer_stack.current
end
end
end

View File

@ -11,12 +11,14 @@ describe Rackstash::BufferStack do
let(:sink) { instance_double(Rackstash::Sink) }
let(:stack) { Rackstash::BufferStack.new(sink) }
describe '#with_buffer' do
describe '#current' do
it 'initializes a buffer' do
stack.with_buffer do |buffer|
expect(buffer).to be_a Rackstash::Buffer
expect(buffer.sink).to equal sink
end
expect(stack.current).to be_a Rackstash::Buffer
expect(stack.current.sink).to equal sink
end
it 'repeatedly returns the same buffer' do
expect(stack.current).to equal stack.current
end
end
end

View File

@ -115,9 +115,9 @@ describe Rackstash::Logger do
let(:buffer_stack) {
double('Rackstash::BufferStack').tap do |buffer_stack|
expect(buffer_stack).to receive(:with_buffer)
expect(buffer_stack).to receive(:current)
.at_least(:once)
.and_yield(buffer)
.and_return(buffer)
end
}