Module: WaterDrop::Producer::Buffer

Included in:
WaterDrop::Producer
Defined in:
lib/waterdrop/producer/buffer.rb

Overview

Component for buffered operations

Instance Method Summary collapse

Instance Method Details

#buffer(message) ⇒ Object

Adds given message into the internal producer buffer without flushing it to Kafka

Parameters:

Raises:



12
13
14
15
16
17
18
19
20
21
# File 'lib/waterdrop/producer/buffer.rb', line 12

def buffer(message)
  ensure_active!

  @monitor.instrument(
    'message.buffered',
    producer_id: id,
    message: message,
    buffer: @messages
  ) { @messages << message }
end

#buffer_many(messages) ⇒ Object

Adds given messages into the internal producer buffer without flushing them to Kafka

Parameters:

  • messages (Array<Hash>)

    array with messages that comply with the Contracts::Message contract

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/waterdrop/producer/buffer.rb', line 29

def buffer_many(messages)
  ensure_active!

  @monitor.instrument(
    'messages.buffered',
    producer_id: id,
    messages: messages,
    buffer: @messages
  ) do
    messages.each { |message| @messages << message }
    messages
  end
end

#flush_asyncArray<Rdkafka::Producer::DeliveryHandle>

Flushes the internal buffer to Kafka in an async way

Returns:

  • (Array<Rdkafka::Producer::DeliveryHandle>)

    delivery handles for messages that were flushed



46
47
48
49
50
51
52
# File 'lib/waterdrop/producer/buffer.rb', line 46

def flush_async
  @monitor.instrument(
    'buffer.flushed_async',
    producer_id: id,
    messages: @messages
  ) { flush(false) }
end

#flush_syncArray<Rdkafka::Producer::DeliveryReport>

Flushes the internal buffer to Kafka in a sync way

Returns:

  • (Array<Rdkafka::Producer::DeliveryReport>)

    delivery reports for messages that were flushed



57
58
59
60
61
62
63
# File 'lib/waterdrop/producer/buffer.rb', line 57

def flush_sync
  @monitor.instrument(
    'buffer.flushed_sync',
    producer_id: id,
    messages: @messages
  ) { flush(true) }
end