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

Deglobal #59

Merged
merged 23 commits into from Jul 31, 2015
Merged

Deglobal #59

merged 23 commits into from Jul 31, 2015

Conversation

smashwilson
Copy link
Member

Stashing the "current" request and response as globals in RequestHelper and ResponseHelper is not a good idea. It works... as long a single presenter is never serving more than one request at once, in which case you'll very quickly start writing output to the wrong response object. I believe this is the cause of the sporadic "headers already sent" bug that we've been seeing this month (and now that I have some basic log aggregation going, I'm seeing the occasional 500 be served in production).

Essentially, this is a consequence of the way that node.js does asynchronous I/O with its event loop. Even though node is single-process and single-threaded, it achieves the throughput it does by interleaving other work while one request's handler is blocked waiting for upstream I/O to complete. This means that, while request A is waiting for a response from the content service, a request B that arrives before that response does will begin to be handled, and trash ResponseHelper._response for request A before request A has a chance to be completed.

I rigged up a simple demonstration of this happening by assigning IDs to each response object and firing a bunch of curl requests. Notice how every request tries to complete by writing to response 5, not its actual response object!

In its place, I've replaced the global service state with a Context object that knows the current request and response and can mediate some of the mechanics that were being handled by the RequestHelper and ResponseHelper methods, as well as the HttpErrorHelper.

smashwilson added a commit that referenced this pull request Jul 31, 2015
@smashwilson smashwilson merged commit 74a5e14 into deconst:master Jul 31, 2015
@smashwilson smashwilson deleted the context-object branch July 31, 2015 19:05
@ktbartholomew
Copy link
Contributor

😢

};

Context.prototype.protocol = function () {
return config.presented_url_proto() || this._request.protocol;
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like a leftover reference to the _request property

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh good catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants