Skip to content

Commit 329198c

Browse files
added connection timeout
1 parent d50dbde commit 329198c

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/apis/apis.service.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import { CACHE_MANAGER } from '@nestjs/cache-manager';
22
import { Inject, Injectable } from '@nestjs/common';
33
import { Cache } from 'cache-manager';
44
import axios from 'axios';
5+
import http from 'http';
6+
import https from 'https';
7+
8+
const agentOptions = {
9+
keepAlive: true,
10+
timeout: 5000
11+
};
12+
13+
const instance = axios.create({
14+
timeout: 5000,
15+
httpAgent: new http.Agent(agentOptions),
16+
httpsAgent: new https.Agent(agentOptions)
17+
});
518

619
@Injectable()
720
export class APIService {
@@ -15,7 +28,7 @@ export class APIService {
1528

1629
if (cache) return JSON.parse(cache);
1730

18-
const response = await axios.post(
31+
const response = await instance.post(
1932
'https://api.github.com/graphql',
2033
{
2134
query:
@@ -74,7 +87,7 @@ export class APIService {
7487
from.setFullYear(from.getFullYear() - 1);
7588
const fromISO = from.toISOString();
7689

77-
const response = await axios.post(
90+
const response = await instance.post(
7891
'https://api.github.com/graphql',
7992
{
8093
query:
@@ -159,7 +172,7 @@ export class APIService {
159172
return JSON.parse(cache);
160173
}
161174

162-
const response = await axios.get(
175+
const response = await instance.get(
163176
`https://wakatime.com/share${path}`,
164177
{ validateStatus: () => true }
165178
);
@@ -192,7 +205,7 @@ export class APIService {
192205
return JSON.parse(cache);
193206
}
194207

195-
const response = await axios.get(
208+
const response = await instance.get(
196209
`https://wakatime.com/share${path}`,
197210
{ validateStatus: () => true }
198211
);
@@ -226,7 +239,7 @@ export class APIService {
226239
return JSON.parse(cache);
227240
}
228241

229-
const response = await axios.get(`${api}${query}`, {
242+
const response = await instance.get(`${api}${query}`, {
230243
validateStatus: () => true
231244
});
232245

@@ -257,7 +270,7 @@ export class APIService {
257270
return JSON.parse(cache);
258271
}
259272

260-
const response = await axios.get(`${api}${id}`, {
273+
const response = await instance.get(`${api}${id}`, {
261274
validateStatus: () => true
262275
});
263276

0 commit comments

Comments
 (0)