Skip to content

Commit

Permalink
log file size before and after encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
raghuramg committed Dec 30, 2024
1 parent 2580b2b commit 93612a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/event_source/operations/mime_encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module Operations
# A class for handling payload compression.
class MimeEncode
include Dry::Monads[:result, :do]

include EventSource::Logging

MIME_TYPES = %w[application/zlib application/json].freeze

# Compresses the payload into a compressed string using Zlib.
Expand Down Expand Up @@ -42,11 +43,21 @@ def validate_payload(payload, mime_type)

def encode(json_payload, mime_type)
encoded_data = Zlib.deflate(json_payload) if mime_type.to_s == 'application/zlib'


logger.debug "*" * 80
logger.debug "Starting payload encoding for MIME type: '#{mime_type}'"
logger.debug "Original payload size: #{data_size_in_kb(json_payload)} KB"
logger.debug "Encoded payload size: #{data_size_in_kb(encoded_data)} KB" if encoded_data
logger.debug "*" * 80

Success(encoded_data || json_payload)
rescue Zlib::Error => e
Failure("Failed to compress payload using Zlib: #{e.message}")
end

def data_size_in_kb(data)
(data.bytesize / 1024.0).round(2)
end
end
end
end
4 changes: 3 additions & 1 deletion lib/event_source/publish_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module EventSource
# Publish {EventSource::Event} messages
class PublishOperation
include EventSource::Logging

# @attr_reader [EventSource::Channel] channel the channel instance used by
# this PublishOperation
# @attr_reader [Object] subject instance of the protocol's publish class
Expand Down Expand Up @@ -39,7 +41,7 @@ def compress_payload_if_required(payload)

message_bindings = @async_api_publish_operation.message['bindings']
encoding = message_bindings.first[1]['contentEncoding'] if message_bindings
return payload if encoding.nil?
return payload unless encoding

output = EventSource::Operations::MimeEncode.new.call(encoding, payload)
if output.success?
Expand Down

0 comments on commit 93612a6

Please sign in to comment.