-
Notifications
You must be signed in to change notification settings - Fork 17
fix: Improve retry mechanisms for split pdf logic #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import async from "async"; | ||
|
||
import { HTTPClient } from "../../lib/http.js"; | ||
import { | ||
AfterErrorContext, | ||
AfterErrorHook, | ||
|
@@ -25,10 +24,12 @@ import { | |
stringToBoolean, | ||
} from "./utils/index.js"; | ||
import { | ||
HTTPClientExtension, | ||
MIN_PAGES_PER_THREAD, | ||
PARTITION_FORM_FILES_KEY, | ||
PARTITION_FORM_SPLIT_PDF_PAGE_KEY, | ||
} from "./common.js"; | ||
import {retry, RetryConfig} from "../../lib/retries"; | ||
|
||
/** | ||
* Represents a hook for splitting and sending PDF files as per page requests. | ||
|
@@ -39,8 +40,7 @@ export class SplitPdfHook | |
/** | ||
* The HTTP client used for making requests. | ||
*/ | ||
client: HTTPClient | undefined; | ||
|
||
client: HTTPClientExtension | undefined; | ||
|
||
/** | ||
* Keeps the strict-mode setting for splitPdfPage feature. | ||
|
@@ -68,9 +68,16 @@ export class SplitPdfHook | |
* @returns The initialized SDK options. | ||
*/ | ||
sdkInit(opts: SDKInitOptions): SDKInitOptions { | ||
const { baseURL, client } = opts; | ||
this.client = client; | ||
return { baseURL: baseURL, client: client }; | ||
const { baseURL } = opts; | ||
this.client = new HTTPClientExtension(); | ||
|
||
this.client.addHook("response", (res) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we could move this into the HTTPClientExtension class? not necessary though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense to me! |
||
if (res.status != 200) { | ||
console.error("Request failed with status code", `${res.status}`); | ||
} | ||
}); | ||
|
||
return { baseURL: baseURL, client: this.client }; | ||
} | ||
|
||
/** | ||
|
@@ -178,23 +185,48 @@ export class SplitPdfHook | |
file.name, | ||
firstPageNumber | ||
); | ||
const timeoutInMs = 60 * 10 * 1000; | ||
const req = new Request(requestClone, { | ||
headers, | ||
body, | ||
signal: AbortSignal.timeout(timeoutInMs) | ||
}); | ||
requests.push(req); | ||
setIndex+=1; | ||
} | ||
|
||
this.partitionSuccessfulResponses[operationID] = new Array(requests.length); | ||
this.partitionFailedResponses[operationID] = new Array(requests.length); | ||
|
||
const allowFailed = this.allowFailed; | ||
|
||
// These are the retry values from our api spec | ||
// We need to hardcode them here until we're able to reuse the SDK | ||
// from within this hook | ||
const oneSecond = 1000; | ||
const oneMinute = 1000 * 60; | ||
const retryConfig = { | ||
strategy: "backoff", | ||
backoff: { | ||
initialInterval: oneSecond * 3, | ||
maxInterval: oneMinute * 12, | ||
exponent: 1.88, | ||
maxElapsedTime: oneMinute * 30, | ||
}, | ||
} as RetryConfig; | ||
|
||
const retryCodes = ["502", "503", "504"]; | ||
|
||
this.partitionRequests[operationID] = async.parallelLimit( | ||
requests.slice(0, -1).map((req, pageIndex) => async () => { | ||
requests.map((req, pageIndex) => async () => { | ||
const pageNumber = pageIndex + startingPageNumber; | ||
try { | ||
const response = await this.client!.request(req); | ||
const response = await retry( | ||
async () => { | ||
return await this.client!.request(req.clone()); | ||
}, | ||
{ config: retryConfig, statusCodes: retryCodes } | ||
); | ||
if (response.status === 200) { | ||
(this.partitionSuccessfulResponses[operationID] as Response[])[pageIndex] = | ||
response.clone(); | ||
|
@@ -206,7 +238,7 @@ export class SplitPdfHook | |
} | ||
} | ||
} catch (e) { | ||
console.error(`Failed to send request for page ${pageNumber}.`); | ||
console.error(`Failed to send request for page ${pageNumber}.`, e); | ||
if (!allowFailed) { | ||
throw e; | ||
} | ||
|
@@ -215,7 +247,8 @@ export class SplitPdfHook | |
concurrencyLevel | ||
); | ||
|
||
return requests.at(-1) as Request; | ||
const dummyRequest = new Request("https://no-op/"); | ||
return dummyRequest; | ||
} | ||
|
||
/** | ||
|
@@ -230,28 +263,39 @@ export class SplitPdfHook | |
successfulResponses: Response[], | ||
failedResponses: Response[] | ||
): Promise<Response> { | ||
let realResponse = response.clone(); | ||
const firstSuccessfulResponse = successfulResponses.at(0); | ||
const isFakeResponse = response.headers.has("fake-response"); | ||
if (firstSuccessfulResponse !== undefined && isFakeResponse) { | ||
realResponse = firstSuccessfulResponse.clone(); | ||
} | ||
|
||
let responseBody, responseStatus, responseStatusText; | ||
const numFailedResponses = failedResponses?.length ?? 0; | ||
const headers = prepareResponseHeaders(response); | ||
const headers = prepareResponseHeaders(realResponse); | ||
|
||
if (!this.allowFailed && failedResponses && failedResponses.length > 0) { | ||
const failedResponse = failedResponses[0]?.clone(); | ||
if (failedResponse) { | ||
responseBody = await failedResponse.text(); | ||
responseStatus = failedResponse.status; | ||
responseStatusText = failedResponse.statusText; | ||
} else { | ||
responseBody = JSON.stringify({"details:": "Unknown error"}); | ||
responseStatus = 503 | ||
responseStatusText = "Unknown error" | ||
} | ||
// if the response status is unknown or was 502, 503, 504, set back to 500 to ensure we don't cause more retries | ||
responseStatus = 500; | ||
console.warn( | ||
`${numFailedResponses} requests failed. The partition operation is cancelled.` | ||
); | ||
} else { | ||
responseBody = await prepareResponseBody([...successfulResponses, response]); | ||
responseStatus = response.status | ||
responseStatusText = response.statusText | ||
if (isFakeResponse) { | ||
responseBody = await prepareResponseBody([...successfulResponses]); | ||
} else { | ||
responseBody = await prepareResponseBody([...successfulResponses, response]); | ||
} | ||
responseStatus = realResponse.status | ||
responseStatusText = realResponse.statusText | ||
if (numFailedResponses > 0) { | ||
console.warn( | ||
`${numFailedResponses} requests failed. The results might miss some pages.` | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on line 213 wont this assignment fail:
(this.partitionSuccessfulResponses[operationID] as Response[])[pageIndex][pageIndex] =
response.clone();
because this.partitionSuccessfulResponses[operationID] has not been initialized to an array?
Have a look at line 192, the success responses is initialized, but the fail responses is not:
this.partitionSuccessfulResponses[operationID] = new Array(requests.length);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Thanks