Class: LogCourier::PendingPayload

Inherits:
Object
  • Object
show all
Defined in:
lib/log-courier/client.rb

Overview

Describes a pending payload

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#eventsObject

Returns the value of attribute events.



33
34
35
# File 'lib/log-courier/client.rb', line 33

def events
  @events
end

#last_sequenceObject

Returns the value of attribute last_sequence.



33
34
35
# File 'lib/log-courier/client.rb', line 33

def last_sequence
  @last_sequence
end

#nextObject

Returns the value of attribute next.



33
34
35
# File 'lib/log-courier/client.rb', line 33

def next
  @next
end

#nonceObject

Returns the value of attribute nonce.



33
34
35
# File 'lib/log-courier/client.rb', line 33

def nonce
  @nonce
end

#payloadObject

Returns the value of attribute payload.



33
34
35
# File 'lib/log-courier/client.rb', line 33

def payload
  @payload
end

#sequence_lenObject

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

#generateObject

Raises:

  • (ArgumentError)


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