Class: Karafka::Messages::Message

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/karafka/messages/message.rb

Overview

It provides lazy loading not only until the first usage, but also allows us to skip using deserializer until we execute our logic. That way we can operate with heavy-deserialization data without slowing down the whole application.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_payload, metadata) ⇒ Message

Returns a new instance of Message.

Parameters:



25
26
27
28
29
30
# File 'lib/karafka/messages/message.rb', line 25

def initialize(raw_payload, )
  @raw_payload = raw_payload
  @metadata = 
  @deserialized = false
  @payload = nil
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



15
16
17
# File 'lib/karafka/messages/message.rb', line 15

def 
  @metadata
end

#raw_payloadObject

raw payload needs to be mutable as we want to have option to change it in the parser prior to the final deserialization



18
19
20
# File 'lib/karafka/messages/message.rb', line 18

def raw_payload
  @raw_payload
end

Instance Method Details

#deserialized?Boolean

Returns did we deserialize payload already.

Returns:

  • (Boolean)

    did we deserialize payload already



44
45
46
# File 'lib/karafka/messages/message.rb', line 44

def deserialized?
  @deserialized
end

#payloadObject

Returns lazy-deserialized data (deserialized upon first request).

Returns:

  • (Object)

    lazy-deserialized data (deserialized upon first request)



33
34
35
36
37
38
39
40
41
# File 'lib/karafka/messages/message.rb', line 33

def payload
  return @payload if deserialized?

  @payload = deserialize
  # We mark deserialization as successful after deserialization, as in case of an error
  # this won't be falsely set to true
  @deserialized = true
  @payload
end

#tombstone?Boolean

Returns true if the message has a key and raw payload is nil, it is a tombstone event. Otherwise it is not.

Returns:

  • (Boolean)

    true if the message has a key and raw payload is nil, it is a tombstone event. Otherwise it is not.



50
51
52
# File 'lib/karafka/messages/message.rb', line 50

def tombstone?
  !raw_key.nil? && @raw_payload.nil?
end