Skip to content

Commit

Permalink
feat: enable asyncLocalStorage by default
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 6, 2022
1 parent f25a1c5 commit 01201c6
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [8, 10, 12, 14, 16]
node-version: [8, 10, 12, 14, 16, 18]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
Expand Down
56 changes: 56 additions & 0 deletions benchmark/middleware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,59 @@ Transfer/sec: 6.01MB
```

![](https://user-images.githubusercontent.com/985607/32474444-2f4b7cde-c332-11e7-923f-8dfb709a7a24.png)

### 2022-12-06

Enable asyncLocalStorage

```
v18.12.1
server started at 7001
------- generator middleware -------
["generator middleware #1","generator middleware #2","generator middleware #3","generator middleware #4","generator middleware #5","generator middleware #6","generator middleware #7","generator middleware #8","generator middleware #9","generator middleware #10","generator middleware #11","generator middleware #12","generator middleware #13","generator middleware #14","generator middleware #15","generator middleware #16","generator middleware #17","generator middleware #18","generator middleware #19","generator middleware #20"]
Running 10s test @ http://127.0.0.1:7001/generator
8 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 2.42ms 584.46us 12.20ms 94.43%
Req/Sec 2.50k 601.24 18.36k 97.63%
198950 requests in 10.10s, 153.00MB read
Requests/sec: 19699.09
Transfer/sec: 15.15MB
------- async middleware -------
["async middleware #1","async middleware #2","async middleware #3","async middleware #4","async middleware #5","async middleware #6","async middleware #7","async middleware #8","async middleware #9","async middleware #10","async middleware #11","async middleware #12","async middleware #13","async middleware #14","async middleware #15","async middleware #16","async middleware #17","async middleware #18","async middleware #19","async middleware #20"]
Running 10s test @ http://127.0.0.1:7001/async
8 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.97ms 1.58ms 43.26ms 96.10%
Req/Sec 3.23k 0.86k 24.84k 95.38%
257959 requests in 10.10s, 179.02MB read
Requests/sec: 25533.50
Transfer/sec: 17.72MB
```

Disable asyncLocalStorage

```
v18.12.1
server started at 7001
------- generator middleware -------
["generator middleware #1","generator middleware #2","generator middleware #3","generator middleware #4","generator middleware #5","generator middleware #6","generator middleware #7","generator middleware #8","generator middleware #9","generator middleware #10","generator middleware #11","generator middleware #12","generator middleware #13","generator middleware #14","generator middleware #15","generator middleware #16","generator middleware #17","generator middleware #18","generator middleware #19","generator middleware #20"]
Running 10s test @ http://127.0.0.1:7001/generator
8 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 2.22ms 1.18ms 38.70ms 93.88%
Req/Sec 2.79k 361.32 3.27k 85.50%
222356 requests in 10.01s, 171.13MB read
Requests/sec: 22208.19
Transfer/sec: 17.09MB
------- async middleware -------
["async middleware #1","async middleware #2","async middleware #3","async middleware #4","async middleware #5","async middleware #6","async middleware #7","async middleware #8","async middleware #9","async middleware #10","async middleware #11","async middleware #12","async middleware #13","async middleware #14","async middleware #15","async middleware #16","async middleware #17","async middleware #18","async middleware #19","async middleware #20"]
Running 10s test @ http://127.0.0.1:7001/async
8 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.71ms 0.98ms 33.63ms 94.19%
Req/Sec 3.60k 511.38 10.65k 91.28%
288119 requests in 10.10s, 200.07MB read
Requests/sec: 28513.37
Transfer/sec: 19.80MB
```
4 changes: 3 additions & 1 deletion lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class EggCore extends KoaApplication {
assert(fs.statSync(options.baseDir).isDirectory(), `Directory ${options.baseDir} is not a directory`);
assert(options.type === 'application' || options.type === 'agent', 'options.type should be application or agent');

super();
// enable asyncLocalStorage by default
// https://github.com/koajs/koa/pull/1721
super({ asyncLocalStorage: true });

this.timing = new Timing();

Expand Down
4 changes: 2 additions & 2 deletions lib/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const getReady = require('get-ready');
const { Ready } = require('ready-callback');
const { EventEmitter } = require('events');
const debug = require('debug')('egg-core:lifecycle');
const utils = require('./utils');

const INIT = Symbol('Lifycycle#init');
const INIT_READY = Symbol('Lifecycle#initReady');
const DELEGATE_READY_EVENT = Symbol('Lifecycle#delegateReadyEvent');
Expand All @@ -15,8 +17,6 @@ const IS_CLOSED = Symbol('Lifecycle#isClosed');
const BOOT_HOOKS = Symbol('Lifecycle#bootHooks');
const BOOTS = Symbol('Lifecycle#boots');

const utils = require('./utils');

class Lifecycle extends EventEmitter {

/**
Expand Down
1 change: 0 additions & 1 deletion lib/utils/timing.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const assert = require('assert');
const MAP = Symbol('Timing#map');
const LIST = Symbol('Timing#list');


class Timing {

constructor() {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"ci": {
"type": "github",
"version": "8, 10, 12, 14, 16",
"version": "8, 10, 12, 14, 16, 18",
"afterScript": "after_success:\n - npminstall codecov && codecov --disable=gcov",
"license": {
"year": 2016
Expand Down Expand Up @@ -73,7 +73,7 @@
"get-ready": "^2.0.1",
"globby": "^10.0.2",
"is-type-of": "^1.2.1",
"koa": "^2.7.0",
"koa": "^2.14.0",
"koa-convert": "^1.2.0",
"node-homedir": "^1.1.1",
"ready-callback": "^2.1.0",
Expand Down
28 changes: 28 additions & 0 deletions test/asyncLocalStorage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const assert = require('assert');
const path = require('path');
const request = require('supertest');
const EggApplication = require('./fixtures/egg').Application;

describe('test/asyncLocalStorage.test.js', () => {
let app;
before(() => {
app = new EggApplication({
baseDir: path.join(__dirname, 'fixtures/session-cache-app'),
type: 'application',
});
app.loader.loadAll();
});

it('should start app with asyncLocalStorage = true by default', async () => {
assert(app.currentContext === undefined);
const res = await request(app.callback())
.get('/');
assert(res.status === 200);
console.log(res.body);
assert(res.body.sessionId === 'mock-session-id-123');
assert(res.body.traceId);
assert(app.currentContext === undefined);
});
});
1 change: 1 addition & 0 deletions test/fixtures/egg/app/middleware/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = function() {
return (ctx, next) => {
ctx.traceId = `trace:${Date.now()}`;
if (ctx.path === '/status') {
ctx.body = 'egg status';
return;
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/egg/node_modules/session/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/session-cache-app/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async ctx => {
ctx.body = await ctx.app.sessionCache.getSessionById('mock-session-id-123');
};
3 changes: 3 additions & 0 deletions test/fixtures/session-cache-app/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = app => {
app.get('/', 'home');
};
3 changes: 3 additions & 0 deletions test/fixtures/session-cache-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "session-cache-app"
}

0 comments on commit 01201c6

Please sign in to comment.