Class: LogCourier::PendingPayload
- Inherits:
-
Object
- Object
- LogCourier::PendingPayload
- Defined in:
- lib/log-courier/client.rb
Overview
Describes a pending payload
Instance Attribute Summary collapse
-
#events ⇒ Object
Returns the value of attribute events.
-
#last_sequence ⇒ Object
Returns the value of attribute last_sequence.
-
#next ⇒ Object
Returns the value of attribute next.
-
#nonce ⇒ Object
Returns the value of attribute nonce.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#sequence_len ⇒ Object
Returns the value of attribute sequence_len.
Instance Method Summary collapse
- #ack(sequence) ⇒ Object
- #generate ⇒ Object
-
#initialize(events, nonce) ⇒ PendingPayload
constructor
A new instance of PendingPayload.
Constructor Details
#initialize(events, nonce) ⇒ PendingPayload
Returns a new instance of PendingPayload.
35 36 37 38 39 40 |
# File 'lib/log-courier/client.rb', line 35 def initialize(events, nonce) @events = events @nonce = nonce generate end |
Instance Attribute Details
#events ⇒ Object
Returns the value of attribute events.
33 34 35 |
# File 'lib/log-courier/client.rb', line 33 def events @events end |
#last_sequence ⇒ Object
Returns the value of attribute last_sequence.
33 34 35 |
# File 'lib/log-courier/client.rb', line 33 def last_sequence @last_sequence end |
#next ⇒ Object
Returns the value of attribute next.
33 34 35 |
# File 'lib/log-courier/client.rb', line 33 def next @next end |
#nonce ⇒ Object
Returns the value of attribute nonce.
33 34 35 |
# File 'lib/log-courier/client.rb', line 33 def nonce @nonce end |
#payload ⇒ Object
Returns the value of attribute payload.
33 34 35 |
# File 'lib/log-courier/client.rb', line 33 def payload @payload end |
#sequence_len ⇒ Object
Returns the value of attribute sequence_len.
33 34 35 |
# File 'lib/log-courier/client.rb', line 33 def sequence_len @sequence_len end |
Instance Method Details
#ack(sequence) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/log-courier/client.rb', line 60 def ack(sequence) return 0, false if sequence <= @last_sequence if sequence >= @sequence_len lines = @sequence_len - @last_sequence @last_sequence = sequence @payload = nil @events = [] return lines, true end lines = sequence - @last_sequence @last_sequence = sequence @payload = nil @events.shift(lines) [lines, false] end |
#generate ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/log-courier/client.rb', line 42 def generate raise ArgumentError, 'Corrupt payload' if @events.length.zero? buffer = Zlib::Deflate.new # Write each event in JSON format events.each do |event| json_data = MultiJson.dump(event) # Add length and then the data buffer << [json_data.bytesize].pack('N') << json_data end # Generate and store the payload @payload = nonce + buffer.finish @last_sequence = 0 @sequence_len = @events.length end |