我想知道是否有可能从 panic 中恢复过来。看来GAE拥有自己的panic recovery机制,但我找不到在我的应用程序中处理它的任何钩子(Hook)。

最佳答案

AppEngine网络应用程序中的处理程序的注册方式与普通Go应用程序中的处理方式相同。您只是不必显式调用http.ListenAndServe()(因为它将由平台调用),并且处理程序注册发生在init()函数中(而不是main()中)。

话虽这么说,同样的紧急恢复包装也可以在AppEngine上使用,但是不幸的是,没有其他更好的方法了。

让我们看一下这个示例:它使用向 HandleFunc() 注册的函数和向 Handler 注册的 Handle() 处理2个URL模式,但都故意 panic (它们拒绝提供服务):

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/myfunchttp://localhost:8080/myhandler会导致HTTP 500状态:内部服务器错误(或空响应,具体取决于检查位置)。

总体思路是使用recover从处理程序(spec: Handling panics)中“捕获” panic 。我们可以通过以下方式“包装”句柄函数或处理程序:首先注册 defer 语句,即使其余函数感到 panic ,该语句也将被调用,并从 panic 状态中恢复。

请参阅以下两个功能:
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)
    })
}

第一个函数接受一个函数并返回一个函数,该函数调用我们传递的函数,但是如果一个函数已启动,则从紧急状态中恢复。

第二个使用Handler并返回另一个Handler,该Handler同样调用传递的http://localhost:8080/myfunc-protected,但也处理紧急情况并恢复正常执行。

现在,如果我们注册受这些方法保护的处理程序函数和http://localhost:8080/myhandler-protected,则注册的处理程序将永远不会崩溃(假设恢复正常执行后的代码不会崩溃):
http.HandleFunc("/myfunc-protected", protectFunc(myHandleFunc))
http.Handle("/myhandler-protected", protectHandler(new(MyHandler)))

以消息200的HTTP 200状态(OK)访问ojit_code和ojit_code URL结果:
Something went bad but I recovered and sent this!

关于google-app-engine - 是否有可能从Google App Engine的 panic 中恢复过来?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28815334/

10-12 03:03