topbit 3.1.9 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.cn.md +2 -7
- package/README.md +2 -7
- package/demo/loader.js +33 -9
- package/demo/monitor.js +6 -7
- package/docs/en/topbit-loader.md +6 -2
- package/docs/topbit-loader.md +6 -2
- package/package.json +1 -1
- package/src/http1.js +2 -9
- package/src/http2.js +2 -12
- package/src/loader/loader.js +23 -6
- package/src/middleware1.js +0 -17
- package/src/middleware2.js +0 -19
- package/src/topbit.js +79 -76
- /package/src/{ctxpool.js → @remove-ctxpool.js} +0 -0
package/README.cn.md
CHANGED
|
@@ -848,8 +848,6 @@ app.run(1234)
|
|
|
848
848
|
//最大解析的文件数量
|
|
849
849
|
maxFiles : 12,
|
|
850
850
|
|
|
851
|
-
daemon : false, //开启守护进程
|
|
852
|
-
|
|
853
851
|
/*
|
|
854
852
|
开启守护进程模式后,如果设置路径不为空字符串,则会把pid写入到此文件,可用于服务管理。
|
|
855
853
|
*/
|
|
@@ -935,9 +933,6 @@ app.run(1234)
|
|
|
935
933
|
//url最大长度
|
|
936
934
|
maxUrlLength: 2048,
|
|
937
935
|
|
|
938
|
-
//请求上下文缓存池最大数量。
|
|
939
|
-
maxpool: 4096,
|
|
940
|
-
|
|
941
936
|
//子进程汇报资源信息的定时器毫秒数。
|
|
942
937
|
monitorTimeSlice: 640,
|
|
943
938
|
|
|
@@ -972,9 +967,9 @@ app.run(1234)
|
|
|
972
967
|
this.config.debug && console.error(errname, err)
|
|
973
968
|
},
|
|
974
969
|
|
|
975
|
-
//最大负载率百分比,默认为75表示当CPU使用率超过75%,则会自动创建子进程。
|
|
970
|
+
//最大负载率百分比,默认为0.75表示当CPU使用率超过75%,则会自动创建子进程。
|
|
976
971
|
//必须通过autoWorker开启自动负载模式才有效。
|
|
977
|
-
maxLoadRate: 75,
|
|
972
|
+
maxLoadRate: 0.75,
|
|
978
973
|
|
|
979
974
|
//http2协议的http2Stream超时,若不设置,-1表示和timeout一致。
|
|
980
975
|
streamTimeout: -1,
|
package/README.md
CHANGED
|
@@ -812,8 +812,6 @@ Full configuration options for app initialization are as follows. Please read th
|
|
|
812
812
|
// Max number of files to parse
|
|
813
813
|
maxFiles : 12,
|
|
814
814
|
|
|
815
|
-
daemon : false, // Enable daemon mode
|
|
816
|
-
|
|
817
815
|
/*
|
|
818
816
|
After enabling daemon mode, if path is not empty string, pid is written to this file. Used for service management.
|
|
819
817
|
*/
|
|
@@ -899,9 +897,6 @@ Full configuration options for app initialization are as follows. Please read th
|
|
|
899
897
|
// Max URL length
|
|
900
898
|
maxUrlLength: 2048,
|
|
901
899
|
|
|
902
|
-
// Max number of request context cache pool.
|
|
903
|
-
maxpool: 4096,
|
|
904
|
-
|
|
905
900
|
// Timer milliseconds for child process resource reporting.
|
|
906
901
|
monitorTimeSlice: 640,
|
|
907
902
|
|
|
@@ -936,9 +931,9 @@ Full configuration options for app initialization are as follows. Please read th
|
|
|
936
931
|
this.config.debug && console.error(errname, err)
|
|
937
932
|
},
|
|
938
933
|
|
|
939
|
-
// Max load rate percentage. Default 75 means if CPU usage > 75%, automatically create child process.
|
|
934
|
+
// Max load rate percentage. Default 0.75 means if CPU usage > 75%, automatically create child process.
|
|
940
935
|
// Must enable auto load mode via autoWorker.
|
|
941
|
-
maxLoadRate: 75,
|
|
936
|
+
maxLoadRate: 0.75,
|
|
942
937
|
|
|
943
938
|
// http2 protocol http2Stream timeout. If not set, -1 means consistent with timeout.
|
|
944
939
|
streamTimeout: -1,
|
package/demo/loader.js
CHANGED
|
@@ -4,26 +4,50 @@ process.chdir(__dirname)
|
|
|
4
4
|
|
|
5
5
|
let Topbit = require('../src/topbit.js')
|
|
6
6
|
let {Loader} = Topbit
|
|
7
|
+
const npargv = Topbit.npargv
|
|
8
|
+
|
|
9
|
+
let {args} = npargv({
|
|
10
|
+
'--loadtype': {
|
|
11
|
+
name: 'loadtype',
|
|
12
|
+
default: 'text',
|
|
13
|
+
limit: ['text', 'json', 'orgjson', 'obj', 'orgobj']
|
|
14
|
+
},
|
|
15
|
+
'--load': {
|
|
16
|
+
name: 'load',
|
|
17
|
+
default: false
|
|
18
|
+
},
|
|
19
|
+
'-w': {
|
|
20
|
+
name: 'worker',
|
|
21
|
+
default: 2,
|
|
22
|
+
min: 1,
|
|
23
|
+
max: 5
|
|
24
|
+
}
|
|
25
|
+
})
|
|
7
26
|
|
|
8
27
|
let app = new Topbit({
|
|
9
28
|
debug: true,
|
|
10
|
-
|
|
29
|
+
loadMonitor: true,
|
|
30
|
+
loadInfoType : args.loadtype,
|
|
31
|
+
globalLog : false,
|
|
32
|
+
logType: 'stdio',
|
|
33
|
+
loadInfoFile : args.load ? '' : '/tmp/topbit-loadinfo.log',
|
|
34
|
+
maxLoadRate: 0.85
|
|
11
35
|
})
|
|
12
36
|
|
|
13
37
|
if (app.isWorker) {
|
|
14
38
|
app.get('/', async ctx => {
|
|
15
39
|
ctx.ok('ok')
|
|
16
40
|
})
|
|
17
|
-
|
|
18
|
-
let ld = new Loader()
|
|
19
|
-
|
|
20
|
-
ld.init(app)
|
|
21
41
|
}
|
|
22
42
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
.
|
|
43
|
+
let ld = new Loader()
|
|
44
|
+
|
|
45
|
+
ld.daemonInit(app, () => {
|
|
46
|
+
app.sched('none')
|
|
47
|
+
.autoWorker(12)
|
|
48
|
+
.printServInfo(100)
|
|
49
|
+
.daemon(1234, args.worker)
|
|
50
|
+
})
|
|
27
51
|
|
|
28
52
|
//console.log(app.midware.midGroup)
|
|
29
53
|
//console.log({...app.router})
|
package/demo/monitor.js
CHANGED
|
@@ -9,8 +9,8 @@ let {args} = npargv({
|
|
|
9
9
|
default: 'text',
|
|
10
10
|
limit: ['text', 'json', 'orgjson', 'obj', 'orgobj']
|
|
11
11
|
},
|
|
12
|
-
'--
|
|
13
|
-
name: '
|
|
12
|
+
'--load': {
|
|
13
|
+
name: 'load',
|
|
14
14
|
default: false
|
|
15
15
|
},
|
|
16
16
|
|
|
@@ -32,8 +32,8 @@ const app = new Topbit({
|
|
|
32
32
|
loadInfoType : args.loadtype,
|
|
33
33
|
globalLog : false,
|
|
34
34
|
logType: 'stdio',
|
|
35
|
-
loadInfoFile : args.
|
|
36
|
-
maxLoadRate: 0.
|
|
35
|
+
loadInfoFile : args.load ? '' : '/tmp/topbit-loadinfo.log',
|
|
36
|
+
maxLoadRate: 0.6
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
app.get('/', async c => {
|
|
@@ -46,7 +46,7 @@ app.get('/test', async c => {
|
|
|
46
46
|
for (let i = 0; i < 90000; i++) {
|
|
47
47
|
sum += Math.random() * i;
|
|
48
48
|
}
|
|
49
|
-
c.to({sum});
|
|
49
|
+
c.setHeader(`x-test-${Math.floor(Math.random() * 100)}`, sum).to({sum});
|
|
50
50
|
}, {group: 'test', name : 'test'});
|
|
51
51
|
|
|
52
52
|
app.post('/test', async c => {
|
|
@@ -75,7 +75,6 @@ app.use(async (c, next) => {
|
|
|
75
75
|
|
|
76
76
|
}, {pre: true, method: 'POST', name: 'transmit'});
|
|
77
77
|
|
|
78
|
-
|
|
79
78
|
app.autoWorker(4)
|
|
80
79
|
|
|
81
|
-
app.daemon(2034, 1)
|
|
80
|
+
app.printServInfo().daemon(2034, 1)
|
package/docs/en/topbit-loader.md
CHANGED
|
@@ -101,7 +101,9 @@ new Loader({
|
|
|
101
101
|
const UserModel = require('./model/user')
|
|
102
102
|
service.userModel = new UserModel(service)
|
|
103
103
|
}
|
|
104
|
-
}).init(app)
|
|
104
|
+
}).init(app, () => {
|
|
105
|
+
app.run(1234)
|
|
106
|
+
})
|
|
105
107
|
```
|
|
106
108
|
|
|
107
109
|
---
|
|
@@ -236,7 +238,9 @@ new Loader({
|
|
|
236
238
|
service[name + 'Model'] = new Model(service)
|
|
237
239
|
}
|
|
238
240
|
}
|
|
239
|
-
}).
|
|
241
|
+
}).daemonInit(app, () => {
|
|
242
|
+
app.daemon(1234, 2)
|
|
243
|
+
})
|
|
240
244
|
```
|
|
241
245
|
|
|
242
246
|
---
|
package/docs/topbit-loader.md
CHANGED
|
@@ -99,7 +99,9 @@ new Loader({
|
|
|
99
99
|
const UserModel = require('./model/user')
|
|
100
100
|
service.userModel = new UserModel(service)
|
|
101
101
|
}
|
|
102
|
-
}).init(app)
|
|
102
|
+
}).init(app, () => {
|
|
103
|
+
app.run(1234)
|
|
104
|
+
})
|
|
103
105
|
```
|
|
104
106
|
|
|
105
107
|
---
|
|
@@ -236,7 +238,9 @@ new Loader({
|
|
|
236
238
|
service[name + 'Model'] = new Model(service)
|
|
237
239
|
}
|
|
238
240
|
}
|
|
239
|
-
}).
|
|
241
|
+
}).daemonInit(app, () => {
|
|
242
|
+
app.daemon(1234, 2)
|
|
243
|
+
})
|
|
240
244
|
```
|
|
241
245
|
|
|
242
246
|
---
|
package/package.json
CHANGED
package/src/http1.js
CHANGED
|
@@ -7,7 +7,6 @@ const process = require('node:process');
|
|
|
7
7
|
const logger = require('./logger.js');
|
|
8
8
|
const {fpurl} = require('./fastParseUrl.js');
|
|
9
9
|
const Context = require('./context1.js');
|
|
10
|
-
const ctxpool = require('./ctxpool.js');
|
|
11
10
|
const checkHeaderLimit = require('./headerLimit.js');
|
|
12
11
|
const sendmsg = require('./sendmsg.js');
|
|
13
12
|
|
|
@@ -25,9 +24,6 @@ class Http1 {
|
|
|
25
24
|
this.isWorker = options.isWorker;
|
|
26
25
|
|
|
27
26
|
this.logger = logger;
|
|
28
|
-
ctxpool.max = this.config.maxpool;
|
|
29
|
-
|
|
30
|
-
this.ctxpool = ctxpool;
|
|
31
27
|
this.Context = Context;
|
|
32
28
|
this.fpurl = fpurl;
|
|
33
29
|
this.host = '';
|
|
@@ -109,7 +105,7 @@ class Http1 {
|
|
|
109
105
|
return ;
|
|
110
106
|
}
|
|
111
107
|
|
|
112
|
-
let ctx =
|
|
108
|
+
let ctx = new Context();
|
|
113
109
|
|
|
114
110
|
ctx.bodyLength = 0;
|
|
115
111
|
ctx.maxBody = self.config.maxBody;
|
|
@@ -134,10 +130,7 @@ class Http1 {
|
|
|
134
130
|
ctx.param = rt.args;
|
|
135
131
|
rt = null;
|
|
136
132
|
|
|
137
|
-
return self.midware.run(ctx)
|
|
138
|
-
ctxpool.free(ctx);
|
|
139
|
-
ctx = null;
|
|
140
|
-
});
|
|
133
|
+
return self.midware.run(ctx);
|
|
141
134
|
};
|
|
142
135
|
|
|
143
136
|
return callback;
|
package/src/http2.js
CHANGED
|
@@ -5,7 +5,6 @@ const fs = require('node:fs');
|
|
|
5
5
|
const process = require('node:process');
|
|
6
6
|
const logger = require('./logger.js');
|
|
7
7
|
const {fpurl} = require('./fastParseUrl.js');
|
|
8
|
-
const ctxpool = require('./ctxpool.js');
|
|
9
8
|
const Context = require('./context2.js');
|
|
10
9
|
const checkHeaderLimit = require('./headerLimit.js');
|
|
11
10
|
const sendmsg = require('./sendmsg.js');
|
|
@@ -25,9 +24,6 @@ class Httpt {
|
|
|
25
24
|
this.isWorker = options.isWorker;
|
|
26
25
|
|
|
27
26
|
this.logger = logger;
|
|
28
|
-
ctxpool.max = this.config.maxpool;
|
|
29
|
-
|
|
30
|
-
this.ctxpool = ctxpool;
|
|
31
27
|
this.Context = Context;
|
|
32
28
|
this.fpurl = fpurl;
|
|
33
29
|
|
|
@@ -114,7 +110,7 @@ class Httpt {
|
|
|
114
110
|
stream.close();
|
|
115
111
|
});
|
|
116
112
|
|
|
117
|
-
let ctx =
|
|
113
|
+
let ctx = new Context();
|
|
118
114
|
|
|
119
115
|
ctx.bodyLength = 0;
|
|
120
116
|
ctx.maxBody = self.config.maxBody;
|
|
@@ -128,10 +124,7 @@ class Httpt {
|
|
|
128
124
|
ctx.stream = stream;
|
|
129
125
|
ctx.res = ctx.stream;
|
|
130
126
|
ctx.req = ctx.stream;
|
|
131
|
-
|
|
132
|
-
ctx.dataHeaders = {};
|
|
133
127
|
ctx.headers = headers;
|
|
134
|
-
|
|
135
128
|
ctx.path = urlobj.path;
|
|
136
129
|
ctx.query = urlobj.query;
|
|
137
130
|
ctx.routepath = rt.key;
|
|
@@ -141,10 +134,7 @@ class Httpt {
|
|
|
141
134
|
ctx.param = rt.args;
|
|
142
135
|
rt = null;
|
|
143
136
|
|
|
144
|
-
return self.midware.run(ctx)
|
|
145
|
-
ctxpool.free(ctx);
|
|
146
|
-
ctx = null;
|
|
147
|
-
});
|
|
137
|
+
return self.midware.run(ctx);
|
|
148
138
|
};
|
|
149
139
|
|
|
150
140
|
return callback;
|
package/src/loader/loader.js
CHANGED
|
@@ -17,8 +17,8 @@ class TopbitLoader {
|
|
|
17
17
|
let appDir = '.';
|
|
18
18
|
|
|
19
19
|
this.globalMidTable = [];
|
|
20
|
-
this.groupMidTable =
|
|
21
|
-
this.fileMidTable =
|
|
20
|
+
this.groupMidTable = Object.create(null);
|
|
21
|
+
this.fileMidTable = Object.create(null);
|
|
22
22
|
|
|
23
23
|
if (typeof options !== 'object') {
|
|
24
24
|
options = {};
|
|
@@ -159,10 +159,7 @@ class TopbitLoader {
|
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
|
|
163
|
-
* 初始化入口 (Async)
|
|
164
|
-
*/
|
|
165
|
-
async init(app) {
|
|
162
|
+
async _coreinit(app) {
|
|
166
163
|
// 注入应用基础信息到 service,供 Controller 或 Model 使用
|
|
167
164
|
Object.defineProperties(app.service, {
|
|
168
165
|
__prepath__: {
|
|
@@ -189,6 +186,26 @@ class TopbitLoader {
|
|
|
189
186
|
app.service.__topbit_loader__ = true;
|
|
190
187
|
}
|
|
191
188
|
|
|
189
|
+
/**
|
|
190
|
+
* 初始化入口 (Async),提供回调函数在完成后执行
|
|
191
|
+
*/
|
|
192
|
+
async init(app, cb) {
|
|
193
|
+
await this._coreinit(app);
|
|
194
|
+
if (cb && typeof cb === 'function') {
|
|
195
|
+
cb(app);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async daemonInit(app, cb) {
|
|
200
|
+
if (app.isWorker) {
|
|
201
|
+
await this._coreinit(app);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (cb && typeof cb === 'function') {
|
|
205
|
+
cb(app);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
192
209
|
loadController(app) {
|
|
193
210
|
if (app.service.__topbit_loader__ && !this.config.multi) {
|
|
194
211
|
outWarning('您已经使用topbit-loader加载过路由,多次加载容易导致路由冲突,重复操作将会被终止。');
|
package/src/middleware1.js
CHANGED
|
@@ -15,30 +15,13 @@ class Middleware extends MidCore {
|
|
|
15
15
|
try {
|
|
16
16
|
await this.exec(ctx, ctx.group);
|
|
17
17
|
} catch (err) {
|
|
18
|
-
|
|
19
18
|
this.errorHandle(err, '--ERR-res--');
|
|
20
|
-
|
|
21
19
|
try {
|
|
22
20
|
if (ctx.res && !ctx.res.writableEnded) {
|
|
23
21
|
ctx.res.statusCode = 500;
|
|
24
22
|
ctx.res.end();
|
|
25
23
|
}
|
|
26
24
|
} catch (err) {}
|
|
27
|
-
|
|
28
|
-
} finally {
|
|
29
|
-
ctx.req = null;
|
|
30
|
-
ctx.res = null;
|
|
31
|
-
ctx.data = null;
|
|
32
|
-
ctx.box = null;
|
|
33
|
-
ctx.service = null;
|
|
34
|
-
ctx.requestCall = null;
|
|
35
|
-
ctx.headers = null;
|
|
36
|
-
ctx.body = null;
|
|
37
|
-
ctx.rawBody = null;
|
|
38
|
-
ctx.files = null;
|
|
39
|
-
ctx.param = null;
|
|
40
|
-
ctx.user = null;
|
|
41
|
-
ctx = null;
|
|
42
25
|
}
|
|
43
26
|
}
|
|
44
27
|
|
package/src/middleware2.js
CHANGED
|
@@ -16,9 +16,7 @@ class Middleware extends MidCore {
|
|
|
16
16
|
try {
|
|
17
17
|
await this.exec(ctx, ctx.group);
|
|
18
18
|
} catch (err) {
|
|
19
|
-
|
|
20
19
|
this.errorHandle(err, '--ERR-RESPONSE--');
|
|
21
|
-
|
|
22
20
|
if (ctx.stream && !ctx.stream.destroyed && ctx.stream.writable) {
|
|
23
21
|
try {
|
|
24
22
|
if (!ctx.stream.headersSent) {
|
|
@@ -30,24 +28,7 @@ class Middleware extends MidCore {
|
|
|
30
28
|
ctx.stream.end();
|
|
31
29
|
} catch (err) {}
|
|
32
30
|
}
|
|
33
|
-
} finally {
|
|
34
|
-
ctx.data = null;
|
|
35
|
-
ctx.dataHeaders = null;
|
|
36
|
-
ctx.stream = null;
|
|
37
|
-
ctx.req = null;
|
|
38
|
-
ctx.res = null;
|
|
39
|
-
ctx.service = null;
|
|
40
|
-
ctx.box = null;
|
|
41
|
-
ctx.requestCall = null;
|
|
42
|
-
ctx.body = null;
|
|
43
|
-
ctx.headers = null;
|
|
44
|
-
ctx.rawBody = null;
|
|
45
|
-
ctx.files = null;
|
|
46
|
-
ctx.param = null;
|
|
47
|
-
ctx.user = null;
|
|
48
|
-
ctx = null;
|
|
49
31
|
}
|
|
50
|
-
|
|
51
32
|
}
|
|
52
33
|
|
|
53
34
|
/** 这是最终添加的请求中间件。基于洋葱模型,这个中间件最先执行,所以最后会返回响应结果。 */
|
package/src/topbit.js
CHANGED
|
@@ -140,7 +140,6 @@ class Topbit {
|
|
|
140
140
|
* - globalLog {bool} 启用全局日志。
|
|
141
141
|
* - maxBody {number} 表示POST/PUT提交表单的最大字节数,包括上传文件。
|
|
142
142
|
* - maxFiles {number} 最大上传文件数量,超过则不处理。
|
|
143
|
-
* - daemon {bool} 启用守护进程模式。
|
|
144
143
|
* - pidFile {string} 保存Master进程PID的文件路径。
|
|
145
144
|
* - logFile {string} 日志文件。
|
|
146
145
|
* - errorLogFile {string} 错误日志文件。
|
|
@@ -156,7 +155,6 @@ class Topbit {
|
|
|
156
155
|
* - memFactor {number} 控制内存最大使用量的系数,范围从 -0.45 ~ 0.45,会使用基本系数加上此值并乘以内存总量。默认值0.28。
|
|
157
156
|
* RSS基本系数是0.52。不要设置的太低,提供比较低的值是为了测试使用。
|
|
158
157
|
* - maxUrlLength 最大URL长度,包括path和querystring
|
|
159
|
-
* - maxpool 请求上下文的最大缓存池数量。
|
|
160
158
|
* - loadMonitor true|false,表示是否启用负载监控功能,在daemon模式有效,默认为true。
|
|
161
159
|
* - monitorTimeSlice 子进程获取系统占用资源的定时器时间片,毫秒值,默认为500。
|
|
162
160
|
* - maxQuery 最大允许的querystring的参数,默认为12。
|
|
@@ -172,6 +170,9 @@ class Topbit {
|
|
|
172
170
|
throw new Error('topbit遵循单例模式,不能构造多次。你可以在多进程或多线程中构造新的实例。');
|
|
173
171
|
|
|
174
172
|
__instance__ += 1;
|
|
173
|
+
|
|
174
|
+
this._is_listening = false;
|
|
175
|
+
this._is_daemon_listening = false;
|
|
175
176
|
|
|
176
177
|
this.config = {
|
|
177
178
|
//此配置表示POST/PUT提交表单的最大字节数,也是上传文件的最大限制,
|
|
@@ -238,8 +239,6 @@ class Topbit {
|
|
|
238
239
|
|
|
239
240
|
maxUrlLength: 1152,
|
|
240
241
|
|
|
241
|
-
maxpool : 8192,
|
|
242
|
-
|
|
243
242
|
//子进程汇报资源信息的定时器毫秒数。
|
|
244
243
|
monitorTimeSlice: 500,
|
|
245
244
|
|
|
@@ -337,10 +336,6 @@ class Topbit {
|
|
|
337
336
|
case 'maxUrlLength':
|
|
338
337
|
optionsCheck(k, options[k], this.config, {type: 'number', min: 1, max: 4096});
|
|
339
338
|
break;
|
|
340
|
-
|
|
341
|
-
case 'maxpool':
|
|
342
|
-
optionsCheck(k, options[k], this.config, {type: 'number', min: 2, max: 50000});
|
|
343
|
-
break;
|
|
344
339
|
|
|
345
340
|
case 'monitorTimeSlice':
|
|
346
341
|
optionsCheck(k, options[k], this.config, {type: 'number', min: 5, max: 10000});
|
|
@@ -856,71 +851,6 @@ class Topbit {
|
|
|
856
851
|
clearService() {
|
|
857
852
|
for (let k in this.service) delete this.service[k];
|
|
858
853
|
}
|
|
859
|
-
|
|
860
|
-
/**
|
|
861
|
-
* 根据配置情况确定运行HTTP/1.1还是HTTP/2
|
|
862
|
-
* @param {number} port 端口号
|
|
863
|
-
* @param {string} host IP地址,可以是IPv4或IPv6
|
|
864
|
-
* 0.0.0.0 对应使用IPv6则是::
|
|
865
|
-
*/
|
|
866
|
-
run(port=2368, host='0.0.0.0') {
|
|
867
|
-
if (typeof port === 'object') {
|
|
868
|
-
if (port.host && typeof port.host === 'string') host = port.host;
|
|
869
|
-
if (port.port && typeof port.port === 'number' && port.port > 0 && port.port <= 65535) port = port.port;
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
if (this.config.server.SNICallback && typeof this.config.server.SNICallback === 'function' && !this.config.https) {
|
|
873
|
-
this.config.https = true;
|
|
874
|
-
}
|
|
875
|
-
|
|
876
|
-
this.router.argsRouteSort();
|
|
877
|
-
|
|
878
|
-
this.rundata.host = (typeof port == 'number' ? host : '');
|
|
879
|
-
this.rundata.port = port;
|
|
880
|
-
|
|
881
|
-
//如果没有添加路由则添加默认路由
|
|
882
|
-
if (this.router.count === 0) {
|
|
883
|
-
this.router.get('/*', async c => {
|
|
884
|
-
c.setHeader('content-type', 'text/html; charset=utf-8').to(_topbit_home_page)
|
|
885
|
-
})
|
|
886
|
-
}
|
|
887
|
-
|
|
888
|
-
//如果发现更改了service指向,则让this.httpServ.service重新指向this.service。
|
|
889
|
-
if (this.service !== this.httpServ.service) {
|
|
890
|
-
this.httpServ.service = this.service;
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
this.midware.addFromCache();
|
|
894
|
-
|
|
895
|
-
this.config.parseBody && this.add(this.bodyparser);
|
|
896
|
-
|
|
897
|
-
this.add(this.httpServ);
|
|
898
|
-
|
|
899
|
-
let m = null;
|
|
900
|
-
while((m = this.__pre_mids__.pop()) !== undefined) {
|
|
901
|
-
this.add(m.callback, m.options);
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
//必须放在最后,用于返回最终数据。
|
|
905
|
-
this.midware.addFinal();
|
|
906
|
-
|
|
907
|
-
if (this.config.useLimit) {
|
|
908
|
-
let connlimit = new this.connfilter(this.limit, this.rundata);
|
|
909
|
-
this.on('connection', connlimit.callback);
|
|
910
|
-
} else {
|
|
911
|
-
this.on('connection', (sock) => {
|
|
912
|
-
this.rundata.conn++;
|
|
913
|
-
sock.on('close', () => {
|
|
914
|
-
this.rundata.conn--;
|
|
915
|
-
});
|
|
916
|
-
|
|
917
|
-
});
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
this.server = this.httpServ.run(port, host);
|
|
921
|
-
return this.server;
|
|
922
|
-
}
|
|
923
|
-
//run end
|
|
924
854
|
|
|
925
855
|
/**
|
|
926
856
|
*
|
|
@@ -1148,7 +1078,6 @@ class Topbit {
|
|
|
1148
1078
|
cpu: {user:0, system:0},
|
|
1149
1079
|
cputm : 1000000
|
|
1150
1080
|
};
|
|
1151
|
-
|
|
1152
1081
|
});
|
|
1153
1082
|
|
|
1154
1083
|
let exitTip = () => {
|
|
@@ -1218,6 +1147,76 @@ class Topbit {
|
|
|
1218
1147
|
|
|
1219
1148
|
}
|
|
1220
1149
|
|
|
1150
|
+
/**
|
|
1151
|
+
* 根据配置情况确定运行HTTP/1.1还是HTTP/2
|
|
1152
|
+
* @param {number} port 端口号
|
|
1153
|
+
* @param {string} host IP地址,可以是IPv4或IPv6
|
|
1154
|
+
* 0.0.0.0 对应使用IPv6则是::
|
|
1155
|
+
*/
|
|
1156
|
+
run(port=2368, host='0.0.0.0') {
|
|
1157
|
+
if (this._is_listening) return this.server;
|
|
1158
|
+
|
|
1159
|
+
if (typeof port === 'object') {
|
|
1160
|
+
if (port.host && typeof port.host === 'string') host = port.host;
|
|
1161
|
+
if (port.port && typeof port.port === 'number' && port.port > 0 && port.port <= 65535) port = port.port;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
if (this.config.server.SNICallback && typeof this.config.server.SNICallback === 'function' && !this.config.https) {
|
|
1165
|
+
this.config.https = true;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
this._is_listening = true;
|
|
1169
|
+
|
|
1170
|
+
this.router.argsRouteSort();
|
|
1171
|
+
|
|
1172
|
+
this.rundata.host = (typeof port == 'number' ? host : '');
|
|
1173
|
+
this.rundata.port = port;
|
|
1174
|
+
|
|
1175
|
+
//如果没有添加路由则添加默认路由
|
|
1176
|
+
if (this.router.count === 0) {
|
|
1177
|
+
this.router.get('/*', async c => {
|
|
1178
|
+
c.setHeader('content-type', 'text/html; charset=utf-8').to(_topbit_home_page)
|
|
1179
|
+
})
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
//如果发现更改了service指向,则让this.httpServ.service重新指向this.service。
|
|
1183
|
+
if (this.service !== this.httpServ.service) {
|
|
1184
|
+
this.httpServ.service = this.service;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
//必须要坚持先加载再listen的原则。
|
|
1188
|
+
this.midware.addFromCache();
|
|
1189
|
+
|
|
1190
|
+
this.config.parseBody && this.add(this.bodyparser);
|
|
1191
|
+
|
|
1192
|
+
this.add(this.httpServ);
|
|
1193
|
+
|
|
1194
|
+
let m = null;
|
|
1195
|
+
while((m = this.__pre_mids__.pop()) !== undefined) {
|
|
1196
|
+
this.add(m.callback, m.options);
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
//必须放在最后,用于返回最终数据。
|
|
1200
|
+
this.midware.addFinal();
|
|
1201
|
+
|
|
1202
|
+
if (this.config.useLimit) {
|
|
1203
|
+
let connlimit = new this.connfilter(this.limit, this.rundata);
|
|
1204
|
+
this.on('connection', connlimit.callback);
|
|
1205
|
+
} else {
|
|
1206
|
+
this.on('connection', (sock) => {
|
|
1207
|
+
this.rundata.conn++;
|
|
1208
|
+
sock.on('close', () => {
|
|
1209
|
+
this.rundata.conn--;
|
|
1210
|
+
});
|
|
1211
|
+
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
this.server = this.httpServ.run(port, host);
|
|
1216
|
+
return this.server;
|
|
1217
|
+
}
|
|
1218
|
+
//run end
|
|
1219
|
+
|
|
1221
1220
|
/**
|
|
1222
1221
|
* 这个函数是可以用于运维部署,此函数默认会根据CPU核数创建对应的子进程处理请求。
|
|
1223
1222
|
* @param {number} port 端口号
|
|
@@ -1225,6 +1224,8 @@ class Topbit {
|
|
|
1225
1224
|
* @param {number} num,要创建的子进程数量,0表示自动,这时候根据CPU核心数量创建。
|
|
1226
1225
|
*/
|
|
1227
1226
|
daemon(port=2368, host='0.0.0.0', num=0) {
|
|
1227
|
+
if (this._is_daemon_listening) return this;
|
|
1228
|
+
|
|
1228
1229
|
if (typeof host === 'number') {
|
|
1229
1230
|
num = host;
|
|
1230
1231
|
host = '0.0.0.0';
|
|
@@ -1236,14 +1237,16 @@ class Topbit {
|
|
|
1236
1237
|
if (port.port && typeof port.port === 'number' && port.port > 0 && port.port <= 65535) port = port.port;
|
|
1237
1238
|
}
|
|
1238
1239
|
|
|
1240
|
+
this._is_daemon_listening = true;
|
|
1239
1241
|
//确保自动创建的worker在终止时不会误认为是系统错误。
|
|
1240
1242
|
setTimeout(() => {
|
|
1241
1243
|
this.errorBreakCount += 1;
|
|
1242
1244
|
}, this.workerErrorTime + 120);
|
|
1243
1245
|
|
|
1244
|
-
|
|
1246
|
+
//暂时去掉,并且屏蔽daemon选项。
|
|
1247
|
+
//this._checkDaemonArgs();
|
|
1245
1248
|
|
|
1246
|
-
if (cluster.isMaster) {
|
|
1249
|
+
if (cluster.isPrimary || cluster.isMaster) {
|
|
1247
1250
|
let osCPUS = os.cpus().length;
|
|
1248
1251
|
if (num > (osCPUS * 2) ) {
|
|
1249
1252
|
num = 0;
|
|
File without changes
|