2015-03-02 85 views

回答

1

AppEngine webapp中的处理程序的注册方式与普通的Go应用程序中的处理程序相同。您只需明确呼叫http.ListenAndServe()(因为它将在平台上),并且处理程序注册发生在init()函数中(不在main()中)。尽管如此,AppEngine也有相同的恐慌恢复包装,不幸的是没有其他更好的方法。

看看这个例子:它使用与Handle()登记处理2种网址模式HandlerHandleFunc()注册的功能,但都有意恐慌(他们拒绝服务):

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)。我们可以用我们首先注册一个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-protectedhttp://localhost:8080/myhandler-protected网址resuls在HTTP 200状态(确定)与消息:

Something went bad but I recovered and sent this! 
+0

由于某些原因,当我从我自己的恐慌中恢复时(比如你提供的例子),但是当一些谷歌包(如谷歌云存储[0]试图用无/不正确的上下文写入)恐慌程序不会恢复。任何想法为什么? http://godoc.org/google.golang.org/cloud/storage#NewReader – 2015-03-10 09:11:11

+0

@Theuserwithnohat上述保护机制只能从ServeHTTP()方法启动的恐慌中恢复。我猜你无法捕捉到的恐慌不是从ServeHttp()方法中调用,而是从其他goroutine(可能或不可能从ServeHTTP()方法中启动)中调用。 – icza 2015-03-10 09:33:45