Skip to content

Instantly share code, notes, and snippets.

@jrgns
Created January 6, 2017 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrgns/e4fc656582a624bfd7b8040a3260a648 to your computer and use it in GitHub Desktop.
Save jrgns/e4fc656582a624bfd7b8040a3260a648 to your computer and use it in GitHub Desktop.
def multi_receive_encoded(encoded_events)
bytes = 0
entries = []
# Split the events into multiple batches to ensure that no single batch
# exceeds `@message_max_size` bytes.
encoded_events.each_with_index do |encoded_event, index|
event, encoded = encoded_event
if encoded.bytesize > @message_max_size
@logger.warn('Message exceeds maximum length and will be dropped', :message_size => encoded.bytesize)
next
end
if entries.size >= @batch_events or (bytes + encoded.bytesize) > @message_max_size
send_message_batch(entries)
bytes = 0
entries = []
end
bytes += encoded.bytesize
entries.push(:id => index.to_s, :message_body => encoded)
end
send_message_batch(entries) unless entries.empty?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment