posthog-ruby 3.15.0 → 3.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab01f90a9fbe05af027e92831b58e906eb38b30914230fd83a35cab28da40fcd
4
- data.tar.gz: cf06c8facfe324712e096cedfeeef90c755705bcce79384cda2d7b5f4d675cf0
3
+ metadata.gz: edb7fbf2f848ccb05f6a79f9c3429d395ac39cde5fdd1f83c59d97990231cbc8
4
+ data.tar.gz: bf96ccaccf1cf2117b502311895d95eb32de786b005809bf1d03471c67fc3920
5
5
  SHA512:
6
- metadata.gz: 6825662770b6a96fce094e2c46619fe142144ef8098db49c6a67b4f1faf54d731b65c8767f985413c91bf097767db84648fe2b2ba938ca4f788a0201ffa6002e
7
- data.tar.gz: 8501399ef589da90e3e7c65629df24d99012240c718874a506fede7833225316a1c97881a8eca1df06a831eace6e9090e3b052686c7656c8cd9a3a1815e3a6b7
6
+ metadata.gz: a60d451d311741d7d3407515a8b05a0029a24d098445aae51fe4c4d51ab894c4005c0f78267f4d5717681a258ef83091e62e508d8f902068a6e9321ceead274b
7
+ data.tar.gz: 01b522405e918921a9667a1cf47271b314cf55cada33687ea31d8c2ed04239f602f61adf35c891920691c25a10d0ac6ad76c816ef2160b3ccfaf6208e564baa1
@@ -33,7 +33,7 @@ module PostHog
33
33
 
34
34
  @attempts += 1
35
35
 
36
- [interval, @max_timeout_ms].min
36
+ interval.clamp(@min_timeout_ms, @max_timeout_ms)
37
37
  end
38
38
 
39
39
  private
@@ -67,6 +67,8 @@ module PostHog
67
67
  # in seconds. Defaults to 30.
68
68
  # @option opts [Integer] :feature_flag_request_timeout_seconds How long to wait for feature flag evaluation,
69
69
  # in seconds. Defaults to 3.
70
+ # @option opts [Integer] :max_retries How many times to retry batch uploads after the first send attempt.
71
+ # Defaults to the transport default. Set to 0 to disable retrying.
70
72
  # @option opts [Integer] :feature_flag_request_max_retries How many times to retry a flag request after a
71
73
  # transient network error. Each retry sleeps on the calling thread before retrying, so this adds to
72
74
  # worst-case latency. Defaults to 1. Set to 0 to disable retrying.
@@ -110,7 +112,7 @@ module PostHog
110
112
  @transport = Transport.new(
111
113
  api_host: opts[:host],
112
114
  skip_ssl_verification: opts[:skip_ssl_verification],
113
- retries: 3,
115
+ retries: opts.key?(:max_retries) ? opts[:max_retries].to_i + 1 : 3,
114
116
  compress_request: opts[:compress_request]
115
117
  )
116
118
  @sync_lock = Mutex.new
@@ -150,7 +150,7 @@ module PostHog
150
150
  group_properties: group_properties
151
151
  }
152
152
  request_data[:flag_keys_to_evaluate] = flag_keys if flag_keys && !flag_keys.empty?
153
- request_data[:geoip_disable] = true if disable_geoip
153
+ request_data[:geoip_disable] = disable_geoip unless disable_geoip.nil?
154
154
 
155
155
  flags_response = _request_feature_flag_evaluation(request_data)
156
156
 
@@ -48,6 +48,7 @@ module PostHog
48
48
  skip_ssl_verification: options[:skip_ssl_verification],
49
49
  compress_request: options[:compress_request]
50
50
  }
51
+ @transport_options[:retries] = options[:max_retries].to_i + 1 if options.key?(:max_retries)
51
52
  @transport = Transport.new(@transport_options)
52
53
  end
53
54
 
@@ -8,6 +8,7 @@ require 'posthog/backoff_policy'
8
8
  require 'net/http'
9
9
  require 'net/https'
10
10
  require 'json'
11
+ require 'time'
11
12
  require 'zlib'
12
13
 
13
14
  module PostHog
@@ -42,11 +43,12 @@ module PostHog
42
43
  options[:port] = options[:port].nil? ? PORT : options[:port]
43
44
  options[:ssl] = options[:ssl].nil? ? SSL : options[:ssl]
44
45
 
45
- @headers = options[:headers] || HEADERS
46
+ @headers = (options[:headers] || HEADERS).dup
46
47
  @path = options[:path] || PATH
47
48
  @retries = options[:retries] || RETRIES
48
49
  @backoff_policy = options[:backoff_policy] || PostHog::BackoffPolicy.new
49
50
  @compress_request = options[:compress_request] != false
51
+ @last_retry_after = nil
50
52
 
51
53
  http = Net::HTTP.new(options[:host], options[:port])
52
54
  http.use_ssl = options[:ssl]
@@ -103,10 +105,8 @@ module PostHog
103
105
  private
104
106
 
105
107
  def should_retry_request?(status_code, body)
106
- if status_code >= 500
107
- true # Server error
108
- elsif status_code == 429 # rubocop:disable Lint/DuplicateBranch
109
- true # Rate limited
108
+ if status_code >= 500 || [408, 429].include?(status_code)
109
+ true # Server error, request timeout, or rate limited
110
110
  elsif status_code >= 400
111
111
  logger.error(body)
112
112
  false # Client error. Do not retry, but log
@@ -136,15 +136,37 @@ module PostHog
136
136
 
137
137
  if should_retry && (retries_remaining > 1)
138
138
  logger.debug("Retrying request, #{retries_remaining} retries left")
139
- sleep(@backoff_policy.next_interval.to_f / 1000)
139
+ sleep(retry_delay_seconds)
140
140
  retry_with_backoff(retries_remaining - 1, &block)
141
141
  else
142
142
  [result, caught_exception]
143
143
  end
144
144
  end
145
145
 
146
+ def retry_delay_seconds
147
+ retry_after = parse_retry_after(@last_retry_after)
148
+ @last_retry_after = nil
149
+ return retry_after if retry_after
150
+
151
+ @backoff_policy.next_interval.to_f / 1000
152
+ end
153
+
154
+ def parse_retry_after(value)
155
+ return nil if value.nil? || value.empty?
156
+
157
+ seconds = Float(value, exception: false)
158
+ return seconds if seconds && seconds >= 0
159
+
160
+ parsed_time = Time.httpdate(value)
161
+ delay = parsed_time - Time.now
162
+ delay.positive? ? delay : nil
163
+ rescue ArgumentError
164
+ nil
165
+ end
166
+
146
167
  # Sends a request for the batch, returns [status_code, body]
147
168
  def send_request(api_key, batch)
169
+ @last_retry_after = nil
148
170
  payload = JSON.generate(api_key: api_key, batch: batch)
149
171
 
150
172
  request_path, request_headers, request_payload = build_request(@path, @headers, payload)
@@ -159,6 +181,7 @@ module PostHog
159
181
  @http_mutex.synchronize do
160
182
  @http.start unless @http.started? # Maintain a persistent connection
161
183
  response = @http.request(request, request_payload)
184
+ @last_retry_after = response['Retry-After']
162
185
  [response.code.to_i, response.body]
163
186
  end
164
187
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PostHog
4
- VERSION = '3.15.0'
4
+ VERSION = '3.15.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posthog-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.15.0
4
+ version: 3.15.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''