forked from nest-cloud/nestcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadbalance.ts
138 lines (124 loc) · 4.94 KB
/
loadbalance.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
ILoadbalance,
IServiceServer,
IServer,
ILoadbalancer,
IService,
Scanner,
SERVICE,
stringToKeyValue,
} from '@nestcloud/common';
import { ServiceOptions } from './interfaces/service-options.interface';
import { Loadbalancer } from './loadbalancer';
import { Server } from './server';
import { ServerState } from './server-state';
import { Rule } from './interfaces/rule.interface';
import { ServiceNotExistException } from './exceptions/service-not-exist.exception';
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import { LoadbalanceChecker } from './loadbalance.checker';
import { LoadbalanceRuleRegistry } from './loadbalance-rule.registry';
import { LoadbalanceConfig } from './loadbalance.config';
@Injectable()
export class Loadbalance implements ILoadbalance, OnModuleInit {
private readonly loadbalancers = new Map<string, Loadbalancer>();
private timer = null;
constructor(
private readonly config: LoadbalanceConfig,
private readonly scanner: Scanner,
private readonly loadbalanceChecker: LoadbalanceChecker,
private readonly loadbalanceRuleRegistry: LoadbalanceRuleRegistry,
@Inject(SERVICE) private readonly service: IService,
) {
}
async onModuleInit(): Promise<void> {
await this.init();
}
private async init() {
const services: string[] = this.service.getServiceNames();
await this.updateServices(services);
this.service.watchServiceList(async (services: string[]) => {
await this.updateServices(services);
});
if (this.timer) {
clearInterval(this.timer);
}
this.timer = setInterval(() => this.pingServers(), 30000);
}
public chooseLoadbalancer(serviceName: string): ILoadbalancer {
const loadbalancer = this.loadbalancers.get(serviceName);
if (!loadbalancer) {
throw new Error(`The service ${serviceName} is not exist`);
}
return loadbalancer;
}
public choose(serviceName: string): IServer {
const loadbalancer = this.loadbalancers.get(serviceName);
if (!loadbalancer) {
throw new ServiceNotExistException(`The service ${serviceName} is not exist`);
}
return loadbalancer.chooseService();
}
public state(): { [service: string]: IServer[] } {
const state = {};
this.loadbalancers.forEach((loadbalancer, service) => {
state[service] = loadbalancer.servers;
});
return state;
}
private updateServices(services: string[], force?: boolean) {
services.forEach(async service => {
const nodes = this.service.getServiceServers(service);
if (!force) {
if (!service || this.loadbalancers.has(service)) {
return null;
}
}
const ruleName = this.config.getRule(service);
let rule: Rule = this.loadbalanceRuleRegistry.getRule(ruleName);
if (!rule) {
await new Promise((resolve, reject) => {
this.loadbalanceRuleRegistry.watch(() => {
rule = this.loadbalanceRuleRegistry.getRule(ruleName);
if (rule) {
resolve();
}
});
setTimeout(() => reject(new Error(`The rule ${ruleName} is not exist`)), 5000);
});
}
this.createLoadbalancer(service, nodes, rule);
this.createServiceWatcher(service, rule);
});
}
private createServiceWatcher(service: string, rule: Rule) {
this.service.watch(service, (nodes: IServiceServer[]) => this.createLoadbalancer(service, nodes, rule));
}
private createLoadbalancer(serviceName: string, nodes: IServiceServer[], rule: Rule) {
const loadbalancer: Loadbalancer = this.loadbalancers.get(serviceName);
const servers = nodes.map(node => {
const server = new Server(node.address, node.port);
server.name = node.name;
if (loadbalancer && loadbalancer.getServer(server.id)) {
server.state = loadbalancer.getServer(server.id).state;
} else {
server.state = new ServerState();
}
server.state.status = node.status;
server.tags = stringToKeyValue(node.tags);
return server;
});
this.loadbalancers.set(serviceName, new Loadbalancer(
serviceName,
serviceName,
servers,
rule,
));
}
private pingServers() {
this.loadbalancers.forEach((loadbalancer, service) => {
const servicesOptions = this.config.getServiceOptions();
const options: ServiceOptions = servicesOptions.filter(rule => rule.name === service)[0];
this.loadbalanceChecker.pingServer(loadbalancer, options);
});
}
}