错误:net / http:等待连接时请求被取消(等待头时超出了Client.Timeout)

时间:2019-10-30 05:42:19

标签: go

大约3〜4分钟,我的日志中会出现一些错误。

net / http:在等待连接时被取消请求(在等待标头时超过了Client.Timeout)

我尝试使用httptrace找出花费时间的地方。 httptrace.GetConn httptrace.GotConn

我认为在httptrace.GotConn之前时间已用完。 所以发生了错误 等待连接时请求已取消

我的机器正常,这是我的netstat。

LAST_ACK 2 CLOSE_WAIT 7 成立108 SYN_SENT 3 TIME_WAIT 43

package main

import (
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "io/ioutil"
    "math/rand"
    "net"
    "net/http"
    "net/http/httptrace"
    "os"
    "sync"
    "time"
)

var Client *http.Client = &http.Client{
    Transport: &http.Transport{
        DisableKeepAlives:true,
        Proxy: http.ProxyFromEnvironment,
        DialContext: (&net.Dialer{
            Timeout:   3 * time.Second, // 连接超时
            KeepAlive: 10 * time.Second,
            DualStack: true,
        }).DialContext,
        IdleConnTimeout:       120 * time.Second,
        ResponseHeaderTimeout: 60 * time.Second,
        ExpectContinueTimeout: 1 * time.Second,
    },
    Timeout: 500 * time.Millisecond,
}

func GenLogId() string {
    h2 := md5.New()
    rand.Seed(time.Now().Unix())
    str := fmt.Sprintf("%d%d%d", os.Getpid(), time.Now().UnixNano(), rand.Int())

    h2.Write([]byte(str))
    uniqid := hex.EncodeToString(h2.Sum(nil))

    return uniqid
}

func main() {
    var (
        wg           sync.WaitGroup
        maxParallel  int       = 50
        parallelChan chan bool = make(chan bool, maxParallel)
    )
    for {
        parallelChan <- true
        wg.Add(1)
        go func() {
            defer func() {
                wg.Done()
                <-parallelChan
            }()
            testHttp2()
        }()
    }
    wg.Wait()
}

func testHttp2() {
    url := "http://10.33.108.39:11222/index.php"
    req, _ := http.NewRequest("GET", url, nil)

    uniqId := GenLogId()
    trace := &httptrace.ClientTrace{
        GetConn: func(hostPort string) {
            fmt.Println("GetConn id:", uniqId, time.Now().UnixNano(), hostPort)
        },
        GotConn: func(connInfo httptrace.GotConnInfo) {
            fmt.Println("GotConn id:", uniqId, time.Now().UnixNano(), connInfo.Conn.LocalAddr())
        },

        ConnectStart: func(network, addr string) {
            fmt.Println("ConnectStart id:", uniqId, time.Now().UnixNano(), network, addr)
        },
        ConnectDone: func(network, addr string, err error) {
            fmt.Println("ConnectDone id:", uniqId, time.Now().UnixNano(), network, addr, err)
        },
    }
    req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
    resp, err := Client.Do(req)
    if err != nil {
        fmt.Println("err: id", uniqId, time.Now().UnixNano(), err)
        return
    }

    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)

    if err != nil {
        fmt.Println("error", string(body))
    }
    return
}

您可以使用我的代码进行复制。我对这个错误感到困惑... 谢谢。

1 个答案:

答案 0 :(得分:1)

您需要增加测试的客户端Timeout值。

  

net/http: request canceled (Client.Timeout exceeded while awaiting headers)

这意味着您的Client.Timeout值小于服务器响应时间,原因有很多原因(例如,忙碌,CPU过载,此处每秒生成的许多请求等)。

下面是解释它并重新生成它的简单方法:

运行此服务器(等待2 * time.Second然后发回响应):

package main

import (
    "io"
    "log"
    "net/http"
    "time"
)

func main() {
    http.HandleFunc(`/`, func(w http.ResponseWriter, r *http.Request) {
        log.Println("wait a couple of seconds ...")
        time.Sleep(2 * time.Second)
        io.WriteString(w, `Hi`)
        log.Println("Done.")
    })
    log.Println(http.ListenAndServe(":8080", nil))
}

然后运行此客户端,该客户端在1 * time.Second中超时:

package main

import (
    "io/ioutil"
    "log"
    "net/http"
    "time"
)

func main() {
    log.Println("HTTP GET")
    client := &http.Client{
        Timeout: 1 * time.Second,
    }
    r, err := client.Get(`http://127.0.0.1:8080/`)
    if err != nil {
        log.Fatal(err)
    }
    defer r.Body.Close()
    bs, err := ioutil.ReadAll(r.Body)
    if err != nil {
        log.Fatal(err)
    }
    log.Println("HTTP Done.")
    log.Println(string(bs))
}

输出为({Client.Timeout exceeded while awaiting headers):

2019/10/30 11:05:08 HTTP GET
2019/10/30 11:05:09 Get http://127.0.0.1:8080/: net/http: request canceled (Client.Timeout exceeded while awaiting headers)
exit status 1

注意:
您需要相应地更改这两个设置(http.Transport.ResponseHeaderTimeouthttp.Client.Timeout)。

您已将ResponseHeaderTimeout: 60 * time.Second,的{​​{1}}设置为半秒。