doucheng3811 2016-05-18 08:24
浏览 893

golang-为什么net.DialTimeout获得一半的超时时间?

This is my test code, I set 6s timeout, but the program execute only for 3s, Why?

package main

import "net"
import "time"
import "fmt"

func main() {
    fmt.Println(time.Now())
    conn, err := net.DialTimeout("tcp", "google.com:80",6*time.Second) // chinese people can't access google
    fmt.Println(time.Now())
    fmt.Println(conn,err)
}

Test result:

2016-05-18 16:21:31.325340213 +0800 CST
2016-05-18 16:21:34.32909193 +0800 CST
<nil> dial tcp 59.24.3.173:80: i/o timeout
  • 写回答

1条回答 默认 最新

  • doulin9679 2016-05-18 14:54
    关注

    Your code is OK, your network/internet connection/route to the internet has problem.
    For example if a router/device between you and google overloaded (or any problem...) it may drop some packets every some seconds, my guess.

    My test result:

    10 192.0109ms 3 &{{0xc082082900}} <nil>
    9 192.0109ms 3 &{{0xc082082780}} <nil>
    8 192.0109ms 3 &{{0xc082082000}} <nil>
    7 197.0112ms 3 &{{0xc082015c80}} <nil>
    6 227.0129ms 3 &{{0xc082082300}} <nil>
    5 372.0212ms 3 &{{0xc082082180}} <nil>
    4 375.0214ms 0 &{{0xc082015e00}} <nil>
    3 375.0214ms 3 &{{0xc082082600}} <nil>
    2 375.0214ms 3 &{{0xc082082480}} <nil>
    1 378.0216ms 3 &{{0xc082082a80}} <nil>
    

    then I disabled network connection:

    10 1.0000572s 0 <nil> dial tcp: i/o timeout
    9 1.0000572s 3 <nil> dial tcp: i/o timeout
    8 1.0000572s 3 <nil> dial tcp: i/o timeout
    7 1.0000572s 3 <nil> dial tcp: i/o timeout
    6 1.0000572s 4 <nil> dial tcp: i/o timeout
    5 1.0000572s 4 <nil> dial tcp: i/o timeout
    4 1.0000572s 4 <nil> dial tcp: i/o timeout
    3 1.0000572s 4 <nil> dial tcp: i/o timeout
    2 1.0000572s 4 <nil> dial tcp: i/o timeout
    1 1.0000572s 4 <nil> dial tcp: i/o timeout
    

    the Test Sample Code with 10 Concurrent tests:

    package main
    
    import (
        "fmt"
        "net"
        "time"
    )
    
    type res struct {
        d time.Duration
        t int64
        n net.Conn
        e error
    }
    
    func check(c chan res) {
        t := time.Now()
        conn, err := net.DialTimeout("tcp", "google.com:80", 1*time.Second)
        d := time.Now().Sub(t)
        c <- res{d, (t.UnixNano() - t0) / time.Millisecond.Nanoseconds(), conn, err}
    }
    
    var t0 int64 = time.Now().UnixNano()
    
    func main() {
        numberOfJobs := 10
        c := make(chan res, numberOfJobs)
        for i := 0; i < numberOfJobs; i++ {
            go check(c)
        }
        for r := range c {
            fmt.Println(numberOfJobs, r.d, r.t, r.n, r.e)
            numberOfJobs--
            if numberOfJobs == 0 {
                break
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?