msgpack-ruby
msgpack-ruby copied to clipboard
Does msgpack_each support nested calls in design principle?
Hi,
I'm using fluentd to collect logs. I saw nested calls in error handle method of fluent-plugin-elasticsearch, and I simplified it to the following code,
# sorry, these code cannot run directly, so I add additional description
# chunk is file, and I guessed that chunk.msgpack_each invoked the following function
#
# def each(unpacker: nil, &block)
# open do |io|
# (unpacker || Fluent::MessagePackFactory.msgpack_unpacker(io)).each(&block)
# end
# nil
# end
# alias :msgpack_each :each
count = 0
chunk.msgpack_each do |time, record|
count += 1
chunk.msgpack_each do |time, record|
end
end
Let's assume chunk has 500 messages inside. If I comment out the nested call, count would be 500 finally. Otherwise, count would be less than 500.
So I want to ask if msgpack_each support nested calls in design principle.
Please help!
Source logical process in fluent-plugin-elasticsearch,
- When event comes,
write
method would be invoked, https://github.com/uken/fluent-plugin-elasticsearch/blob/728329b22143e921bb4f1f680daedda61412f403/lib/fluent/plugin/out_elasticsearch.rb#L825-L855 -
write
method is going tomsgpack_each
chunk, https://github.com/uken/fluent-plugin-elasticsearch/blob/728329b22143e921bb4f1f680daedda61412f403/lib/fluent/plugin/out_elasticsearch.rb#L841 - When condition met,
send_bulk
method would be invoked, https://github.com/uken/fluent-plugin-elasticsearch/blob/728329b22143e921bb4f1f680daedda61412f403/lib/fluent/plugin/out_elasticsearch.rb#L854-L862 - If
send_bulk
encounter error,handle_error
method would be invoked, https://github.com/uken/fluent-plugin-elasticsearch/blob/728329b22143e921bb4f1f680daedda61412f403/lib/fluent/plugin/out_elasticsearch.rb#L1114-L1117 -
handle_error
wouldmsgpack_each
the same chunk again, https://github.com/uken/fluent-plugin-elasticsearch/blob/728329b22143e921bb4f1f680daedda61412f403/lib/fluent/plugin/elasticsearch_error_handler.rb#L38-L50
This seems contrary to normal Ruby idioms to me. You can't do the same with Array#each
for example.
Open to discussion?
This seems contrary to normal Ruby idioms to me. You can't do the same with
Array#each
for example.
Sorry for replying late. As you comment, maybe it should not code like that, and i think there is no need to further discuss.
And I had commit a PR to fix that problem, by avoiding nested callding. :)