Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't cache the intermediate locals for application #1146

Merged
merged 3 commits into from Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 4 additions & 8 deletions lib/application.js
Expand Up @@ -11,7 +11,6 @@ const { assign } = require('utility');
const KEYS = Symbol('Application#keys');
const HELPER = Symbol('Application#Helper');
const LOCALS = Symbol('Application#locals');
const LOCALS_LIST = Symbol('Application#localsList');
const BIND_EVENTS = Symbol('Application#bindEvents');
const WARN_CONFUSED_CONFIG = Symbol('Application#warnConfusedConfig');
const EGG_LOADER = Symbol.for('egg#loader');
Expand Down Expand Up @@ -76,18 +75,15 @@ class Application extends EggApplication {
if (!this[LOCALS]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个逻辑不能变化的。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是为什么呢

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看测试用例

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See.

this[LOCALS] = {};
}
if (this[LOCALS_LIST] && this[LOCALS_LIST].length) {
assign(this[LOCALS], this[LOCALS_LIST]);
this[LOCALS_LIST] = null;
}
return this[LOCALS];
}

set locals(val) {
if (!this[LOCALS_LIST]) {
this[LOCALS_LIST] = [];
if (!this[LOCALS]) {
this[LOCALS] = {};
}
this[LOCALS_LIST].push(val);

assign(this[LOCALS], val);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions test/app/extend/application.test.js
Expand Up @@ -77,6 +77,32 @@ describe('test/app/extend/application.test.js', () => {
.get('/app_same_ref')
.expect('true');
});

it('should app.locals not OOM', () => {
return app.httpRequest()
.get('/app_locals_oom')
.expect('ok');
});
});

describe('app.locals.foo = bar', () => {
let app;
before(() => {
app = utils.app('apps/app-locals-getter');
return app.ready();
});
after(() => app.close());

it('should work', () => {
return app.httpRequest()
.get('/test')
.expect({
locals: {
foo: 'bar',
abc: '123',
},
});
});
});

describe('app.createAnonymousContext()', () => {
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/apps/app-locals-getter/app/router.js
@@ -0,0 +1,11 @@
'use strict';

module.exports = app => {
app.get('/test', function* () {
this.app.locals.foo = 'bar';
this.locals.abc = '123';
this.body = {
locals: this.locals,
};
});
};
@@ -0,0 +1 @@
exports.keys = 'foo';
3 changes: 3 additions & 0 deletions test/fixtures/apps/app-locals-getter/package.json
@@ -0,0 +1,3 @@
{
"name": "app-locals-getter"
}
10 changes: 10 additions & 0 deletions test/fixtures/apps/locals/app/router.js
Expand Up @@ -14,6 +14,16 @@ module.exports = app => {
this.body = app1 === app2;
});

app.get('/app_locals_oom', function*() {
for (let i = 0; i < 1000; i++) {
app.locals = {
// 10MB
buff: Buffer.alloc(10 * 1024 * 1024).toString()
};
}
this.body = 'ok';
});

app.get('/ctx_same_ref', function*() {
let ctx1, ctx2;
this.locals = {
Expand Down