Skip to content

Commit f1e73be

Browse files
committed
Make to_msgpack thread safe by using Monitor
1 parent 3ca09d1 commit f1e73be

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/fluent/logger/fluent_logger.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ def initialize(tag_prefix = nil, *args)
8181
@socket_path = options[:socket_path]
8282
@nanosecond_precision = options[:nanosecond_precision]
8383

84-
factory = MessagePack::Factory.new
84+
@factory = MessagePack::Factory.new
8585
if @nanosecond_precision
86-
factory.register_type(EventTime::TYPE, EventTime)
86+
@factory.register_type(EventTime::TYPE, EventTime)
8787
end
88-
@packer = factory.packer
88+
@packer = @factory.packer
8989

9090
@mon = Monitor.new
9191
@pending = nil
@@ -182,9 +182,11 @@ def pending_bytesize
182182

183183
def to_msgpack(msg)
184184
begin
185-
res = @packer.pack(msg).to_s
186-
@packer.clear
187-
res
185+
@mon.synchronize {
186+
res = @packer.pack(msg).to_s
187+
@packer.clear
188+
res
189+
}
188190
rescue NoMethodError
189191
JSON.parse(JSON.generate(msg)).to_msgpack
190192
end

0 commit comments

Comments
 (0)