forked from xiangsx/gpt4free-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.ts
425 lines (377 loc) · 12 KB
/
pool.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { v4 } from 'uuid';
import winston from 'winston';
import moment from 'moment';
import { ComError, parseJSON, shuffleArray } from './index';
import fs from 'fs';
import { fileDebouncer } from './file';
import path from 'path';
import { newLogger } from './log';
import { jsonArrayToMarkdownTable, markdownToHTML } from './web';
const PoolDir = './run/pool';
export interface Info {
id: string;
ready: boolean;
}
interface PoolChild<T extends Info> {
get info(): T;
update(v: Partial<T>): void;
// 初始化
init(): Promise<void>;
use(): void;
// 完成调用,释放
release(): void;
// 销毁,删除数据
destroy(options?: DestroyOptions): void;
initFailed(e?: Error): void;
}
export interface ComInfo extends Info {
useCount: number;
lastUseTime: number;
}
export interface DestroyOptions {
delFile: boolean;
delMem: boolean;
}
export interface ChildOptions {
onUpdate: () => void;
onDestroy: (options?: DestroyOptions) => void;
onRelease: () => void;
onInitFailed: (options?: DestroyOptions) => void;
onUse: () => void;
}
export class ComChild<T extends ComInfo> implements PoolChild<T> {
private _info: T;
protected options: ChildOptions | undefined;
protected logger: winston.Logger;
constructor(label: string, info: T, options?: ChildOptions) {
this.logger = newLogger(label);
this._info = info;
this.options = options;
}
get info() {
return this._info;
}
public update(v: Partial<T>) {
Object.assign(this._info, v);
this.options?.onUpdate();
}
init(): Promise<void> {
throw new Error('Method not implemented.');
}
public use(): void {
this.options?.onUse();
// @ts-ignore
this.update({
lastUseTime: moment().unix(),
useCount: (this._info.useCount || 0) + 1,
});
}
public destroy(options?: DestroyOptions): void {
this.options?.onDestroy(options);
}
public release(): void {
this.options?.onRelease();
}
public initFailed(e?: Error): void {
this.options?.onInitFailed({ delFile: false, delMem: true });
}
}
interface PoolOptions<T extends Info> {
delay?: number;
// 串行
serial?: number | (() => number);
preHandleAllInfos?: (allInfos: T[]) => Promise<T[]>;
needDel?: (info: T) => boolean;
}
// 根据maxsize控制创建的数量
// 根据delay控制创建的速度
// 根据children控制创建的对象
// 根据filepath控制保存的路径
// 根据child.valid()判断历史数据是否有效,并如果true则创建并传入历史数据,整个pool的Info历史数据需要保存到文件中,以便下次启动时读取,保存路径由用户指定
// 需要实现方法 pop(弹出一个空闲的child), init(不断定时检测child的数量,维持在maxsize,并打印当前空闲的child数量)
export class Pool<U extends Info, T extends PoolChild<U>> {
private readonly using: Set<string> = new Set();
private allInfos: U[] = [];
private children: T[] = [];
private readonly childMap: Map<string, T> = new Map();
private readonly logger: winston.Logger;
private readonly filepath: string;
private creating = 0;
constructor(
private readonly label: string = 'Unknown',
private readonly maxsize: () => number = () => 0,
private readonly createChild: (info: U, options: ChildOptions) => T,
private readonly isInfoValid: (info: U) => boolean,
private readonly options?: PoolOptions<U>,
) {
this.logger = newLogger(label);
this.filepath = path.join(PoolDir, `${this.label}.json`);
this.init().then();
}
private read() {}
private save() {
fileDebouncer.writeFileSync(this.filepath, this.stringify());
}
private del(id: string, delFile: boolean, delMem: boolean) {
if (delMem) {
this.childMap.delete(id);
this.children = this.children.filter((child) => child.info.id !== id);
}
if (delFile) {
this.allInfos = this.allInfos.filter((v) => v.id !== id);
this.save();
}
}
private stringify() {
return JSON.stringify(this.allInfos);
}
private getOneOldInfo() {
const randomIndex = Math.floor(Math.random() * this.allInfos.length);
for (let idx = 0; idx < this.allInfos.length; idx++) {
const i = (randomIndex + idx) % this.allInfos.length;
const info = this.allInfos[i];
if (!this.childMap.has(info.id) && this.isInfoValid(info)) {
this.logger.debug(`get valid: ${i}/${this.allInfos.length}`);
return info;
}
}
}
async create() {
const oldInfo = this.getOneOldInfo();
const info = oldInfo || ({ id: v4(), ready: false } as U);
const child = this.createChild(info, {
onUpdate: () => {
this.save();
},
onDestroy: (options) => {
const { delFile = false, delMem = true } = options || {};
if (delMem) {
this.del(info.id, delFile, delMem);
this.using.delete(info.id);
}
},
onRelease: () => {
this.using.delete(info.id);
},
onUse: () => {
this.using.add(info.id);
},
onInitFailed: (options) => {
const { delFile = false, delMem = true } = options || {};
this.del(info.id, delFile, delMem);
this.using.delete(info.id);
},
});
child.update({ ready: false } as Partial<U>);
if (!oldInfo) {
this.allInfos.push(child.info);
this.save();
}
this.children.push(child);
this.childMap.set(child.info.id, child);
let start = Date.now();
await child
.init()
.then(() => {
child.update({ ready: true } as Partial<U>);
let init_time = Date.now() - start;
this.logger.info(
`[${init_time} ms] create new child ok, current ready size: ${this.children.reduce(
(prev, cur) => prev + (cur.info.ready ? 1 : 0),
0,
)}/${this.maxsize()}`,
{ init_time },
);
})
.catch((e) => {
this.logger.error(`create new child failed: ${e.message}`);
try {
child.initFailed(e);
} catch (e: any) {
this.logger.error('init failed run failed:', e.message);
}
});
return true;
}
async init() {
if (!fs.existsSync(PoolDir)) {
fs.mkdirSync(PoolDir, { recursive: true });
}
if (!fs.existsSync(this.filepath)) {
fs.writeFileSync(this.filepath, this.stringify());
}
const str = fs.readFileSync(this.filepath, { encoding: 'utf-8' });
this.allInfos = parseJSON<U[]>(str, []);
if (this.options?.preHandleAllInfos) {
this.allInfos = await this.options.preHandleAllInfos(this.allInfos);
}
if (this.options?.needDel) {
this.allInfos = this.allInfos.filter(
(info) => !this.options!.needDel!(info),
);
this.save();
}
this.logger.info('read old info ok, total: ' + this.allInfos.length);
setInterval(async () => {
if (this.options?.preHandleAllInfos) {
this.allInfos = await this.options.preHandleAllInfos(this.allInfos);
}
const maxSize = +this.maxsize() || 0;
if (this.options?.serial) {
const serials =
this.options.serial instanceof Function
? this.options.serial()
: this.options.serial;
if (serials && this.creating >= serials) {
return;
}
}
if (this.children.length === maxSize) {
return;
}
if (this.children.length > maxSize) {
// 随机剔除一个
const randomIndex = Math.floor(Math.random() * this.children.length);
for (let idx = 0; idx < this.children.length; idx++) {
const i = (randomIndex + idx) % this.children.length;
const child = this.children[i];
if (!this.using.has(child.info.id)) {
child.destroy({ delFile: false, delMem: true });
this.logger.info(
`delete child ok: ${i}/${
this.children.length
}, current ready size: ${this.children.reduce(
(prev, cur) => prev + (cur.info.ready ? 1 : 0),
0,
)}/${maxSize}`,
);
break;
}
}
return;
}
const validInfo = this.allInfos.filter((info) => this.isInfoValid(info));
this.logger.info(
`read old info ok, total: ${this.allInfos.length}, valid: ${validInfo.length}, creating: ${this.creating}`,
);
this.creating += 1;
await this.create();
this.creating -= 1;
}, this.options?.delay || 5000);
}
// 从children中弹出一个空闲的child
// 如果没有空闲的child,则等待
// 如果有空闲的child,则返回
// 如果有空闲的child,但是child的数量小于maxsize,则创建一个新的child
async pop(): Promise<T> {
// 随机从数组中的随机位置开始遍历,避免每次都从头开始遍历,遍历全部元素
const randomIndex = Math.floor(Math.random() * this.children.length);
for (let idx = 0; idx < this.children.length; idx++) {
const i = (randomIndex + idx) % this.children.length;
const child = this.children[i];
if (!this.using.has(child.info.id) && child.info.ready) {
this.logger.debug(`pop idx:${i}/${this.children.length}`);
child.use();
return child;
}
}
throw new ComError(
'当前模型负载较高,请稍候重试,或者切换其他模型',
ComError.Status.RequestTooMany,
);
}
async popIf(condition: (v: U) => boolean) {
const randomIndex = Math.floor(Math.random() * this.children.length);
for (let idx = 0; idx < this.children.length; idx++) {
const i = (randomIndex + idx) % this.children.length;
const child = this.children[i];
if (
!this.using.has(child.info.id) &&
child.info.ready &&
condition(child.info)
) {
this.logger.debug(`pop idx:${i}/${this.children.length}`);
child.use();
return child;
}
}
throw new ComError(
'当前模型负载较高,请稍候重试,或者切换其他模型',
ComError.Status.RequestTooMany,
);
}
getValidInfos() {
return this.allInfos.filter((info) => this.isInfoValid(info));
}
getAllInfos() {
return this.allInfos;
}
findOne(func: (v: U) => boolean) {
return this.allInfos.find(func);
}
updateOneInfo(id: string, v: Partial<U>) {
const info = this.allInfos.find((v) => v.id === id);
if (info) {
Object.assign(info, v);
this.save();
}
}
// 转成 html 展示详细 info 列表
showInfosWithMarkdown(title?: string) {
return markdownToHTML(
title || `${process.env['apm.serviceName']}:${this.label}`,
jsonArrayToMarkdownTable(this.allInfos),
);
}
}
class PuppeteerUserDirPool {
static SiteDir = './run/site';
private siteMap: Record<string, string[]> = {};
private using: Set<string> = new Set();
constructor() {
this.readOldSite();
}
private readOldSite() {
const siteDir = PuppeteerUserDirPool.SiteDir;
if (!fs.existsSync(siteDir)) {
fs.mkdirSync(siteDir, { recursive: true });
}
const sites = fs.readdirSync(siteDir);
for (const site of sites) {
const sitePath = path.join(siteDir, site);
const list = fs.readdirSync(sitePath);
this.siteMap[site] = list;
}
}
popUserDir(site: string) {
let siteUserDirs = this.siteMap[site];
let id: string;
if (!siteUserDirs) {
siteUserDirs = [];
this.siteMap[site] = siteUserDirs;
fs.mkdirSync(path.join(PuppeteerUserDirPool.SiteDir, site), {
recursive: true,
});
}
for (const user of siteUserDirs) {
if (!this.using.has(user)) {
this.using.add(user);
const p = path.join(PuppeteerUserDirPool.SiteDir, site, user);
fs.rmSync(path.join(p, 'SingletonLock'), {
force: true,
recursive: true,
});
return p;
}
}
id = v4();
siteUserDirs.push(id);
this.using.add(id);
return path.join(PuppeteerUserDirPool.SiteDir, site, id);
}
releaseUserDir(userDir: string) {
const id = path.basename(userDir);
this.using.delete(id);
}
}
export const puppeteerUserDirPool = new PuppeteerUserDirPool();