Created
January 14, 2023 00:56
-
-
Save VinSpee/fff0677bc5ff0445b61f6cf265257cd0 to your computer and use it in GitHub Desktop.
I was able to add a little strategy around the retries, using [`got`](https://github.com/sindresorhus/got/blob/main/documentation/9-hooks.md#afterresponse) which implements an "exponential back off" (first retry happens in 1 second, then 2, then 4, then 8, then 16, etc etc, with a little variance to not make it look like a bot. Here's the code:
This file contains 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
import got, { type RequestError } from "got"; | |
import { env } from "~/env"; | |
import { StatusCodes } from "http-status-codes"; | |
const generateAction = async (request, response) => { | |
const result = await got | |
.post(env.HUGGINGFACE_MODEL_URL, { | |
hooks: { | |
afterResponse: [ | |
(result, retryWithMergedOptions) => { | |
if (result.statusCode === StatusCodes.SERVICE_UNAVAILABLE) { | |
return retryWithMergedOptions({}); | |
} | |
return result; | |
}, | |
], | |
beforeRetry: [ | |
(error, retryCount) => { | |
console.log( | |
`Retrying [${retryCount}]: ${error.code}, (${error.response?.statusCode}, ${error.response?.statusMessage})` | |
); | |
}, | |
], | |
}, | |
encoding: "base64", | |
headers: { | |
Authorization: `Bearer ${env.HUGGINGFACE_READ_TOKEN}`, | |
}, | |
json: { | |
inputs: data.prompt, | |
}, | |
retry: { | |
statusCodes: [StatusCodes.SERVICE_UNAVAILABLE], | |
methods: ["POST"], | |
limit: 10, | |
}, | |
}) | |
.catch((error: RequestError) => { | |
console.error("❌ error hitting `huggingface`", { | |
statusCode: error.response?.statusCode, | |
code: error.code, | |
message: error.message, | |
name: error.name, | |
}); | |
}); | |
if (result?.ok) { | |
const imageData = await result.body; | |
response.status = StatusCodes.OK; | |
return { prompt: data.prompt, image: imageData }; | |
} | |
response.status === StatusCodes.INTERNAL_SERVER_ERROR; | |
throw new Error("Image could not be generated"); | |
}; | |
export default generateAction; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment