use "Timer" instead of "time.After"

This commit is contained in:
patterniha 2025-04-30 03:02:00 +03:30
parent e702c9534f
commit d1fb3afba8

View File

@ -22,7 +22,7 @@ func TcpRaceDial(ctx context.Context, src net.Address, ips []net.IP, port net.Po
var resultCh = make(chan *result, len(ips)) var resultCh = make(chan *result, len(ips))
nextTryIndex := 0 nextTryIndex := 0
activeNum := uint32(0) activeNum := uint32(0)
waitTime := time.Duration(0) timer := time.NewTimer(0)
var winConn net.Conn var winConn net.Conn
for { for {
select { select {
@ -31,7 +31,7 @@ func TcpRaceDial(ctx context.Context, src net.Address, ips []net.IP, port net.Po
select { select {
case <-ctx.Done(): case <-ctx.Done():
cancel() cancel()
waitTime = 100 * time.Hour timer.Stop()
if winConn != nil { if winConn != nil {
winConn.Close() winConn.Close()
} }
@ -45,7 +45,7 @@ func TcpRaceDial(ctx context.Context, src net.Address, ips []net.IP, port net.Po
default: default:
if r.conn != nil { if r.conn != nil {
cancel() cancel()
waitTime = 100 * time.Hour timer.Stop()
if winConn == nil { if winConn == nil {
winConn = r.conn winConn = r.conn
} else { } else {
@ -59,17 +59,17 @@ func TcpRaceDial(ctx context.Context, src net.Address, ips []net.IP, port net.Po
continue continue
} }
if nextTryIndex < len(ips) { if nextTryIndex < len(ips) {
waitTime = time.Duration(0) timer.Reset(0)
continue continue
} }
if activeNum == 0 { if activeNum == 0 {
return nil, r.err return nil, r.err
} }
waitTime = 100 * time.Hour timer.Stop()
continue continue
} }
case <-time.After(waitTime): case <-timer.C:
if nextTryIndex == len(ips) || activeNum == sockopt.HappyEyeballs.MaxConcurrentTry { if nextTryIndex == len(ips) || activeNum == sockopt.HappyEyeballs.MaxConcurrentTry {
panic("impossible situation") panic("impossible situation")
} }
@ -77,9 +77,9 @@ func TcpRaceDial(ctx context.Context, src net.Address, ips []net.IP, port net.Po
activeNum++ activeNum++
nextTryIndex++ nextTryIndex++
if nextTryIndex == len(ips) || activeNum == sockopt.HappyEyeballs.MaxConcurrentTry { if nextTryIndex == len(ips) || activeNum == sockopt.HappyEyeballs.MaxConcurrentTry {
waitTime = 100 * time.Hour timer.Stop()
} else { } else {
waitTime = time.Duration(sockopt.HappyEyeballs.TryDelayMs) * time.Millisecond timer.Reset(time.Duration(sockopt.HappyEyeballs.TryDelayMs) * time.Millisecond)
} }
continue continue
} }