2008年7月25日 星期五

HTTP request solution in GAE

雖然 GAE 有提供 urlfetch 這個 API 達到 server side 的 HTTP request 功能,但是在仔細閱讀 GAE 的線上文件後,就不難發現這其實是個看到卻不好吃的功能,為什麼呢?以下這段但書節錄自 urlfetch API 的說明:

Since your application must respond to the user's request within several seconds, a URL fetch action to a slow remote server may cause your application to return a server error to the user. There is currently no way to specify a time limit to the URL fetch action.

從以上的敘述,不難發現 urlfetch 無法設定 timeout 值,任何耗時過長的 urlfetch 呼叫都會導致 GAE 回傳 HTTP 404 error 給使用者,又因為 Google 捨棄了很多 python runtime 功能以達到快速的 response time,包括無法在單一 request 中起始多個 thread,所以自然也沒有所謂的 callback function or method,呼叫 urlfetch 的 script 會一直處於等待回傳的狀態。

既然無法設定 timeout 且 GAE developer 又無法控制遠端服務的回傳時間,真不知 urlfetch API 有何實質用處?或許這是 Google 本身的陰謀:讓 developer 只使用 urlfetch 來取得 Google 本身的其他服務內容。畢竟,從 GAE 發出 HTTP request 給其他 Google Service 的回應時間絕對是最短的。

Well,解決辦法也不是沒有,就是將 HTTP request 從 server side 搬到 client side 去,在 web 開發漸漸朝向 AJAX 靠攏後,似乎也沒有必要在 server side 執行 HTTP request 了。下圖的紅色資料流即是 server side HTTP request 的資料流,綠色部分則是 Yu-Chao 所建議的 HTTP request 方式,亦即透過 client side javascrip 發出 HTTP request。



覺得寫 AJAX 很麻煩嗎?可是在 GAE 又無法使用 Microsoft AJAX.NET 這種傻瓜開發法(效能不佳),那 Yahoo 的 YUI library 倒不失為一好的解決方案。

沒有留言: