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}" }
= {}
["content-type"] = "application/json"
["content-encoding"] = "gzip" if @config.compress_events
Impl::Util.(@sdk_key, @config).each { |k, v| [k] = v }
unless is_diagnostic
["X-LaunchDarkly-Event-Schema"] = CURRENT_SCHEMA_VERSION.to_s
["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: ,
body: body,
})
rescue StandardError => exn
@logger.warn { "[LDClient] Error sending events: #{exn.inspect}." }
next
end
status = response.status.code
body = response.to_s
if status >= 200 && status < 300
res_time = nil
unless response.["date"].nil?
begin
res_time = Time.httpdate(response.["date"])
rescue ArgumentError
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
EventSenderResult.new(false, false, nil)
ensure
@http_client_pool.release(http_client)
end
end
|