我想知道是否有可能从恐慌中恢复过来。看来GAE有自己的panic recovery机制,但我找不到任何钩子来处理它在我的应用程序。是否有可能从谷歌应用程序引擎的恐慌中恢复?
回答
AppEngine webapp中的处理程序的注册方式与普通的Go应用程序中的处理程序相同。您只需明确呼叫http.ListenAndServe()
(因为它将在平台上),并且处理程序注册发生在init()
函数中(不在main()
中)。尽管如此,AppEngine也有相同的恐慌恢复包装,不幸的是没有其他更好的方法。
看看这个例子:它使用与Handle()
登记处理2种网址模式Handler
与HandleFunc()
注册的功能,但都有意恐慌(他们拒绝服务):
func myHandleFunc(w http.ResponseWriter, r *http.Request) {
panic("I'm myHandlerFunc and I refuse to serve!")
}
type MyHandler int
func (m *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
panic("I'm MyHandler and I refuse to serve!")
}
func main() {
http.HandleFunc("/myfunc", myHandleFunc)
http.Handle("/myhandler", new(MyHandler))
panic(http.ListenAndServe(":8080", nil))
}
指挥你浏览器到http://localhost:8080/myfunc
和http://localhost:8080/myhandler
导致HTTP 500状态:内部服务器错误(或根据您检查它的位置的空响应)。
总的想法是使用recover
来“处理”来自处理程序的恐慌(spec: Handling panics)。我们可以用我们首先注册一个defer
语句的方式来“包装”句柄函数或处理程序,即使该函数的其余部分发生混乱并且我们从panicing状态中恢复,该语句也会被调用。
见这两个函数:
func protectFunc(hf func(http.ResponseWriter,
*http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
r := recover()
if r != nil {
// hf() paniced, we just recovered from it.
// Handle error somehow, serve custom error page.
w.Write([]byte("Something went bad but I recovered and sent this!"))
}
}()
hf(w, r)
}
}
func protectHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
r := recover()
if r != nil {
// h.ServeHTTP() paniced, we just recovered from it.
// Handle error somehow, serve custom error page.
w.Write([]byte("Something went bad but I recovered and sent this!"))
}
}()
h.ServeHTTP(w, r)
})
}
第一个就是一个函数,并返回一个其卡列斯我们通过一个但如果一个已启动panicing状态中恢复。
第二个需要Handler
并返回另一个Handler
,它类似地调用传递的但还处理恐慌并恢复正常执行。
现在,如果我们登记处理功能和Handler
S按这些方法保护,注册处理程序将永远不会惊慌(假设代码恢复正常执行不慌后):
http.HandleFunc("/myfunc-protected", protectFunc(myHandleFunc))
http.Handle("/myhandler-protected", protectHandler(new(MyHandler)))
参观http://localhost:8080/myfunc-protected
和http://localhost:8080/myhandler-protected
网址resuls在HTTP 200状态(确定)与消息:
Something went bad but I recovered and sent this!
由于某些原因,当我从我自己的恐慌中恢复时(比如你提供的例子),但是当一些谷歌包(如谷歌云存储[0]试图用无/不正确的上下文写入)恐慌程序不会恢复。任何想法为什么? http://godoc.org/google.golang.org/cloud/storage#NewReader – 2015-03-10 09:11:11
@Theuserwithnohat上述保护机制只能从ServeHTTP()方法启动的恐慌中恢复。我猜你无法捕捉到的恐慌不是从ServeHttp()方法中调用,而是从其他goroutine(可能或不可能从ServeHTTP()方法中启动)中调用。 – icza 2015-03-10 09:33:45
- 1. 是否有可能在谷歌应用程序引擎
- 2. 谷歌应用程序引擎程序恢复
- 3. 从谷歌应用程序引擎
- 4. 延期功能是否可以恐慌,特别是当它已经恐慌?
- 5. 从谷歌应用引擎备份和恢复实体
- 6. 是否可以使用谷歌应用程序引擎中的pandoc?
- 7. WebTest的谷歌应用程序引擎
- 8. 谷歌应用程序引擎的Bigtable
- 9. 谷歌应用程序引擎的phpBB
- 10. Django的谷歌应用程序引擎
- 11. 谷歌应用程序引擎从应用程序隔离
- 12. 谷歌应用程序引擎应用程序的索引
- 13. 谷歌应用程序引擎_method?
- 14. 在谷歌应用程序引擎
- 15. 在谷歌应用程序引擎
- 16. Pinax谷歌应用程序引擎
- 17. 谷歌应用程序引擎将Appstats
- 18. 在谷歌应用程序引擎JAVA
- 19. 在cygwin谷歌应用程序引擎
- 20. UnicodeEncodeError谷歌应用程序引擎
- 21. richfaces 3.3.3谷歌应用程序引擎
- 22. pycurl与谷歌应用程序引擎
- 23. jquery谷歌应用程序引擎
- 24. 谷歌应用程序引擎蟒蛇
- 25. 谷歌应用程序引擎和Python
- 26. 在谷歌应用程序引擎SDK
- 27. 在谷歌应用程序引擎
- 28. 谷歌应用程序引擎
- 29. URLFetchService与谷歌应用程序引擎
- 30. 谷歌应用程序引擎getImageServingUrl与
怀疑你只需要在每个处理程序中恢复()(可能通过将它们包装在一个恐慌恢复功能中)。 (对不起,用GAE等不足以真正给你代码。) – twotwotwo 2015-03-02 17:15:44