-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathLibCurl.php
More file actions
140 lines (110 loc) · 4.62 KB
/
LibCurl.php
File metadata and controls
140 lines (110 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
namespace Segment\Consumer;
class LibCurl extends QueueConsumer
{
protected string $type = 'LibCurl';
/**
* Send a batch of messages to the API with spec-compliant retry logic:
* - 2xx/3xx: success
* - 429 + Retry-After: sleep without consuming retry budget
* - 429 without Retry-After / other retryable (5xx except 501/505/511,
* 408/410/460): exponential backoff, counts against retry budget
* - Non-retryable 4xx / 501/505/511: drop immediately
*
* @param array $messages array of all the messages to send
* @return bool whether the request succeeded
*/
public function flushBatch(array $messages): bool
{
$body = $this->payload($messages);
$payload = json_encode($body);
$secret = $this->secret;
if ($this->compress_request) {
$payload = gzencode($payload);
}
$host = $this->host ?: 'api.segment.io';
$url = $this->protocol . $host . '/v1/batch';
$library = $messages[0]['context']['library'];
$userAgent = $library['name'] . '/' . $library['version'];
$backoffMs = 500; // base 500ms per e2e spec
$backoffCapMs = 60000; // cap 60s
$retriesRemaining = $this->retry_count;
$attempt = 0;
$backoffStartTime = null;
$rateLimitStartTime = null;
while (true) {
$attempt++;
$responseHeaders = [];
$ch = curl_init();
$headers = [
'Content-Type: application/json',
'User-Agent: ' . $userAgent,
];
if ($this->compress_request) {
$headers[] = 'Content-Encoding: gzip';
}
if ($attempt > 1) {
$headers[] = 'X-Retry-Count: ' . ($attempt - 1);
}
curl_setopt($ch, CURLOPT_USERPWD, $secret . ':');
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->curl_connecttimeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) {
$parts = explode(':', $header, 2);
if (count($parts) === 2) {
$responseHeaders[strtolower(trim($parts[0]))] = trim($parts[1]);
}
return strlen($header);
});
$responseContent = curl_exec($ch);
$err = curl_error($ch);
$responseCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($err) {
$this->handleError(0, $err);
return false;
}
// 2xx and 3xx are success
if ($responseCode >= 200 && $responseCode < 400) {
return true;
}
$this->handleError($responseCode, $responseContent);
// 429: check for Retry-After header first
if ($responseCode === 429) {
$retryAfterS = $this->parseRetryAfter($responseHeaders['retry-after'] ?? null);
if ($retryAfterS !== null) {
if ($rateLimitStartTime === null) {
$rateLimitStartTime = microtime(true);
}
if ((microtime(true) - $rateLimitStartTime) * 1000 >= $this->max_rate_limit_duration_ms) {
return false;
}
$sleepMs = min($retryAfterS * 1000, $this->rate_limit_retry_after_cap_s * 1000);
usleep($sleepMs * 1000);
continue; // Do NOT decrement retriesRemaining
}
// No Retry-After: fall through to counted backoff
}
if (!$this->isRetryable($responseCode)) {
return false;
}
$retriesRemaining--;
if ($retriesRemaining <= 0) {
return false;
}
if ($backoffStartTime === null) {
$backoffStartTime = microtime(true);
}
if ((microtime(true) - $backoffStartTime) * 1000 >= $this->max_total_backoff_duration_ms) {
return false;
}
usleep($backoffMs * 1000);
$backoffMs = min($backoffMs * 2, $backoffCapMs);
}
}
}