class PostHog::MessageBatch
A batch of ‘Message`s to be sent to the API
def <<(message)
def <<(message) begin message_json = message.to_json rescue StandardError => e raise JSONGenerationError, "Serialization error: #{e}" end message_json_size = message_json.bytesize if message_too_big?(message_json_size) logger.error('a message exceeded the maximum allowed size') else @messages << message @json_size += message_json_size + 1 # One byte for the comma end end
def clear
def clear @messages.clear @json_size = 0 end
def full?
def full? item_count_exhausted? || size_exhausted? end
def initialize(max_message_count)
def initialize(max_message_count) @messages = [] @max_message_count = max_message_count @json_size = 0 end
def item_count_exhausted?
def item_count_exhausted? @messages.length >= @max_message_count end
def message_too_big?(message_json_size)
def message_too_big?(message_json_size) message_json_size > Defaults::Message::MAX_BYTES end
def size_exhausted?
peeking, and to consider the next message size when calculating whether
The alternative is to use our own `Queue` implementation that allows
here is that we might fit in less messages than possible into a batch.
to use a native Ruby `Queue` that doesn't allow peeking. The tradeoff
message of the largest size possible. This is a shortcut that allows us
We consider the max size here as just enough to leave room for one more
def size_exhausted? @json_size >= (MAX_BYTES - Defaults::Message::MAX_BYTES) end