github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/latency/tcp.go (about) 1 package latency 2 3 import ( 4 "context" 5 "fmt" 6 "net" 7 "net/http" 8 "time" 9 10 "github.com/Asutorufa/yuhaiin/pkg/net/netapi" 11 ) 12 13 func HTTP(p netapi.Proxy, target string) (time.Duration, error) { 14 tr := &http.Transport{ 15 DisableKeepAlives: true, 16 DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { 17 ad, err := netapi.ParseAddress(netapi.PaseNetwork(network), addr) 18 if err != nil { 19 return nil, fmt.Errorf("parse address failed: %w", err) 20 } 21 22 ctx, cancel := context.WithTimeout(ctx, timeout) 23 defer cancel() 24 25 return p.Conn(ctx, ad) 26 }, 27 } 28 defer tr.CloseIdleConnections() 29 30 start := time.Now() 31 resp, err := (&http.Client{Transport: tr, Timeout: timeout}).Get(target) 32 if err != nil { 33 return 0, err 34 } 35 defer resp.Body.Close() 36 37 return time.Since(start), nil 38 }