Class: LaunchDarkly::Impl::EventSender

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/event_sender.rb

Overview

Since:

  • 5.5.0

Constant Summary collapse

CURRENT_SCHEMA_VERSION =

Since:

  • 5.5.0

4
DEFAULT_RETRY_INTERVAL =

Since:

  • 5.5.0

1

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, config, http_client = nil, retry_interval = DEFAULT_RETRY_INTERVAL) ⇒ EventSender

Returns a new instance of EventSender.

Since:

  • 5.5.0



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ldclient-rb/impl/event_sender.rb', line 16

def initialize(sdk_key, config, http_client = nil, retry_interval = DEFAULT_RETRY_INTERVAL)
  @sdk_key = sdk_key
  @config = config
  @events_uri = config.events_uri + "/bulk"
  @diagnostic_uri = config.events_uri + "/diagnostic"
  @logger = config.logger
  @retry_interval = retry_interval
  @http_client_pool = UnboundedPool.new(
    lambda { LaunchDarkly::Util.new_http_client(@config.events_uri, @config) },
    lambda { |client| client.close })
end

Instance Method Details

#send_event_data(event_data, description, is_diagnostic) ⇒ Object

Since:

  • 5.5.0



32
33
34
35
36
37
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ldclient-rb/impl/event_sender.rb', line 32

def send_event_data(event_data, description, is_diagnostic)
  uri = is_diagnostic ? @diagnostic_uri : @events_uri
  payload_id = is_diagnostic ? nil : SecureRandom.uuid
  begin
    http_client = @http_client_pool.acquire()
    response = nil
    2.times do |attempt|
      if attempt > 0
        @logger.warn { "[LDClient] Will retry posting events after #{@retry_interval} second" }
        sleep(@retry_interval)
      end
      begin
        @logger.debug { "[LDClient] sending #{description}: #{event_data}" }
        headers = {}
        headers["content-type"] = "application/json"
        headers["content-encoding"] = "gzip" if @config.compress_events
        Impl::Util.default_http_headers(@sdk_key, @config).each { |k, v| headers[k] = v }
        unless is_diagnostic
          headers["X-LaunchDarkly-Event-Schema"] = CURRENT_SCHEMA_VERSION.to_s
          headers["X-LaunchDarkly-Payload-ID"] = payload_id
        end

        body = event_data
        if @config.compress_events
          gzip = Zlib::GzipWriter.new(StringIO.new)
          gzip << event_data

          body = gzip.close.string
        end

        response = http_client.request("POST", uri, {
          headers: headers,
          body: body,
        })
      rescue StandardError => exn
        @logger.warn { "[LDClient] Error sending events: #{exn.inspect}." }
        next
      end
      status = response.status.code
      # must fully read body for persistent connections
      body = response.to_s
      if status >= 200 && status < 300
        res_time = nil
        unless response.headers["date"].nil?
          begin
            res_time = Time.httpdate(response.headers["date"])
          rescue ArgumentError
            # Ignored
          end
        end
        return EventSenderResult.new(true, false, res_time)
      end
      must_shutdown = !LaunchDarkly::Util.http_error_recoverable?(status)
      can_retry = !must_shutdown && attempt == 0
      message = LaunchDarkly::Util.http_error_message(status, "event delivery", can_retry ? "will retry" : "some events were dropped")
      @logger.error { "[LDClient] #{message}" }
      if must_shutdown
        return EventSenderResult.new(false, true, nil)
      end
    end
    # used up our retries
    EventSenderResult.new(false, false, nil)
  ensure
    @http_client_pool.release(http_client)
  end
end

#stopObject

Since:

  • 5.5.0



28
29
30
# File 'lib/ldclient-rb/impl/event_sender.rb', line 28

def stop
  @http_client_pool.dispose_all()
end