15.5 确保网页应用健壮

当网页应用的处理函数发生 panic,服务器会简单地终止运行。这可不妙:网页服务器必须是足够健壮的程序,能够承受任何可能的突发问题。

首先能想到的是在每个处理函数中使用 defer/recover(),不过这样会产生太多的重复代码。13.5 节使用闭包的错误处理模式是更优雅的方案。我们把这种机制应用到前一章的简单网页服务器上。实际上,它可以被简单地应用到任何网页服务器程序中。

为增强代码可读性,我们为页面处理函数创建一个类型:

  1. type HandleFnc func(http.ResponseWriter, *http.Request)

我们的错误处理函数应用了 13.5 节的模式,变成了以下的 logPanics() 函数:

  1. func logPanics(function HandleFnc) HandleFnc {
  2. return func(writer http.ResponseWriter, request *http.Request) {
  3. defer func() {
  4. if x := recover(); x != nil {
  5. log.Printf("[%v] caught panic: %v", request.RemoteAddr, x)
  6. }
  7. }()
  8. function(writer, request)
  9. }
  10. }

然后我们用 logPanics() 来包装对处理函数的调用:

  1. http.HandleFunc("/test1", logPanics(SimpleServer))
  2. http.HandleFunc("/test2", logPanics(FormServer))

处理函数现在可以恢复 panic 调用,类似 13.5 节中的错误检测函数。完整代码如下:

示例 15.11 robust_webserver.go

  1. package main
  2. import (
  3. "io"
  4. "log"
  5. "net/http"
  6. )
  7. const form = `<html><body><form action="#" method="post" name="bar">
  8. <input type="text" name="in"/>
  9. <input type="submit" value="Submit"/>
  10. </form></html></body>`
  11. type HandleFnc func(http.ResponseWriter, *http.Request)
  12. /* handle a simple get request */
  13. func SimpleServer(w http.ResponseWriter, request *http.Request) {
  14. io.WriteString(w, "<h1>hello, world</h1>")
  15. }
  16. /* handle a form, both the GET which displays the form
  17. and the POST which processes it.*/
  18. func FormServer(w http.ResponseWriter, request *http.Request) {
  19. w.Header().Set("Content-Type", "text/html")
  20. switch request.Method {
  21. case "GET":
  22. /* display the form to the user */
  23. io.WriteString(w, form)
  24. case "POST":
  25. /* handle the form data, note that ParseForm must
  26. be called before we can extract form data*/
  27. //request.ParseForm();
  28. //io.WriteString(w, request.Form["in"][0])
  29. io.WriteString(w, request.FormValue("in"))
  30. }
  31. }
  32. func main() {
  33. http.HandleFunc("/test1", logPanics(SimpleServer))
  34. http.HandleFunc("/test2", logPanics(FormServer))
  35. if err := http.ListenAndServe(":8088", nil); err != nil {
  36. panic(err)
  37. }
  38. }
  39. func logPanics(function HandleFnc) HandleFnc {
  40. return func(writer http.ResponseWriter, request *http.Request) {
  41. defer func() {
  42. if x := recover(); x != nil {
  43. log.Printf("[%v] caught panic: %v", request.RemoteAddr, x)
  44. }
  45. }()
  46. function(writer, request)
  47. }
  48. }

链接