From 20c8a1026030ee57068da21c349fd19c564496ca Mon Sep 17 00:00:00 2001 From: Yuta Iwama Date: Mon, 30 Sep 2019 14:43:13 +0900 Subject: [PATCH] reuse same object after calling purge_obsolete_socks since @available_sockets is initialized with `Hash.new { |obj, k| obj[k] = [] }` at https://github.com/fluent/fluentd/blob/v1.7.1/lib/fluent/plugin/out_forward/socket_cache.rb#L27 Signed-off-by: Yuta Iwama --- lib/fluent/plugin/out_forward/socket_cache.rb | 4 +++- test/plugin/out_forward/test_socket_cache.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/fluent/plugin/out_forward/socket_cache.rb b/lib/fluent/plugin/out_forward/socket_cache.rb index 5501addc72..525988e9b3 100644 --- a/lib/fluent/plugin/out_forward/socket_cache.rb +++ b/lib/fluent/plugin/out_forward/socket_cache.rb @@ -81,7 +81,9 @@ def purge_obsolete_socks end end end - @available_sockets = @available_sockets.select { |_, v| !v.empty? } + + # reuse same object (@available_sockets) + @available_sockets.reject! { |_, v| v.empty? } sockets += @inactive_sockets @inactive_sockets.clear diff --git a/test/plugin/out_forward/test_socket_cache.rb b/test/plugin/out_forward/test_socket_cache.rb index 48d4508c8e..ffb3fb088c 100644 --- a/test/plugin/out_forward/test_socket_cache.rb +++ b/test/plugin/out_forward/test_socket_cache.rb @@ -38,6 +38,16 @@ class SocketCacheTest < Test::Unit::TestCase sock = mock!.open { new_sock }.subject assert_equal(new_sock, c.checkout_or('key') { sock.open }) end + + test 'reuse same hash object after calling purge_obsolete_socks' do + c = Fluent::Plugin::ForwardOutput::SocketCache.new(10, $log) + c.checkout_or('key') { 'socket' } + c.purge_obsolete_socks + + assert_nothing_raised(NoMethodError) do + c.checkout_or('key') { 'new socket' } + end + end end sub_test_case 'checkin' do