Skip to content

Commit c5e7bc4

Browse files
committed
fix(clients): reduce chances of Push rate limiting
1 parent 9a6dd5f commit c5e7bc4

File tree

3 files changed

+52
-55
lines changed

3 files changed

+52
-55
lines changed

clients/algoliasearch-client-php/lib/FormDataProcessor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
*/
1313

1414
/**
15-
* Search API.
15+
* Ingestion API.
1616
*
17-
* The Algolia Search API lets you search, configure, and manage your indices and records. ## Client libraries Use Algolia's API clients and libraries to reliably integrate Algolia's APIs with your apps. The official API clients are covered by Algolia's [Service Level Agreement](https://www.algolia.com/policies/sla/). See: [Algolia's ecosystem](https://www.algolia.com/doc/guides/getting-started/how-algolia-works/in-depth/ecosystem/) ## Base URLs The base URLs for requests to the Search API are: - `https://{APPLICATION_ID}.algolia.net` - `https://{APPLICATION_ID}-dsn.algolia.net`. If your subscription includes a [Distributed Search Network](https://dashboard.algolia.com/infra), this ensures that requests are sent to servers closest to users. Both URLs provide high availability by distributing requests with load balancing. **All requests must use HTTPS.** ## Retry strategy To guarantee high availability, implement a retry strategy for all API requests using the URLs of your servers as fallbacks: - `https://{APPLICATION_ID}-1.algolianet.com` - `https://{APPLICATION_ID}-2.algolianet.com` - `https://{APPLICATION_ID}-3.algolianet.com` These URLs use a different DNS provider than the primary URLs. You should randomize this list to ensure an even load across the three servers. All Algolia API clients implement this retry strategy. ## Authentication To authenticate your API requests, add these headers: - `x-algolia-application-id`. Your Algolia application ID. - `x-algolia-api-key`. An API key with the necessary permissions to make the request. The required access control list (ACL) to make a request is listed in each endpoint's reference. You can find your application ID and API key in the [Algolia dashboard](https://dashboard.algolia.com/account). ## Request format Depending on the endpoint, request bodies are either JSON objects or arrays of JSON objects, ## Parameters Parameters are passed as query parameters for GET and DELETE requests, and in the request body for POST and PUT requests. Query parameters must be [URL-encoded](https://developer.mozilla.org/en-US/docs/Glossary/Percent-encoding). Non-ASCII characters must be UTF-8 encoded. Plus characters (`+`) are interpreted as spaces. Arrays as query parameters must be one of: - A comma-separated string: `attributesToRetrieve=title,description` - A URL-encoded JSON array: `attributesToRetrieve=%5B%22title%22,%22description%22%D` ## Response status and errors The Search API returns JSON responses. Since JSON doesn't guarantee any specific ordering, don't rely on the order of attributes in the API response. Successful responses return a `2xx` status. Client errors return a `4xx` status. Server errors are indicated by a `5xx` status. Error responses have a `message` property with more information. ## Version The current version of the Search API is version 1, as indicated by the `/1/` in each endpoint's URL.
17+
* The Ingestion API lets you connect third-party services and platforms with Algolia and schedule tasks to ingest your data. The Ingestion API powers the no-code [data connectors](https://dashboard.algolia.com/connectors). ## Base URLs The base URLs for requests to the Ingestion API are: - `https://data.us.algolia.com` - `https://data.eu.algolia.com` Use the URL that matches your [analytics region](https://dashboard.algolia.com/account/infrastructure/analytics). **All requests must use HTTPS.** ## Authentication To authenticate your API requests, add these headers: - `x-algolia-application-id`. Your Algolia application ID. - `x-algolia-api-key`. An API key with the necessary permissions to make the request. The required access control list (ACL) to make a request is listed in each endpoint's reference. You can find your application ID and API key in the [Algolia dashboard](https://dashboard.algolia.com/account). ## Request format Request bodies must be JSON objects. ## Response status and errors Response bodies are JSON objects. Successful responses return a `2xx` status. Client errors return a `4xx` status. Server errors are indicated by a `5xx` status. Error responses have a `message` property with more information. ## Version The current version of the Ingestion API is version 1, as indicated by the `/1/` in each endpoint's URL.
1818
*
1919
* The version of the OpenAPI document: 1.0.0
2020
* Generated by: https://openapi-generator.tech
@@ -29,7 +29,7 @@
2929

3030
namespace Algolia\AlgoliaSearch;
3131

32-
use Algolia\AlgoliaSearch\Model\Search\ModelInterface;
32+
use Algolia\AlgoliaSearch\Model\Ingestion\ModelInterface;
3333
use DateTime;
3434
use GuzzleHttp\Psr7\Utils;
3535
use Psr\Http\Message\StreamInterface;

templates/javascript/clients/client/api/ingestionHelpers.mustache

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,41 @@ async chunkedPush(
3030
);
3131
records = [];
3232
}
33-
}
34-
35-
let retryCount = 0;
3633

37-
if (waitForTasks) {
38-
for (const resp of responses) {
39-
if (!resp.eventID) {
40-
throw new Error('received unexpected response from the push endpoint, eventID must not be undefined');
41-
}
34+
if (waitForTasks && (responses.length % 50 === 0 || i === objects.length - 1)) {
35+
for (const resp of responses.slice(-50)) {
36+
if (!resp.eventID) {
37+
throw new Error('received unexpected response from the push endpoint, eventID must not be undefined');
38+
}
4239

43-
await createIterablePromise({
44-
func: async () => {
45-
if (resp.eventID === undefined || !resp.eventID) {
46-
throw new Error('received unexpected response from the push endpoint, eventID must not be undefined');
47-
}
40+
let retryCount = 0;
4841

49-
return this.getEvent({ runID: resp.runID, eventID: resp.eventID }).catch((error: ApiError) => {
50-
if (error.status === 404) {
51-
return undefined;
42+
await createIterablePromise({
43+
func: async () => {
44+
if (resp.eventID === undefined || !resp.eventID) {
45+
throw new Error('received unexpected response from the push endpoint, eventID must not be undefined');
5246
}
5347

54-
throw error;
55-
})
56-
},
57-
validate: (response) => response !== undefined,
58-
aggregator: () => (retryCount += 1),
59-
error: {
60-
validate: () => retryCount >= 50,
61-
message: () => `The maximum number of retries exceeded. (${retryCount}/${50})`,
62-
},
63-
timeout: (): number => Math.min(retryCount * 500, 5000),
64-
});
48+
return this.getEvent({ runID: resp.runID, eventID: resp.eventID }).catch((error: ApiError) => {
49+
if (error.status === 404) {
50+
return undefined;
51+
}
52+
53+
throw error;
54+
})
55+
},
56+
validate: (response) => response !== undefined,
57+
aggregator: () => (retryCount += 1),
58+
error: {
59+
validate: () => retryCount >= 50,
60+
message: () => `The maximum number of retries exceeded. (${retryCount}/${50})`,
61+
},
62+
timeout: (): number => Math.min(retryCount * 500, 5000),
63+
});
64+
}
6565
}
6666
}
6767

68+
6869
return responses;
69-
},
70+
},

templates/php/api.mustache

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -348,37 +348,33 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
348348
}
349349

350350
++$count;
351-
}
352351

353-
if (!empty($records)) {
354-
$responses[] = $this->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions);
355-
}
352+
if ($waitForTasks && (count($responses) % 50 === 0 || $count === sizeof($objects) - 1)) {
353+
$timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout';
356354
357-
if ($waitForTasks && !empty($responses)) {
358-
$timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout';
355+
foreach ($responses as $response) {
356+
$retry = 0;
359357
360-
foreach ($responses as $response) {
361-
$retry = 0;
358+
while ($retry < 50) {
359+
try {
360+
$this->getEvent($response['runID'], $response['eventID']);
362361
363-
while ($retry < 50) {
364-
try {
365-
$this->getEvent($response['runID'], $response['eventID']);
362+
$ok = true;
366363
367-
$ok = true;
364+
break;
365+
} catch (NotFoundException $e) {
366+
// just retry
367+
}
368368

369-
break;
370-
} catch (NotFoundException $e) {
371-
// just retry
369+
++$retry;
370+
usleep(
371+
call_user_func_array($timeoutCalculation, [$this->config->getWaitTaskTimeBeforeRetry(), $retry])
372+
);
372373
}
373374

374-
++$retry;
375-
usleep(
376-
call_user_func_array($timeoutCalculation, [$this->config->getWaitTaskTimeBeforeRetry(), $retry])
377-
);
378-
}
379-
380-
if (false === $ok) {
381-
throw new ExceededRetriesException('Maximum number of retries (50) exceeded.');
375+
if (false === $ok) {
376+
throw new ExceededRetriesException('Maximum number of retries (50) exceeded.');
377+
}
382378
}
383379
}
384380
}
@@ -892,4 +888,4 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
892888
);
893889
}
894890
}
895-
{{/operations}}
891+
{{/operations}}

0 commit comments

Comments
 (0)