forked from dehypnosis/moleculer-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
broker.ts
364 lines (309 loc) · 14 KB
/
broker.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import * as kleur from "kleur";
import * as _ from "lodash";
import { RecursivePartial } from "../interface";
import { Logger } from "../logger";
import { APIRequestContext } from "../server";
import { ServiceRegistry, ServiceRegistryOptions, Service, ServiceAction, ServiceNode } from "./registry";
import { Reporter, ReporterOptions } from "./reporter";
import { EventPubSub, EventPacket, DiscoveryPubSub, EventListener } from "./pubsub";
import { ParamsMapper, ParamsMapperProps } from "./params";
import { BatchingPool, BatchingPoolOptions } from "./batching";
import { createInlineFunction, InlineFunctionOptions, InlineFunctionProps } from "./function";
import { ServiceBrokerDelegatorConstructors, ServiceBrokerDelegatorConstructorOptions, ServiceBrokerDelegator } from "./delegator";
export type ServiceBrokerOptions = {
registry: ServiceRegistryOptions;
batching: BatchingPoolOptions;
function: InlineFunctionOptions;
reporter: ReporterOptions;
log: {
event: boolean;
call: boolean;
},
} & ServiceBrokerDelegatorConstructorOptions;
export type ServiceBrokerProps = {
id: string;
logger: Logger;
};
export type ServiceBrokerListeners = {
connected: (service: Readonly<Service>) => void;
disconnected: (service: Readonly<Service>) => void;
nodePoolUpdated: (service: Readonly<Service>) => void;
};
export type CallArgs = { action: Readonly<ServiceAction>, params?: any, batchingParams?: any, disableCache: boolean};
export type EventPublishArgs = Omit<EventPacket, "from">;
export type DelegatedCallArgs = Omit<CallArgs, "batchingParams"> & { node: Readonly<ServiceNode> };
export type DelegatedEventPublishArgs = EventPublishArgs;
export class ServiceBroker<DelegatorContext = any> {
private readonly registry: ServiceRegistry;
private readonly delegator: ServiceBrokerDelegator<DelegatorContext>;
private readonly delegatorSymbol: symbol;
private readonly discoveryPubSub: DiscoveryPubSub;
private readonly eventPubSub: EventPubSub;
private readonly opts: RecursivePartial<ServiceBrokerOptions>;
constructor(protected readonly props: ServiceBrokerProps, opts?: RecursivePartial<ServiceBrokerOptions>) {
// save options
this.opts = _.defaultsDeep(opts || {}, {
log: {
event: true,
call: true,
},
});
// create delegator from options
const delegatorKeys = Object.keys(ServiceBrokerDelegatorConstructors);
let delegatorKey = delegatorKeys.find(type => !!this.opts[type as keyof ServiceBrokerDelegatorConstructorOptions]);
if (!delegatorKey) {
delegatorKey = delegatorKeys[0];
this.props.logger.info(`available delegator options are not specified: use ${delegatorKey} delegator`);
}
const key = delegatorKey as keyof ServiceBrokerDelegatorConstructorOptions;
this.delegator = new (ServiceBrokerDelegatorConstructors[key])({
logger: this.props.logger.getChild(key),
emitEvent: this.emitEvent.bind(this),
emitServiceConnected: this.emitServiceConnected.bind(this),
emitServiceDisconnected: this.emitServiceDisconnected.bind(this),
}, this.opts[key] || {});
this.delegatorSymbol = Symbol(`BrokerDelegator:${this.props.id}`);
// create event buses
this.eventPubSub = new EventPubSub({
onError: error => this.props.logger.error(error),
eventNamePatternResolver: this.delegator.eventNameResolver,
maxListeners: Infinity,
});
this.discoveryPubSub = new DiscoveryPubSub({
onError: error => this.props.logger.error(error),
});
// create registry
this.registry = new ServiceRegistry({
logger: this.props.logger.getChild("registry"),
}, this.opts.registry);
}
/* lifecycle */
private working = false;
public async start(listeners: ServiceBrokerListeners): Promise<void> {
this.working = true;
await this.discoveryPubSub.subscribeAll(listeners);
await this.registry.start();
await this.delegator.start();
this.props.logger.info(`service broker has been started: ${kleur.yellow(this.delegator.key)}`);
}
public async stop(): Promise<void> {
this.working = false;
await this.registry.stop();
this.discoveryPubSub.unsubscribeAll();
this.eventPubSub.unsubscribeAll();
await this.delegator.stop();
this.props.logger.info(`service broker has been stopped`);
}
/*
protected methods for brokering discovery and events by delegator
*/
protected async emitEvent(packet: EventPacket): Promise<void> {
if (!this.working) {
return;
}
// publish and store
await this.eventPubSub.publish(packet.event, packet);
this.registry.addEventExample(this.resolveEventName(packet.event), packet);
// log
this.props.logger[this.opts.log!.event! ? "info" : "debug"](`received ${kleur.green(packet.event)} ${packet.broadcast ? "broadcast " : ""}event from ${kleur.yellow(packet.from || "unknown")}`);
}
protected async emitServiceConnected(service: Service): Promise<void> {
if (!this.working) {
return;
}
if (!(service instanceof Service) || !service.id) { // unrecognized service
this.props.logger.error(`service broker discovered a unrecognized service: ${service}`);
return;
}
const foundService = this.registry.findServiceByHash(service.hash);
if (foundService) { // node pool updated
for (const node of service.nodeIdMap.values()) {
foundService.addNode(node);
}
await this.discoveryPubSub.publish("nodePoolUpdated", service);
} else { // new service connected
service.setBroker(this);
this.registry.addService(service);
await this.discoveryPubSub.publish("connected", service);
}
}
protected async emitServiceDisconnected(service: Service, nodeId: string): Promise<void> {
if (!this.working) {
return;
}
const foundService = this.registry.findServiceByHash(service.hash);
if (!foundService || !foundService.nodeIdMap.delete(nodeId)) { // unknown service
this.props.logger.error(`service broker has been disconnected from non-registered service node: ${service} (${nodeId})`);
} else if (foundService.nodeIdMap.size === 0) { // service disconnected
await this.discoveryPubSub.publish("disconnected", foundService);
this.registry.removeServiceByHash(foundService.hash);
} else { // node pool updated
await this.discoveryPubSub.publish("nodePoolUpdated", foundService);
}
}
/* context resource management */
private getDelegatorContext(context: APIRequestContext): DelegatorContext {
let delegatorContext: DelegatorContext | undefined = context.get(this.delegatorSymbol);
if (!delegatorContext) {
delegatorContext = this.delegator.createContext(context);
context.set(this.delegatorSymbol, delegatorContext, ctx => this.delegator.clearContext(ctx));
}
return delegatorContext;
}
private static EventSubscriptionSymbol = Symbol("BrokerEventSubscriptions");
private getEventSubscriptions(context: APIRequestContext): Array<number | AsyncIterator<EventPacket>> {
let subscriptions: Array<number | AsyncIterator<EventPacket>> | undefined = context.get(ServiceBroker.EventSubscriptionSymbol);
if (!subscriptions) {
subscriptions = [];
context.set(ServiceBroker.EventSubscriptionSymbol, subscriptions, subs => {
for (const sub of subs) {
this.unsubscribeEvent(sub);
}
});
}
return subscriptions;
}
private static BatchingPoolSymbol = Symbol("BrokerBatchingPool");
private getBatchingPool(context: APIRequestContext): BatchingPool {
let batchingPool: BatchingPool | undefined = context.get(ServiceBroker.BatchingPoolSymbol);
if (!batchingPool) {
batchingPool = new BatchingPool(this.opts.batching);
context.set(ServiceBroker.BatchingPoolSymbol, batchingPool, pool => {
pool.clear();
});
}
return batchingPool;
}
/* action call */
public async call(context: APIRequestContext, args: CallArgs): Promise<any> {
const ctx = this.getDelegatorContext(context);
const {action, params, batchingParams, disableCache} = args;
const node = this.delegator.selectActionTargetNode(ctx, action)!;
console.assert(node && action, "there are no available nodes to call the action");
// do batching
if (batchingParams) {
const batchingPool = this.getBatchingPool(context);
const batchingParamNames = Object.keys(batchingParams);
const batchingKey = batchingPool.getBatchingKey({action: action.id, params, batchingParamNames});
// set batching handler for this call
if (!batchingPool.hasBatchingHandler(batchingKey)) { // or register job
batchingPool.setBatchingHandler(batchingKey, async (batchingParamsList: any[]) => {
// merge common params with batching params
const mergedParams = (params || {}) as any;
for (const batchingParamName of batchingParamNames) {
mergedParams[batchingParamName] = [];
}
for (const bParams of batchingParamsList) {
for (const [k, v] of Object.entries(bParams)) {
mergedParams[k].push(v);
}
}
// log in advance
this.props.logger[this.opts.log!.call! ? "info" : "debug"](`call ${action}${kleur.cyan("@")}${node} ${kleur.cyan(batchingParamsList.length)} times in a batch from ${kleur.yellow((context.id || "unknown") + "@" + (context.ip || "unknown"))}`);
// do batching call
const response = await this.delegator.call(ctx, {action, node, params: mergedParams, disableCache});
this.registry.addActionExample({action, params: mergedParams, response});
return response;
});
}
// add batch entry and wait response
return batchingPool.batch(batchingKey, batchingParams);
} else {
// log in advance
this.props.logger[this.opts.log!.call! ? "info" : "debug"](`call ${action}${kleur.cyan("@")}${node} from ${kleur.yellow((context.id || "unknown") + "@" + (context.ip || "unknown"))}`);
// normal request
const response = await this.delegator.call(ctx, {action, node, params, disableCache});
this.registry.addActionExample({action, params, response});
return response;
}
}
/* action call cache */
public async clearActionCache(action: Readonly<ServiceAction>): Promise<void> {
if (await this.delegator.clearActionCache(action)) {
this.props.logger.info(`${action} action caches have been cleared`);
}
}
public async clearServiceCache(service: Readonly<Service>): Promise<void> {
if (await this.delegator.clearServiceCache(service)) {
this.props.logger.info(`${service} service caches have been cleared`);
}
}
public async clearAllCache(): Promise<void> {
if (await this.delegator.clearAllCache()) {
this.props.logger.info(`all caches have been cleared`);
}
}
/* event pub/sub */
public async subscribeEvent<Listener extends EventListener | null>(context: APIRequestContext, eventNamePattern: string, listener: Listener): Promise<Listener extends EventListener ? void : AsyncIterator<Readonly<EventPacket>>> {
const subscriptions = this.getEventSubscriptions(context);
if (listener) {
subscriptions.push(...(await this.eventPubSub.subscribe(eventNamePattern, listener!)));
return undefined as any;
}
// returns event packet async iterator when no listener given
const iterator = this.eventPubSub.asyncIterator(eventNamePattern);
subscriptions.push(iterator);
return iterator as any;
}
public async unsubscribeEvent(subscription: number | AsyncIterator<EventPacket>): Promise<void> {
if (typeof subscription === "number") {
this.eventPubSub.unsubscribe(subscription);
} else if (subscription.return) {
await subscription.return();
}
}
public async publishEvent(context: APIRequestContext, args: EventPublishArgs): Promise<void> {
const ctx = this.getDelegatorContext(context);
await this.delegator.publish(ctx, args);
// add from information to original packet and store as example
const packet: EventPacket = args;
packet.from = `${context.id || "unknown"}@${context.ip || "unknown"}`;
this.registry.addEventExample(this.resolveEventName(args.event), packet);
// log
this.props.logger[this.opts.log!.event! ? "info" : "debug"](`published ${kleur.green(packet.event)} ${packet.broadcast ? "broadcast " : ""}event from ${kleur.yellow(packet.from!)}`);
}
/* params mapper */
public createParamsMapper<MappableArgs>(opts: ParamsMapperProps): ParamsMapper<MappableArgs> {
return new ParamsMapper<MappableArgs>(opts);
}
/* compile inline function string */
public createInlineFunction<MappableArgs, Return>(props: InlineFunctionProps<MappableArgs, Return>): (args: MappableArgs) => Return {
return createInlineFunction<MappableArgs, Return>(props, this.opts.function);
}
/* service reporter */
public createReporter(service: Readonly<Service>): Reporter {
return new Reporter({
logger: this.props.logger.getChild(`${service}
`),
service,
props: null,
send: (messages, table) => this.delegator.report(service, messages, table),
}, this.opts.reporter);
}
/* pattern matching for action and event names */
public matchActionName(name: string, namePattern: string): boolean {
return this.delegator.actionNameResolver(name).includes(namePattern);
}
public resolveActionName(name: string): string[] {
return this.delegator.actionNameResolver(name);
}
public matchEventName(name: string, namePattern: string): boolean {
return this.delegator.eventNameResolver(name).includes(namePattern);
}
public resolveEventName(name: string): string[] {
return this.delegator.eventNameResolver(name);
}
/* health check */
public healthCheckService(service: Readonly<Service>) {
return this.delegator.healthCheckService(service);
}
public healthCheckCall(action: Readonly<ServiceAction>) {
return this.delegator.healthCheckCall(action);
}
public healthCheckPublish(args: Omit<EventPublishArgs, "params">) {
return this.delegator.healthCheckPublish(args);
}
public healthCheckSubscribe() {
return this.delegator.healthCheckSubscribe();
}
}