Skip to content

Commit

Permalink
fix(lastfm): Patch lfm library to actually implement a request timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Jan 31, 2024
1 parent d88ac1b commit ccb50c6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
38 changes: 38 additions & 0 deletions patches/lastfm-node-client+2.2.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
diff --git a/node_modules/lastfm-node-client/lib/ApiRequest.js b/node_modules/lastfm-node-client/lib/ApiRequest.js
index ecd39d0..c8e1cc5 100644
--- a/node_modules/lastfm-node-client/lib/ApiRequest.js
+++ b/node_modules/lastfm-node-client/lib/ApiRequest.js
@@ -91,7 +91,7 @@ class ApiRequest {
const paramsStr = querystring.stringify(paramsObj);
const options = {
hostname: "ws.audioscrobbler.com",
- path: "/2.0"
+ path: "/2.0",
};

if (method === "POST") {
@@ -113,7 +113,11 @@ class ApiRequest {
httpResponse.on("data", chunk => data += chunk);
httpResponse.on("end", () => resolve(data));
httpResponse.on("error", err => reject(err));
- });
+ })
+ // stop waiting for request if it takes longer than 3 seconds
+ .setTimeout(3000, () => {
+ httpRequest.destroy(new Error('ETIMEDOUT - socket hang up'));
+ });

httpRequest.on("error", err => reject(err));

diff --git a/node_modules/lastfm-node-client/lib/LastFm.js b/node_modules/lastfm-node-client/lib/LastFm.js
index 2393a4d..75bdf37 100644
--- a/node_modules/lastfm-node-client/lib/LastFm.js
+++ b/node_modules/lastfm-node-client/lib/LastFm.js
@@ -1045,7 +1045,6 @@ class LastFm {
* @param {callback} [callback]
* @returns {(Promise|LastFm)}
*/
-
trackUpdateNowPlaying(params, callback) {
const apiRequest = new ApiRequest()
.set(params)
12 changes: 8 additions & 4 deletions src/backend/common/vendor/LastfmApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,19 @@ export default class LastfmApiClient extends AbstractApiClient {
} = e;
// for now check for exceptional errors by matching error code text
const retryError = retryErrors.find(x => message.toLocaleLowerCase().includes(x));
if (undefined !== retryError) {
const timeout = retryError === undefined && message.includes('ETIMEDOUT');
if (undefined !== retryError || timeout) {
if (retries < maxRequestRetries) {
const delay = (retries + 1) * retryMultiplier;
this.logger.warn(`API call was not good but recoverable (${retryError}), retrying in ${delay} seconds...`);
if(timeout) {
this.logger.warn(`API call timed out after 3 seconds, retrying in ${delay} seconds...`);
} else {
this.logger.warn(`API call was not good but recoverable (${retryError}), retrying in ${delay} seconds...`);
}
await sleep(delay * 1000);
return this.callApi(func, retries + 1);
} else {
this.logger.warn('Could not recover!');
throw e;
throw new UpstreamError(`API call failed due -> ${retryError ?? 'API call timed out'} <- after max retries hit ${maxRequestRetries}`, {cause: e})
}
}

Expand Down

0 comments on commit ccb50c6

Please sign in to comment.