在Golang中以X ms格式化后以编程方式关闭http连接

时间:2018-02-03 11:59:23

标签: http go timeout

我正在做X并行的http请求,当其中一个没有响应X ms(想象是100毫秒)或更少时,我想切断这个连接。我写的代码似乎不起作用,我如何切断连接并将响应作为零?

这是我的示例代码:

cx, cancel := context.WithCancel(context.Background())
ch := make(chan *HttpResponse)
var responses []*HttpResponse

timeout := 1.000 //1ms for testing purposes
var client = &http.Client{
    Timeout: 1 * time.Second,
}

startTime := time.Now()
for _, url := range urls {
    go func(url string) {
        fmt.Printf("Fetching %s \n", url)
        req, _ := http.NewRequest("POST", url, bytes.NewReader(request)) //request is json string
        req.WithContext(cx)
        resp, err := client.Do(req)
        ch <- &HttpResponse{url, resp, err}
        var timeElapsed = time.Since(startTime)
        msec := timeElapsed.Seconds() * float64(time.Second/time.Millisecond)
        if msec >= timeout {
            cancel()
        }
        if err != nil && resp != nil && resp.StatusCode == http.StatusOK {
            resp.Body.Close()
        }
    }(url)
}

for {
    select {
    case r := <-ch:
        fmt.Printf("%s was fetched\n", r.Url)
        if r.Err != nil {
            fmt.Println("with an error", r.Err)
        }
        responses = append(responses, r)
        if len(responses) == len(*feeds) {
            return responses
        }
    case <-time.After(100):
        //Do something
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码会等待请求完成(并获得一个resposne或错误),然后计算通过的时间,如果它超过预期的时间,您的代码将取消所有请求。

    req, _ := http.NewRequest("POST", url, bytes.NewReader(request)) //request is json string
    req.WithContext(cx) //Here you use a common cx, which all requests share.
    resp, err := client.Do(req) //Here the request is being sent and you wait it until done.
    ch <- &HttpResponse{url, resp, err}
    var timeElapsed = time.Since(startTime)
    msec := timeElapsed.Seconds() * float64(time.Second/time.Millisecond)
    if msec >= timeout {
        cancel() //here you cancel all the requests.
    }

修复方法是正确使用context包。

    req, _ := http.NewRequest("POST", url, bytes.NewReader(request)) //request is json string
    ctx,cancel := context.WithTimeout(request.Context(),time.Duration(timeout)*time.Millisecond)
    resp,err:=client.Do(req.WithContext(ctx))
    defer cancel()

有了这个,你将得到一个零resp(和一个错误),并在超时时获得连接切断。

相关问题