2 回答
TA贡献1806条经验 获得超8个赞
此解决方案不回答使用 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
}
- 2 回答
- 0 关注
- 239 浏览
添加回答
举报