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