Skip to content

Commit

Permalink
fix: The server no longer crashes when community.plex.tv rate limits …
Browse files Browse the repository at this point in the history
…have been hit. Also improved logging and increased API paging chunks to minimize the occurrence of this error. (#1253)
  • Loading branch information
jorenn92 authored Sep 6, 2024
1 parent bf8c2d3 commit 8227f8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions server/src/modules/api/external-api/external-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from '@nestjs/common';
import axios, { AxiosInstance, RawAxiosRequestConfig } from 'axios';
import axios, { AxiosError, AxiosInstance, RawAxiosRequestConfig } from 'axios';
import NodeCache from 'node-cache';

// 15 minute default TTL (in seconds)
Expand Down Expand Up @@ -177,15 +177,27 @@ export class ExternalApiService {
return cachedItem;
}

const response = await this.axios.post<T>(endpoint, data, config);
const response = await this.axios
.post<T>(endpoint, data, config)
.catch((err: AxiosError) => {
throw err;
});

if (this.cache) {
this.cache.set(cacheKey, response.data, ttl ?? DEFAULT_TTL);
}

return response.data;
} catch (err) {
Logger.debug(`POST request failed: ${err}`);
} catch (err: any) {
if (err.response?.status === 429) {
const retryAfter = err.response.headers['retry-after'] || 'unknown';
Logger.warn(
`${endpoint} Rate limit hit. Retry after: ${retryAfter} seconds.`,
);
} else {
Logger.warn(`POST request failed: ${err.message}`);
Logger.debug(err);
}
return undefined;
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/modules/api/plex-api/plex-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ export class PlexApiService {
let result: PlexCommunityWatchList[] = [];
let next = true;
let page = null;
const size = 50;
const size = 100;

while (next) {
const resp = await this.plexCommunityClient.query({
Expand Down

0 comments on commit 8227f8c

Please sign in to comment.