Skip to content

Commit 12d2f08

Browse files
MichaelGHSegclaude
andcommitted
Omit X-Retry-Count header on first attempt, send only on retries
First request (retry_count=0) no longer includes the header. Retries with retry_count > 0 continue to send X-Retry-Count: 1, 2, 3, etc. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 184fe1f commit 12d2f08

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

segment/analytics/request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ def post(write_key, host=None, gzip=False, timeout=15, proxies=None, oauth_manag
5454
headers = {
5555
'Content-Type': 'application/json',
5656
'User-Agent': 'analytics-python/' + VERSION,
57-
'X-Retry-Count': str(retry_count)
5857
}
58+
if retry_count > 0:
59+
headers['X-Retry-Count'] = str(retry_count)
5960

6061
# Add Authorization header - prefer OAuth Bearer token, fallback to Basic auth
6162
if auth:

segment/analytics/test/test_request.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ def mock_post_fn(*args, **kwargs):
120120
self.assertEqual(headers['Authorization'], 'Bearer test_token_123')
121121

122122
def test_x_retry_count_header(self):
123-
"""Test that X-Retry-Count header is included"""
123+
"""Test that X-Retry-Count header is omitted on first attempt and included on retries"""
124124
def mock_post_fn(*args, **kwargs):
125125
res = mock.Mock()
126126
res.status_code = 200
127127
return res
128128

129129
with mock.patch('segment.analytics.request._session.post', side_effect=mock_post_fn) as mock_post:
130-
# Test with retry_count=0 (first attempt)
130+
# Test with retry_count=0 (first attempt) — header should be absent
131131
post('testsecret', retry_count=0, batch=[{
132132
'userId': 'userId',
133133
'event': 'python event',
@@ -136,8 +136,7 @@ def mock_post_fn(*args, **kwargs):
136136

137137
args, kwargs = mock_post.call_args
138138
headers = kwargs['headers']
139-
self.assertIn('X-Retry-Count', headers)
140-
self.assertEqual(headers['X-Retry-Count'], '0')
139+
self.assertNotIn('X-Retry-Count', headers)
141140

142141
with mock.patch('segment.analytics.request._session.post', side_effect=mock_post_fn) as mock_post:
143142
# Test with retry_count=5

0 commit comments

Comments
 (0)