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

Use a different BufferStack per thread

That way, we can ensure that the BufferStack and the Buffers themselves
including their nested fields can not be accessed by different threads,
providing some thread safety for non malicious users.
This commit is contained in:
Holger Just 2017-02-04 01:04:18 +01:00
parent d78739d7d4
commit 3078c7d4ca
2 changed files with 18 additions and 1 deletions

View File

@ -3,6 +3,7 @@
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.
require 'concurrent'
require 'forwardable'
require 'rackstash/buffer_stack'
@ -51,6 +52,8 @@ module Rackstash
@level = DEBUG
@progname = PROGNAME
@formatter = Formatter.new
@buffer_stack = Concurrent::ThreadLocalVar.new
end
# Set the base log level as either one of the {SEVERITIES} or a
@ -239,7 +242,7 @@ module Rackstash
private
def buffer_stack
@buffer_stack ||= Rackstash::BufferStack.new(@sink)
@buffer_stack.value ||= BufferStack.new(@sink)
end
def buffer

View File

@ -321,4 +321,18 @@ describe Rackstash::Logger do
expect(messages.last).to include message: 'Unknown', severity: 5
end
end
context 'with multiple threads' do
it 'maintains thread-local stacks' do
first_stack = logger.send(:buffer_stack)
expect(first_stack).to be_a Rackstash::BufferStack
Thread.new do
second_stack = logger.send(:buffer_stack)
expect(second_stack).to be_a Rackstash::BufferStack
expect(second_stack).to_not eql first_stack
end.join
end
end
end