如何使用gorilla mux和http.TimeoutHandler?

4
在使用Go编写的HTTP服务器中,我使用gorilla/mux进行路由。
我想使用http.TimeoutHandler(和/或其他“中间件”),但我不明白我该把它们放在哪里。

为了让它更清楚,我遵循以下步骤:
  1. 通过gorillaMux := mux.NewRouter()创建一个新的路由器。
  2. 通过类似于gorillaMux.HandleFunc("/", rootHandler)的调用添加我的路由。
  3. 通过server := &http.Server{Addr:":1234"}server.ListenAndServe()创建服务器。
我应该在哪里插入http.TimeoutHandler或任何其他中间件?
2个回答

11

以下是您可以实现此操作的方法:

package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "net/http"
    "time"
)

func rootHandler(w http.ResponseWriter, r *http.Request) {
    time.Sleep(5 * time.Second)
    fmt.Fprintf(w, "Hello!")
}

func main() {
    mux := mux.NewRouter()
    mux.HandleFunc("/", rootHandler)

    muxWithMiddlewares := http.TimeoutHandler(mux, time.Second*3, "Timeout!")

    http.ListenAndServe(":8080", muxWithMiddlewares)
}

如果您有多个HTTP处理程序,您可以将它们堆叠起来:

// this is quite synthetic and ugly example, but it illustrates how Handlers works
muxWithMiddlewares := http.StripPrefix("/api", http.TimeoutHandler(mux, time.Second*3, "Timeout!"))

1
请注意,这应用于HTTP请求的超时,但即使没有人在监听,运行 rootHandler 的 goroutine 最终仍会完成。 - zed
如果该端点有一个事务,我们该怎么回滚这个事务? - Samuel Ricky Saputro

2

这个解决方案没有使用TimeoutHandler。我在这里发布它,以防有人想要对他们的服务器超时进行精细控制。如果需要,这个替代方案将允许定义IdleTimeout和RequestHeaderTimeout。我在这个示例中同时使用了gorilla/mux和gorilla/handlers。希望能有所帮助。

// ReadTimeout is a timing constraint on the client http request imposed by the server from the moment
// of initial connection up to the time the entire request body has been read.
// [Accept] --> [TLS Handshake] --> [Request Headers] --> [Request Body] --> [Response]

// WriteTimeout is a time limit imposed on client connecting to the server via http from the
// time the server has completed reading the request header up to the time it has finished writing the response.
// [Accept] --> [TLS Handshake] --> [Request Headers] --> [Request Body] --> [Response]

func main() {
    mux := router.EpicMux()

    srv := &http.Server{
        Handler:      handlers.LoggingHandler(os.Stdout, mux),
        Addr:         "localhost:8080",
        WriteTimeout: 15 * time.Second,
        ReadTimeout:  15 * time.Second,
    }

    log.Fatal(srv.ListenAndServe())
}

func EpicMux() http.Handler {
    r := mux.NewRouter()
    r.HandleFunc("/", BaseURLRouter).Methods(http.MethodGet)
    // create the subroutes for v1 and v2
    v1 := r.PathPrefix("api/v1").Subrouter()
    // register handlers to appropriate version
    v1.HandleFunc("/person", PersonHandlerV1).Methods(http.MethodPost)

    v2 := r.PathPrefix("api/v2").Subrouter()
    v2.HandleFunc("/person",    PersonHandlerV2).Methods(http.MethodPost)
    return r
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接