TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Automatic Flushing: The Rails 3.1 Plan

46 点作者 ivey超过 14 年前

5 条评论

judofyr超过 14 年前
I’ve been doing some research for this earlier, and my conclusion was: This is <i>very hard</i>, if not impossible, to implement automatically. The main problem is that it’s impossible to handle exceptions correctly without making the whole stack aware of it.<p>Currently, when an exception occurs, the system can simply change the response (since the response hasn’t been sent to the client yet, but is only buffered inside the system). With this approach, a response can be in x different states: before flushing, after the 1st flushing, … and after the xth flushing. And after the 1st flushing, the status, headers and some content has been sent to the client.<p>Imagine that something raises an exception after the 1st flushing. Then a 200 status has already been sent, togeher with some headers and some content. First of all, the system has to make sure the HTML is valid and at least give the user some feedback. It’s not impossible, but still a quite hard problem (because ERB doesn’t give us any hint of where tags are open/closed). The system also need to take care of all the x different state and return correct HTML in all of them.<p>Another issue is that we’re actually sending an error page with a 200 status. This means that the response is cacheable with whatever caching rules you decied earlier in the controller (before you knew that an error will occur). Suddenly you have your 500.html cached all over the placed, at the client-side, in your reverse proxy and everywhere.<p>Let’s not forget that exceptions don’t always render the error page, but do other things as well. For instance, sometimes an exception is raised to tell the system that the user needs to be authenticated or doesn’t have permission to do something. These are often implemented as Rack middlewares, but with automatic flushing they <i>also</i> need to take care of each x states. And if it for instance needs to redirect the user, it can’t change the status/headers to a 302/Location if it’s already in the 1st state, and therefore needs to inject a &#60;script&#62;window.location=’foo’&#60;/script&#62; in a cacheable 200 response.<p>Of course, the views <i>shouldn’t</i> really raise any exceptions because it should be dumb. However, in Rails it’s very usual in Rails to defer the expensive method calls to the view. The controllers sets everything up, but it’s not until it needs to be rendered that it’s actually called. This increases the possibilty that an exception is raised in the rendering phrase.<p>Maybe I’m just not smart enough, but I just can’t come up with a way to tackle all of these problems (completely automated) without requiring any changes in the app.
评论 #1671569 未加载
评论 #1671545 未加载
Twisol超过 14 年前
So how does this work with Rack? Unless I'm mistaken, you have to return the body all at once, which entirely negates the benefits here. I don't see Rails mandating that an asynchronous server be used (i.e. Thin, Mongrel2, etc.), so I'm rather confused.
评论 #1671438 未加载
briandoll超过 14 年前
From Yehuda on twitter: "BTW: Those who have brought up issues with exceptions/status codes re: flushing, you're right, but it's not specific to the fiber solution"
zbanks超过 14 年前
Cool idea. It's one of those "cheap" speed boosts which are always nice to find/have.<p>It'd be nice to see this implemented in Django as well...
aaronblohowiak超过 14 年前
This encourages having SQL queries initiated by the view, after the header has rendered. This seems antithetical to MVC to me.
评论 #1670860 未加载
评论 #1670852 未加载
评论 #1671220 未加载