-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathloadbalance.checker.ts
39 lines (35 loc) · 1.21 KB
/
loadbalance.checker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Inject, Injectable } from '@nestjs/common';
import { ServiceOptions } from './interfaces/service-options.interface';
import { PASSING } from './server-state';
import { get } from 'lodash';
import { Loadbalancer } from './loadbalancer';
import { AxiosInstance } from 'axios';
import { AXIOS_INSTANCE_PROVIDER } from './loadbalance.constants';
const defaultServiceOptions = {
name: '',
rule: '',
check: {
protocol: 'http',
url: '/health',
},
};
@Injectable()
export class LoadbalanceChecker {
constructor(
@Inject(AXIOS_INSTANCE_PROVIDER) private readonly axios: AxiosInstance,
) {
}
public pingServer(loadbalancer: Loadbalancer, options: ServiceOptions = defaultServiceOptions) {
loadbalancer.servers.filter(server => server.state.status !== PASSING).map(async server => {
const protocol = get(options, 'check.protocol', 'http');
const url = get(options, 'check.url', '/health');
try {
await this.axios.get(
`${protocol}://${server.address}:${server.port}${url}`,
);
server.state.status = PASSING;
} catch (e) {
}
});
}
}