golang - 从任何地方退出 http 处理程序

我正在使用 net/http 包并想知道如何从代码中的任何位置退出处理程序。说我有这段代码:


func main() {

    http.HandleFunc("/", handler)

    http.ListenAndServe(":8080", nil)

}


func handler(w http.ResponseWriter, r *http.Request){

    err := checkSomeThing(w, r)


    if err != nil {

        return

    }


    fmt.Println("End of Handler.")

    return

}


func checkSomeThing(w http.ResponseWriter, r *http.Request) error{

    http.Error(w, "Bad Request!", http.StatusBadRequest) 

    

    return errors.New("bad request")

}

理想情况下,我想从 checkSomeThing 函数中退出处理程序,而不必返回然后再向上返回一个级别,随着应用程序的增长,这会变得更糟。这纯粹是为了代码可读性。


aluckdog
浏览 116回答 3
3回答

江户川乱折腾

惯用的方法是检查错误返回调用链。要从任何地方退出处理程序,请按照 encoding/json 包中的模式使用 panic 和 recover 。为恐慌定义一个独特的类型:type httpError struct {    status  int    message string}编写要在 defer 语句中使用的函数。该函数检查类型并适当地处理错误。否则,函数会继续恐慌。func handleExit(w http.ResponseWriter) {    if r := recover(); r != nil {        if he, ok := r.(httpError); ok {            http.Error(w, he.message, he.status)        } else {            panic(r)        }    }}为调用 panic 编写一个辅助函数:func exit(status int, message string) {    panic(httpError{status: status, message: message})}使用这样的功能:func example() {   exit(http.StatusBadRequest, "Bad!")}func someHandler(w http.ResponseWriter, r *http.Request) {   defer handleExit(w)   example()}

慕桂英4014372

我的回答: 首先,在 Golang 中建立的通用模式是让错误作为返回值从被调用者“冒泡”回到调用者。它在可读性和可重用性方面具有很多优势。副作用是有很多if err != nil {return}支票。如果你真的想打破常规,我的建议我要提出一个想法,我认为它在 golang 编码风格和模式方面并不常见或不标准。但我没有在网上看到任何暗示这是灾难性的事情。让我们看看我在评论中说这太糟糕了。您可以使用runtime.Goexit()来实现您想要的。处理程序只是等待另一个 goroutine 来完成工作。如果 go-routine 中运行的内部代码想要中止处理,它可以调用 Goexit()。它的优点是所有defer语句仍将执行。这看起来像是 Golang 目前不支持的异常处理的弱版本。但我要把它扔出去。func handler(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; var cleanExit bool = false&nbsp; &nbsp; var ch = make(chan bool)&nbsp; &nbsp; // the actual handler implementation in a goroutine&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; defer close(ch)&nbsp; &nbsp; &nbsp; &nbsp; handlerImpl(w, r)&nbsp; &nbsp; &nbsp; &nbsp; cleanExit = true // if handlerImpl invokes goExit, this line doesn't execute&nbsp; &nbsp; }()&nbsp; &nbsp; // wait for goroutine to exit&nbsp; &nbsp; <-ch&nbsp; &nbsp; if cleanExit {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Handler exited normally")&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Hanlder was aborted")&nbsp; &nbsp; }}func handlerImpl(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; checkSomeThing(w, r)}func checkSomeThing(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; http.Error(w, "Bad Request!", http.StatusBadRequest)&nbsp; &nbsp; runtime.Goexit()}

呼如林

如果checkSomeThing()特定于该路线,您可能应该继续使用您粘贴的代码示例。如果checkSomeThing()是所有路由(或路由子集)共有的函数,您可以选择一种方式在调用特定路由的处理程序之前运行中间件。例如,请参阅此答案或此答案,或者这是一种仅使用标准 http 包中的代码来完成此操作的方法:func checkSomething(...) error {&nbsp; &nbsp; ...}func WrapWithCheck(handler http.Handler) http.Handler {&nbsp; &nbsp; return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; err := checkSomething(w, req)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; handler.ServeHTTP(w, req)&nbsp; &nbsp; })}func setupRouter() http.Handler {&nbsp; &nbsp; mux := http.NewServeMux()&nbsp; &nbsp; mux.HandleFunc("/foo/", handleFoo)&nbsp; &nbsp; mux.HandleFunc("/bar/", handleBar)&nbsp; &nbsp; mux.HandleFunc("/baz/", handleBaz)&nbsp; &nbsp; // add your common call to 'checkSomething' here :&nbsp; &nbsp; handler := WrapWithCheck(mux)&nbsp; &nbsp; return handler}注意:我尝试httptest在上面的操场上使用,但由于某种原因它在操场上陷入僵局。sample.go如果您将此代码复制/粘贴到一个文件中并使用,它会很好地工作go run sample.go
打开App,查看更多内容
随时随地看视频慕课网APP