Skip to content

Commit 4552d6b

Browse files
Improve healthcheck performance
1 parent e98d8f7 commit 4552d6b

File tree

2 files changed

+43
-51
lines changed

2 files changed

+43
-51
lines changed

src/controller/app/app.controller.ts

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Controller, Get, Logger } from '@nestjs/common';
1+
import { Controller, Get, Inject, Logger } from '@nestjs/common';
22
import {
33
HealthCheckService,
44
HttpHealthIndicator,
55
HealthCheck,
66
TypeOrmHealthIndicator,
77
HealthIndicator,
88
HealthIndicatorResult,
9+
HealthCheckResult,
910
} from '@nestjs/terminus';
1011
import { ApiExcludeEndpoint } from '@nestjs/swagger';
1112
import { PostgresService } from 'src/db/postgres.service';
@@ -16,15 +17,17 @@ import { ConfigService } from '@nestjs/config';
1617
import { AllocatorReportGeneratorJobService } from 'src/jobs/allocator-report-generator-job/allocator-report-generator-job.service';
1718
import { IpniAdvertisementFetcherJobService } from 'src/jobs/ipni-advertisement-fetcher-job/ipni-advertisement-fetcher-job.service';
1819
import { LocationService } from 'src/service/location/location.service';
19-
import { CacheTTL } from '@nestjs/cache-manager';
20+
import { Cache, CACHE_MANAGER, CacheTTL } from '@nestjs/cache-manager';
2021
import { GitHubTriggersHandlerService } from 'src/service/github-triggers-handler-service/github-triggers-handler.service';
22+
import { Cacheable } from 'src/utils/cacheable';
2123

2224
@Controller()
2325
export class AppController extends HealthIndicator {
2426
private readonly logger = new Logger(AppController.name);
2527
private readonly appStartTime = new Date();
2628

2729
constructor(
30+
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
2831
private readonly healthCheckService: HealthCheckService,
2932
private readonly httpHealthIndicator: HttpHealthIndicator,
3033
private readonly typeOrmHealthIndicator: TypeOrmHealthIndicator,
@@ -50,57 +53,57 @@ export class AppController extends HealthIndicator {
5053
@Get('/debug')
5154
@CacheTTL(1) // disable cache
5255
@ApiExcludeEndpoint()
53-
async getDebug() {
56+
public async getDebug() {
5457
return 'debug';
5558
}
5659

57-
private async _getHealth(): Promise<HealthIndicatorResult> {
60+
@Get('health')
61+
@HealthCheck()
62+
@CacheTTL(1) // disable cache
63+
public async getHealth(): Promise<HealthCheckResult> {
64+
return this._getHealth();
65+
}
66+
67+
private async _getHealthMetadata(): Promise<HealthIndicatorResult> {
5868
return this.getStatus('app', true, {
5969
appStartTime: this.appStartTime,
6070
});
6171
}
6272

63-
@Get('health')
64-
@HealthCheck()
65-
@CacheTTL(1000 * 10) // 10 seconds
66-
public async getHealth() {
67-
this.logger.debug('Running healthcheck');
73+
// cache http ping checks for better performance
74+
@Cacheable({ ttl: 1000 * 60 * 10 }) // 10 minutes
75+
private async _httpPingCheck(
76+
name: string,
77+
url: string,
78+
): Promise<HealthIndicatorResult> {
79+
return this.httpHealthIndicator.pingCheck(name, url);
80+
}
81+
82+
// dedicated cache for ipinfo.io because of token limits
83+
@Cacheable({ ttl: 1000 * 60 * 60 }) // 1 hour
84+
private async _httpPingCheckIpInfo(): Promise<HealthIndicatorResult> {
85+
return await this.httpHealthIndicator.pingCheck(
86+
'ipinfo.io',
87+
`https://ipinfo.io/8.8.8.8?token=${this.configService.get<string>('IP_INFO_TOKEN')}`,
88+
);
89+
}
6890

91+
@Cacheable({ ttl: 1000 * 10 }) // 10 seconds
92+
private async _getHealth(): Promise<HealthCheckResult> {
93+
// prettier-ignore
6994
return this.healthCheckService.check([
70-
() => this._getHealth(),
71-
() => this.locationService.getHealth(),
72-
() =>
73-
this.httpHealthIndicator.pingCheck(
74-
'cid.contact',
75-
'https://cid.contact/health',
76-
),
77-
() =>
78-
this.httpHealthIndicator.pingCheck(
79-
'glif-api',
80-
`${this.configService.get<string>('GLIF_API_BASE_URL')}/v1`,
81-
),
82-
() =>
83-
this.httpHealthIndicator.pingCheck(
84-
'allocator-tech-api',
85-
`${this.configService.get<string>('ALLOCATOR_TECH_BASE_URL')}/health`,
86-
),
87-
() =>
88-
this.httpHealthIndicator.pingCheck(
89-
'api.datacapstats.io',
90-
'https://api.datacapstats.io/api/health',
91-
),
92-
() =>
93-
this.httpHealthIndicator.pingCheck(
94-
'stats.filspark.com',
95-
'https://stats.filspark.com',
96-
),
97-
() =>
98-
this.typeOrmHealthIndicator.pingCheck('database', {
95+
() => this._getHealthMetadata(),
96+
() => this._httpPingCheckIpInfo(),
97+
() => this._httpPingCheck('cid.contact', 'https://cid.contact/health'),
98+
() => this._httpPingCheck('glif-api', `${this.configService.get<string>('GLIF_API_BASE_URL')}/v1`),
99+
() => this._httpPingCheck('allocator-tech-api', `${this.configService.get<string>('ALLOCATOR_TECH_BASE_URL')}/health`),
100+
() => this._httpPingCheck('api.datacapstats.io', 'https://api.datacapstats.io/api/health'),
101+
() => this._httpPingCheck('stats.filspark.com', 'https://stats.filspark.com'),
102+
() => this.typeOrmHealthIndicator.pingCheck('database', {
99103
connection: this.postgresService.pool,
100104
timeout: 2000,
101105
}),
102-
() =>
103-
this.typeOrmHealthIndicator.pingCheck('database-dmob', {
106+
() => this.typeOrmHealthIndicator.pingCheck('database-dmob', {
104107
connection: this.postgresDmobService.pool,
105108
timeout: 2000,
106109
}),

src/service/location/location.service.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { HttpService } from '@nestjs/axios';
66
import { firstValueFrom } from 'rxjs';
77
import { Address, IPResponse } from './types.location';
88
import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager';
9-
import { HealthIndicatorResult, HttpHealthIndicator } from '@nestjs/terminus';
109
import { Cacheable } from 'src/utils/cacheable';
1110

1211
@Injectable()
@@ -16,19 +15,9 @@ export class LocationService {
1615
constructor(
1716
private configService: ConfigService,
1817
private readonly httpService: HttpService,
19-
private httpHealthIndicator: HttpHealthIndicator,
2018
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
2119
) {}
2220

23-
// dedicated, heavily cached healthcheck needed because of ipinfo.io token limits
24-
@Cacheable({ ttl: 1000 * 60 * 60 }) // 1 hour
25-
public async getHealth(): Promise<HealthIndicatorResult> {
26-
return await this.httpHealthIndicator.pingCheck(
27-
'ipinfo.io',
28-
`https://ipinfo.io/8.8.8.8?token=${this.configService.get<string>('IP_INFO_TOKEN')}`,
29-
);
30-
}
31-
3221
public async getLocation(multiAddrs?: string[]): Promise<IPResponse | null> {
3322
if (!multiAddrs) return null;
3423

0 commit comments

Comments
 (0)