Skip to content

Commit e1ff721

Browse files
authored
1 parent 4275314 commit e1ff721

3 files changed

Lines changed: 9 additions & 9 deletions

File tree

lib/egg.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const assert = require('assert');
2-
const { AsyncLocalStorage } = require('async_hooks');
32
const fs = require('fs');
43
const KoaApplication = require('koa');
54
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
65
const debug = require('debug')('egg-core');
76
const is = require('is-type-of');
87
const co = require('co');
98
const Router = require('@eggjs/router').EggRouter;
9+
const { getAsyncLocalStorage } = require('gals');
1010
const BaseContextClass = require('./utils/base_context_class');
1111
const utils = require('./utils');
1212
const Timing = require('./utils/timing');
@@ -16,7 +16,6 @@ const DEPRECATE = Symbol('EggCore#deprecate');
1616
const ROUTER = Symbol('EggCore#router');
1717
const EGG_LOADER = Symbol.for('egg#loader');
1818
const CLOSE_PROMISE = Symbol('EggCore#closePromise');
19-
const EGG_CTX_STORAGE = Symbol.for('egg#ctxStorage');
2019

2120
class EggCore extends KoaApplication {
2221

@@ -40,10 +39,7 @@ class EggCore extends KoaApplication {
4039
// disable koa als and use egg logic
4140
super({ asyncLocalStorage: false });
4241
// can access the AsyncLocalStorage instance in global
43-
if (!global[EGG_CTX_STORAGE]) {
44-
global[EGG_CTX_STORAGE] = new AsyncLocalStorage();
45-
}
46-
this.ctxStorage = global[EGG_CTX_STORAGE];
42+
this.ctxStorage = getAsyncLocalStorage();
4743

4844
this.timing = new Timing();
4945

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"egg-logger": "^3.1.0",
6666
"egg-path-matching": "^1.0.1",
6767
"extend2": "^1.0.0",
68+
"gals": "^1.0.1",
6869
"get-ready": "^2.0.1",
6970
"globby": "^11.0.2",
7071
"is-type-of": "^1.2.1",

test/asyncLocalStorage.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const assert = require('assert');
22
const path = require('path');
33
const { AsyncLocalStorage } = require('async_hooks');
44
const request = require('supertest');
5+
const { getAsyncLocalStorage, kGALS } = require('gals');
56
const EggApplication = require('./fixtures/egg').Application;
67

78
describe('test/asyncLocalStorage.test.js', () => {
@@ -26,8 +27,10 @@ describe('test/asyncLocalStorage.test.js', () => {
2627
});
2728

2829
it('should access als on global', async () => {
29-
assert(global[Symbol.for('egg#ctxStorage')]);
30-
assert(global[Symbol.for('egg#ctxStorage')] instanceof AsyncLocalStorage);
31-
assert.equal(app.ctxStorage, global[Symbol.for('egg#ctxStorage')]);
30+
assert(global[Symbol.for('gals#asyncLocalStorage')]);
31+
assert(global[kGALS]);
32+
assert(global[Symbol.for('gals#asyncLocalStorage')] instanceof AsyncLocalStorage);
33+
assert.equal(app.ctxStorage, global[Symbol.for('gals#asyncLocalStorage')]);
34+
assert.equal(app.ctxStorage, getAsyncLocalStorage());
3235
});
3336
});

0 commit comments

Comments
 (0)