Skip to content

Commit

Permalink
Add timeout option to API requests and update monitor configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
simlarsen committed Sep 18, 2024
1 parent e2ef8ae commit cc8c3f7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
13 changes: 10 additions & 3 deletions Common/Utils/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import URL from "../Types/API/URL";
import Dictionary from "../Types/Dictionary";
import APIException from "../Types/Exception/ApiException";
import { JSONArray, JSONObject } from "../Types/JSON";
import axios, { AxiosError, AxiosResponse } from "axios";
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";
import Sleep from "../Types/Sleep";

export interface RequestOptions {
retries?: number | undefined;
exponentialBackoff?: boolean | undefined;
timeout?: number | undefined;
}

export default class API {
Expand Down Expand Up @@ -383,12 +384,18 @@ export default class API {
while (currentRetry <= maxRetries) {
currentRetry++;
try {
result = await axios({
const axiosOptions: AxiosRequestConfig = {
method: method,
url: url.toString(),
headers: finalHeaders,
data: finalBody,
});
};

if (options?.timeout) {
axiosOptions.timeout = options.timeout;
}

result = await axios(axiosOptions);

break;
} catch (e) {
Expand Down
7 changes: 7 additions & 0 deletions Probe/Utils/Monitors/Monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import API from "Common/Utils/API";
import LocalCache from "Common/Server/Infrastructure/LocalCache";
import logger from "Common/Server/Utils/Logger";
import Monitor from "Common/Models/DatabaseModels/Monitor";
import PositiveNumber from "Common/Types/PositiveNumber";

export default class MonitorUtil {
public static async probeMonitor(
Expand Down Expand Up @@ -151,6 +152,7 @@ export default class MonitorUtil {
{
retry: 5,
monitorId: monitor.id!,
timeout: new PositiveNumber(60000), // 60 seconds
},
);

Expand All @@ -167,6 +169,7 @@ export default class MonitorUtil {
{
retry: 5,
monitorId: monitor.id!,
timeout: new PositiveNumber(60000), // 60 seconds
},
);

Expand Down Expand Up @@ -203,6 +206,7 @@ export default class MonitorUtil {
{
retry: 5,
monitorId: monitor.id!,
timeout: new PositiveNumber(60000), // 60 seconds
},
);

Expand Down Expand Up @@ -278,6 +282,7 @@ export default class MonitorUtil {
{
retry: 5,
monitorId: monitor.id!,
timeout: new PositiveNumber(60000), // 60 seconds
},
);

Expand Down Expand Up @@ -305,6 +310,7 @@ export default class MonitorUtil {
isHeadRequest: MonitorUtil.isHeadRequest(monitorStep),
monitorId: monitor.id!,
retry: 5,
timeout: new PositiveNumber(60000), // 60 seconds
},
);

Expand Down Expand Up @@ -345,6 +351,7 @@ export default class MonitorUtil {
monitorId: monitor.id!,
requestType: monitorStep.data?.requestType || HTTPMethod.GET,
retry: 5,
timeout: new PositiveNumber(60000), // 60 seconds
},
);

Expand Down
9 changes: 9 additions & 0 deletions Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default class ApiMonitor {
currentRetryCount?: number | undefined;
monitorId?: ObjectID | undefined;
isOnlineCheckRequest?: boolean | undefined;
timeout?: PositiveNumber; // timeout in milliseconds
},
): Promise<APIResponse | null> {
if (!options) {
Expand All @@ -62,6 +63,10 @@ export default class ApiMonitor {
url,
options.requestBody || undefined,
options.requestHeaders || undefined,
undefined,
{
timeout: options.timeout?.toNumber() || 5000,
},
);

if (
Expand All @@ -75,6 +80,10 @@ export default class ApiMonitor {
url,
options.requestBody || undefined,
options.requestHeaders || undefined,
undefined,
{
timeout: options.timeout?.toNumber() || 5000,
},
);
}

Expand Down
5 changes: 3 additions & 2 deletions Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default class WebsiteMonitor {
currentRetryCount?: number | undefined;
monitorId?: ObjectID | undefined;
isOnlineCheckRequest?: boolean | undefined;
timeout?: PositiveNumber; // timeout in milliseconds
},
): Promise<ProbeWebsiteResponse | null> {
if (!options) {
Expand All @@ -59,7 +60,7 @@ export default class WebsiteMonitor {
let startTime: [number, number] = process.hrtime();
let result: WebsiteResponse = await WebsiteRequest.fetch(url, {
isHeadRequest: options.isHeadRequest,
timeout: 30000,
timeout: options.timeout?.toNumber() || 5000,
});

if (
Expand All @@ -70,7 +71,7 @@ export default class WebsiteMonitor {
startTime = process.hrtime();
result = await WebsiteRequest.fetch(url, {
isHeadRequest: false,
timeout: 30000,
timeout: options.timeout?.toNumber() || 5000,
});
}

Expand Down

0 comments on commit cc8c3f7

Please sign in to comment.