Golang net dial question


(Gemini Chan) #1

I write a tool to test conn time, but i see a lot of connections time is error.
i see a lot of connections time is greater than timemin, but i capture the packet and see it is right.
i don’t know where is error.
have anyone help me?
my code is

func main() {
var wg sync.WaitGroup
chV, chR := peashooter.Start(config.Config(), &wg)
wg.Wait()

}

func Start(config *config.GlobalConfig, wg *sync.WaitGroup) (chan time.Duration, chan time.Duration) {
vAddr := net.JoinHostPort(config.Vip, config.VipPort)
rAddr := net.JoinHostPort(config.Rip, config.RipPort)
chV := make(chan time.Duration, config.PacketCounter)
chR := make(chan time.Duration, config.PacketCounter)
chLimit := make(chan int, config.PackLimit)
timeout := time.Second * time.Duration(config.Timeout)
timemin := time.Second * time.Duration(config.TimeoutMin)
for i := 0; i < config.PacketCounter; i++ {
	wg.Add(1)
	go shooter(config.PacketType, vAddr, timeout, timemin, chLimit, chV, wg)
}

for i := 0; i < config.PacketCounter; i++ {
	wg.Add(1)
	go shooter(config.PacketType, rAddr, timeout, timemin, chLimit, chR, wg)
}
return chV, chR

}

func shooter(QType string, vAddr string, timeout time.Duration, timemin time.Duration, ChLimit chan int, ch chan time.Duration, wg *sync.WaitGroup) {
ChLimit <- 1
defer wg.Done()
start := time.Now()
conn, err := net.DialTimeout(QType, vAddr, timeout)
if err != nil {
	fmt.Println(err.Error())
	<-ChLimit
	return
}
defer conn.Close()
t := time.Now()
elapsed := t.Sub(start)
if elapsed.Seconds() > timemin.Seconds() {
	fmt.Println(conn.LocalAddr())
}
ch <- elapsed
<-ChLimit

}