Class: Kilden::Sender Private

Inherits:
Object
  • Object
show all
Defined in:
lib/kilden/sender.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Owns one batch from build to success or exhaustion (spec §4.3). Failed batches never go back into the main queue — that would shuffle ordering and could evict fresh events.

Constant Summary collapse

MAX_RETRIES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

3
GZIP_THRESHOLD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(write_key:, host:, transport:, logger:, sleeper: nil, rng: Random.new) ⇒ Sender

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Sender.



17
18
19
20
21
22
23
24
25
26
# File 'lib/kilden/sender.rb', line 17

def initialize(write_key:, host:, transport:, logger:, sleeper: nil, rng: Random.new)
  @write_key = write_key
  @capture_url = "#{host.chomp('/')}/capture"
  @transport = transport
  @logger = logger
  @sleeper = sleeper || ->(seconds) { sleep(seconds) }
  @rng = rng
  @dropped = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#dropped_countObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/kilden/sender.rb', line 15

def dropped_count
  @dropped_count
end

Instance Method Details

#dropped!(count) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/kilden/sender.rb', line 28

def dropped!(count)
  @mutex.synchronize { @dropped += count }
end

#dropped_totalObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/kilden/sender.rb', line 32

def dropped_total
  @mutex.synchronize { @dropped }
end

#send_batch(events, deadline: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends up to MAX_RETRIES + 1 attempts. deadline (monotonic seconds) cuts the loop short during shutdown: telemetry never hangs a process.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kilden/sender.rb', line 38

def send_batch(events, deadline: nil)
  return :ok if events.empty?

  attempt = 0
  loop do
    response = deliver(events)
    return :ok if success?(response)

    unless retryable?(response)
      drop(events,
           "kilden: dropped #{events.size} events (HTTP #{response.status}: #{response.body.to_s.strip[0, 120]})")
      return :dropped
    end

    attempt += 1
    if attempt > MAX_RETRIES || past?(deadline)
      drop(events, "kilden: dropped #{events.size} events after #{attempt} attempts")
      return :dropped
    end

    delay = backoff(attempt, response)
    if deadline && Process.clock_gettime(Process::CLOCK_MONOTONIC) + delay > deadline
      drop(events, "kilden: dropped #{events.size} events (shutdown deadline)")
      return :dropped
    end
    @sleeper.call(delay)
  end
end