topbit 3.1.7 → 3.1.9
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 +4 -3
- package/README.md +5 -4
- package/demo/http2.js +20 -5
- package/demo/loader.js +3 -4
- package/docs/en/topbit-loader.md +5 -2
- package/docs/topbit-loader.md +5 -2
- package/package.json +1 -1
- package/src/httpc.js +1 -1
- package/src/topbit.js +70 -34
- package/test/test-bigctx.js +16 -13
- package/test/test-daemon-args.js +2 -2
package/README.cn.md
CHANGED
|
@@ -62,7 +62,8 @@ const app = new Topbit({
|
|
|
62
62
|
debug: true
|
|
63
63
|
})
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
//输出服务信息并运行服务
|
|
66
|
+
app.printServInfo().run(1234)
|
|
66
67
|
|
|
67
68
|
```
|
|
68
69
|
|
|
@@ -112,7 +113,7 @@ app.get('/', async ctx => {
|
|
|
112
113
|
ctx.to('success')
|
|
113
114
|
})
|
|
114
115
|
|
|
115
|
-
app.run(1234)
|
|
116
|
+
app.printServInfo().run(1234)
|
|
116
117
|
|
|
117
118
|
```
|
|
118
119
|
|
|
@@ -159,7 +160,7 @@ app.put('/p', async c => {
|
|
|
159
160
|
})
|
|
160
161
|
|
|
161
162
|
//默认监听0.0.0.0,参数和原生接口listen一致。
|
|
162
|
-
app.run(8080)
|
|
163
|
+
app.printServInfo().run(8080)
|
|
163
164
|
|
|
164
165
|
```
|
|
165
166
|
|
package/README.md
CHANGED
|
@@ -46,7 +46,8 @@ const app = new Topbit({
|
|
|
46
46
|
debug: true
|
|
47
47
|
})
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
//in debug mode, output service info
|
|
50
|
+
app.printServInfo().run(1234)
|
|
50
51
|
```
|
|
51
52
|
|
|
52
53
|
When no routes are added, Topbit adds a default route:
|
|
@@ -72,7 +73,7 @@ app.get('/', async ctx => {
|
|
|
72
73
|
})
|
|
73
74
|
|
|
74
75
|
// Defaults to listening on 0.0.0.0, parameters are consistent with the native listen interface.
|
|
75
|
-
app.run(1234)
|
|
76
|
+
app.printServInfo().run(1234)
|
|
76
77
|
|
|
77
78
|
```
|
|
78
79
|
|
|
@@ -94,7 +95,7 @@ app.get('/', async ctx => {
|
|
|
94
95
|
ctx.to('success')
|
|
95
96
|
})
|
|
96
97
|
|
|
97
|
-
app.run(1234)
|
|
98
|
+
app.printServInfo().run(1234)
|
|
98
99
|
|
|
99
100
|
```
|
|
100
101
|
|
|
@@ -141,7 +142,7 @@ app.put('/p', async c => {
|
|
|
141
142
|
})
|
|
142
143
|
|
|
143
144
|
// Defaults to listening on 0.0.0.0
|
|
144
|
-
app.run(8080)
|
|
145
|
+
app.printServInfo().run(8080)
|
|
145
146
|
|
|
146
147
|
```
|
|
147
148
|
|
package/demo/http2.js
CHANGED
|
@@ -5,12 +5,17 @@
|
|
|
5
5
|
process.chdir(__dirname)
|
|
6
6
|
|
|
7
7
|
let Topbit = require('../src/topbit.js')
|
|
8
|
-
let {Loader} = Topbit
|
|
8
|
+
let {Loader, npargv} = Topbit
|
|
9
|
+
|
|
10
|
+
let {args} = npargv({
|
|
11
|
+
'--httpc': 'httpc'
|
|
12
|
+
})
|
|
9
13
|
|
|
10
14
|
let app = new Topbit({
|
|
11
15
|
debug: true,
|
|
12
16
|
globalLog: true,
|
|
13
17
|
logType: 'stdio',
|
|
18
|
+
allowHTTP1: args.httpc,
|
|
14
19
|
loadInfoFile: '--mem',
|
|
15
20
|
cert: './cert/localhost-cert.pem',
|
|
16
21
|
key: './cert/localhost-privkey.pem',
|
|
@@ -25,10 +30,20 @@ if (app.isWorker) {
|
|
|
25
30
|
let ld = new Loader()
|
|
26
31
|
|
|
27
32
|
ld.init(app)
|
|
28
|
-
}
|
|
29
33
|
|
|
30
|
-
app.
|
|
34
|
+
app.get('/httpc/:id', async ctx => {
|
|
35
|
+
console.log(ctx.headers, ctx.method, ctx.path, ctx.name, ctx.group)
|
|
36
|
+
ctx.to({
|
|
37
|
+
major: ctx.major,
|
|
38
|
+
protocol: ctx.protocol,
|
|
39
|
+
port: ctx.port,
|
|
40
|
+
group: ctx.group
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
}
|
|
31
44
|
|
|
32
|
-
app.autoWorker(3)
|
|
33
45
|
|
|
34
|
-
app.
|
|
46
|
+
app.sched('none')
|
|
47
|
+
.autoWorker(3)
|
|
48
|
+
.printServInfo()
|
|
49
|
+
.daemon(1234, 2)
|
package/demo/loader.js
CHANGED
package/docs/en/topbit-loader.md
CHANGED
|
@@ -61,8 +61,11 @@ if (app.isWorker) {
|
|
|
61
61
|
new Loader().init(app)
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
app.
|
|
64
|
+
// Max elastic workers
|
|
65
|
+
app.autoWorker(16)
|
|
66
|
+
//delay 100ms, output service info
|
|
67
|
+
.printServInfo(100)
|
|
68
|
+
.daemon(443, 4) // 4 base workers
|
|
66
69
|
```
|
|
67
70
|
|
|
68
71
|
Run `node app.js` → full-featured service is up!
|
package/docs/topbit-loader.md
CHANGED
|
@@ -59,8 +59,11 @@ if (app.isWorker) {
|
|
|
59
59
|
new Loader().init(app)
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
app.
|
|
62
|
+
// 最大弹性进程数
|
|
63
|
+
app.autoWorker(16)
|
|
64
|
+
//延迟100ms后输出服务运行信息
|
|
65
|
+
.printServInfo(100)
|
|
66
|
+
.daemon(443, 4) // 4 个基础进程
|
|
64
67
|
```
|
|
65
68
|
|
|
66
69
|
只需执行 `node app.js` 即可启动完整服务!
|
package/package.json
CHANGED
package/src/httpc.js
CHANGED
package/src/topbit.js
CHANGED
|
@@ -39,7 +39,7 @@ const ErrorLog = require('./lib/errorlog.js')
|
|
|
39
39
|
let __instance__ = 0;
|
|
40
40
|
|
|
41
41
|
let _topbit_server_running = `
|
|
42
|
-
|
|
42
|
+
:.:.:.:.:\x1b[34;5m.\x1b[0m:\x1b[36;5m.\x1b[0m topbit in service \x1b[36;5m.\x1b[0m:\x1b[34;5m.\x1b[0m:.:.:.:.:
|
|
43
43
|
`;
|
|
44
44
|
|
|
45
45
|
let _topbit_home_page = `<!DOCTYPE html><html>
|
|
@@ -916,39 +916,70 @@ class Topbit {
|
|
|
916
916
|
|
|
917
917
|
});
|
|
918
918
|
}
|
|
919
|
-
|
|
920
|
-
/**
|
|
921
|
-
* 输出路由表,如果是启用了cluster,则通过发送消息的方式让master进程输出。
|
|
922
|
-
* */
|
|
923
|
-
if (this.config.debug) {
|
|
924
|
-
if (typeof port === 'string' && port.indexOf('.sock') > 0) {
|
|
925
|
-
host = '';
|
|
926
|
-
}
|
|
927
|
-
|
|
928
|
-
let is_https = this.config.https;
|
|
929
|
-
|
|
930
|
-
let protocol = this.config.http2
|
|
931
|
-
? ('http2' + (is_https ? '[https]' : ''))
|
|
932
|
-
: (is_https ? 'https' : 'http');
|
|
933
|
-
|
|
934
|
-
if (cluster.isMaster) {
|
|
935
|
-
this.router.printTable();
|
|
936
|
-
console.log(`PID: ${process.pid}, Listen ${host}:${port}, Protocol: ${protocol}`);
|
|
937
|
-
console.log(_topbit_server_running);
|
|
938
|
-
} else if (process.send && typeof process.send === 'function') {
|
|
939
|
-
process.send({type:'_route-table',
|
|
940
|
-
route: this.router.getTable(),
|
|
941
|
-
listen: `Listen: ${host}${host.length > 0 ? ':' : ''}${port}, `,
|
|
942
|
-
protocol: `Protocol: ${protocol}`
|
|
943
|
-
});
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
|
|
919
|
+
|
|
947
920
|
this.server = this.httpServ.run(port, host);
|
|
948
921
|
return this.server;
|
|
949
922
|
}
|
|
950
923
|
//run end
|
|
951
924
|
|
|
925
|
+
/**
|
|
926
|
+
*
|
|
927
|
+
* @param {number} timeout - default 0
|
|
928
|
+
* @param {boolean} debug - default true(true表示只有debug模式才会输出)
|
|
929
|
+
* @returns
|
|
930
|
+
*/
|
|
931
|
+
printServInfo(timeout=0, debug=true) {
|
|
932
|
+
if (typeof timeout === 'boolean') {
|
|
933
|
+
debug = timeout
|
|
934
|
+
timeout = 0
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
if (!debug || (debug && this.config.debug)) {
|
|
938
|
+
if (timeout > 0) {
|
|
939
|
+
setTimeout(() => {
|
|
940
|
+
this._corePrintServ()
|
|
941
|
+
}, timeout)
|
|
942
|
+
} else {
|
|
943
|
+
queueMicrotask(() => {
|
|
944
|
+
this._corePrintServ()
|
|
945
|
+
})
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
return this
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
_corePrintServ() {
|
|
953
|
+
let {port, host} = this.rundata
|
|
954
|
+
|
|
955
|
+
/**
|
|
956
|
+
* 输出路由表,如果是启用了cluster,则通过发送消息的方式让master进程输出。
|
|
957
|
+
* */
|
|
958
|
+
if (typeof port === 'string' && port.indexOf('.sock') > 0) {
|
|
959
|
+
host = '';
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
let is_https = this.config.https;
|
|
963
|
+
|
|
964
|
+
let protocol = this.config.http2
|
|
965
|
+
? ('http2' + (is_https ? '[https]' : ''))
|
|
966
|
+
: (is_https ? 'https' : 'http');
|
|
967
|
+
|
|
968
|
+
if (cluster.isMaster) {
|
|
969
|
+
this.router.printTable();
|
|
970
|
+
console.log(`PID: ${process.pid}, Listen ${host}:${port}, Protocol: ${protocol}`);
|
|
971
|
+
console.log(_topbit_server_running);
|
|
972
|
+
} else if (process.send && typeof process.send === 'function') {
|
|
973
|
+
process.send({type:'_route-table',
|
|
974
|
+
route: this.router.getTable(),
|
|
975
|
+
listen: `Listen: ${host}${host.length > 0 ? ':' : ''}${port}, `,
|
|
976
|
+
protocol: `Protocol: ${protocol}`
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
return this
|
|
981
|
+
}
|
|
982
|
+
|
|
952
983
|
/**
|
|
953
984
|
* @param {string} evt
|
|
954
985
|
* @param {function} callback
|
|
@@ -968,8 +999,9 @@ class Topbit {
|
|
|
968
999
|
count: 0,
|
|
969
1000
|
mode: mode,
|
|
970
1001
|
callback: callback
|
|
971
|
-
}
|
|
972
|
-
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
return this;
|
|
973
1005
|
}
|
|
974
1006
|
|
|
975
1007
|
getMsgEvent(evt) {
|
|
@@ -1047,11 +1079,13 @@ class Topbit {
|
|
|
1047
1079
|
}
|
|
1048
1080
|
|
|
1049
1081
|
autoWorker(max) {
|
|
1050
|
-
if (typeof max === 'number' && max
|
|
1051
|
-
this.workerCount.max = max;
|
|
1082
|
+
if (!isNaN(max) && typeof max === 'number' && max >= 0) {
|
|
1083
|
+
this.workerCount.max = max > 0 ? max : os.cpus().length;
|
|
1052
1084
|
} else {
|
|
1053
|
-
throw new Error('autoWorker
|
|
1085
|
+
throw new Error('autoWorker参数必须是一个>=0的数字,表示最大允许创建多少个子进程处理请求。');
|
|
1054
1086
|
}
|
|
1087
|
+
|
|
1088
|
+
return this;
|
|
1055
1089
|
}
|
|
1056
1090
|
|
|
1057
1091
|
/**
|
|
@@ -1066,6 +1100,8 @@ class Topbit {
|
|
|
1066
1100
|
} else {
|
|
1067
1101
|
return cluster.schedulingPolicy;
|
|
1068
1102
|
}
|
|
1103
|
+
|
|
1104
|
+
return this;
|
|
1069
1105
|
}
|
|
1070
1106
|
|
|
1071
1107
|
_checkDaemonArgs() {
|
package/test/test-bigctx.js
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
'use'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const Topbit = require('../src/topbit.js');
|
|
4
|
+
|
|
5
|
+
let app = new Topbit({
|
|
4
6
|
debug: true,
|
|
5
7
|
//http2: true
|
|
6
8
|
});
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
let start_time = Date.now()
|
|
9
11
|
|
|
10
|
-
|
|
12
|
+
let ctx = null
|
|
11
13
|
|
|
12
|
-
let total = 20000
|
|
14
|
+
let total = 20000
|
|
13
15
|
|
|
14
|
-
for (let i=0
|
|
15
|
-
ctx = new app.httpServ.Context()
|
|
16
|
+
for (let i=0; i < total; i++) {
|
|
17
|
+
ctx = new app.httpServ.Context()
|
|
16
18
|
ctx.path = '/';
|
|
17
19
|
ctx.ip = '127.0.0.1';
|
|
18
|
-
ctx.requestCall =
|
|
19
|
-
c.
|
|
20
|
-
}
|
|
20
|
+
ctx.requestCall = c => {
|
|
21
|
+
c.to('success')
|
|
22
|
+
}
|
|
23
|
+
|
|
21
24
|
ctx.box.rand = Math.random()
|
|
22
|
-
ctx = null
|
|
25
|
+
ctx = null
|
|
23
26
|
}
|
|
24
27
|
|
|
25
|
-
|
|
28
|
+
let end_time = Date.now()
|
|
26
29
|
|
|
27
30
|
let rtm = end_time - start_time;
|
|
28
31
|
|
|
29
|
-
console.log(rtm, 'ms', `${parseInt(total * 1000 / rtm)}/s`)
|
|
32
|
+
console.log(rtm, 'ms', `${parseInt(total * 1000 / rtm)}/s`)
|
package/test/test-daemon-args.js
CHANGED