Skip to content

Commit

Permalink
fix: set bing retry count to 3, avoid too much retry request
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Jan 12, 2025
1 parent d95a774 commit 327548a
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/translation/microsoft/bing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const defaultBingHost = "www.bing.com";
// * bing host depends ip, if ip is in china, `must` use cn.bing.com, otherwise use www.bing.com. And vice versa.
let bingHost = myPreferences.bingHost || defaultBingHost;

let retryCount = 0;

/**
* Request Microsoft Bing Web Translator.
*/
Expand Down Expand Up @@ -104,24 +106,39 @@ export async function requestWebBingTranslate(queryWordInfo: QueryWordInfo): Pro
const finalUrl = response.request.res.responseUrl;
console.log(`bing finalUrl: ${finalUrl}`);

// get host
bingHost = new URL(finalUrl).host;
// Get new host
const newBingHost = new URL(finalUrl).host;
const responseData = response.data;
console.warn(`bing translate cost time: ${response.headers[requestCostTime]}`);

// If bing translate response is empty, may be ip has been changed, bing tld is not correct, so check ip again, then request again.
if (!responseData) {
console.warn(`bing translate response is empty, change to use new host: ${bingHost}, then request again`);
requestBingConfig().then((bingConfig) => {
if (bingConfig) {
requestWebBingTranslate(queryWordInfo)
.then((result) => resolve(result))
.catch((error) => reject(error));
} else {
reject(undefined);
}
});
if (bingHost !== newBingHost && retryCount < 3) {
console.warn(
`bing translate response is empty, change to use new host: ${bingHost}, then request again, retryCount: ${retryCount}`
);
retryCount++;
requestBingConfig().then((bingConfig) => {
if (bingConfig) {
requestWebBingTranslate(queryWordInfo)
.then((result) => resolve(result))
.catch((error) => reject(error));
} else {
return reject({
type: TranslationType.Bing,
message: "Bing translate response is empty, get bing config failed",
} as RequestErrorInfo);
}
});
} else {
return reject({
type: TranslationType.Bing,
message: "Bing translate response is empty",
} as RequestErrorInfo);
}
} else {
retryCount = 0;

console.log(`bing response: ${JSON.stringify(responseData, null, 4)}`);
const bingTranslateResult = responseData[0] as BingTranslateResult;
const translations = bingTranslateResult.translations[0].text.split("\n");
Expand Down

0 comments on commit 327548a

Please sign in to comment.