http ReverseProxy 如何设置超时时间

func ReverseProxy(host string) gin.HandlerFunc {
    return func(c *gin.Context) {
        director := func(req *http.Request) {
            req.URL.Scheme = "http"
            req.URL.Host = host
        }
        proxy := &httputil.ReverseProxy{Director: director}
        proxy.ServeHTTP(c.Writer, c.Request)
    }
}

请问在如何设置http的请求超时时间

已邀请:

lifei6671 - PHPer

赞同来自: Goho

 director := func(req *http.Request) {
            req.URL.Scheme = "http"
            req.URL.Host = host
        }
 proxy := &httputil.ReverseProxy{
            Director: director,
            Transport: &http.Transport{
                DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
                    return net.DialTimeout(network, addr, time.Second*30)
                },
            }
proxy.ServeHTTP(c.Writer, c.Request)

不应该是这么用吗?

要回复问题请先登录注册