go-resty / resty

Simple HTTP and REST client library for Go
MIT License
9.95k stars 704 forks source link

Why is ConnTime so long? #606

Open hoslo opened 1 year ago

hoslo commented 1 year ago
ufile download fileName: 67F0C1CA4548440EP08A07B6.jpg, trace: {DNSLookup:1.59315ms ConnTime:10.074076038s TCPConnTime:0s TLSHandshake:0s ServerTime:84.875535ms ResponseTime:20.539µs TotalTime:10.158972112s IsConnReused:true IsConnWasIdle:false ConnIdleTime:0s RequestAttempt:1 RemoteAddr:10.23.255.247:80}, status: 200
// define global client
defaultTransport := &http.Transport{
        Proxy:                 http.ProxyFromEnvironment,
        DialContext:           dialer.DialContext,
        ForceAttemptHTTP2:     true,
        MaxIdleConns:          100,
        IdleConnTimeout:       90 * time.Second,
        TLSHandshakeTimeout:   10 * time.Second,
        ExpectContinueTimeout: 1 * time.Second,
        MaxIdleConnsPerHost:   1000,
    }
uRfileClinet = resty.New()
uRfileClinet.SetTransport(defaultTransport)

// use in gin handler
ctx, cacel := context.WithTimeout(context.Background(), time.Second*20)
defer cacel()
uFileResp, err := uRfileClinet.R().
EnableTrace().
SetContext(ctx).
SetDoNotParseResponse(true).
Get(url)
log.LgReqAccess.Sugar().Infof("ufile_upstream_response_time: %s fileName: %s", time.Since(ufileUpstreamStartTime), fileName)
trace := uFileResp.Request.TraceInfo()
log.LgReqAccess.Sugar().Infof("ufile download fileName: %s, trace: %+v, status: %d", fileName, trace, uFileResp.StatusCode())
if err != nil {
  log.LgReqAccess.Sugar().Errorf("ufile download error: %+v, fileName: %s", err, fileName)
  statusCode = uFileResp.StatusCode()
  return
}
defer uFileResp.RawBody().Close()

_, err = io.Copy(c.Writer, uFileResp.RawBody())
moorereason commented 1 year ago

Could you be more specific? Does the connection not take 10s? Is the connection made quickly but it takes 10s to download the file (which should not make ConnTime long)? Can you try a tool such as httpstat to see what it shows? More details would be helpful.

jeevatkm commented 11 months ago

@hoslo, any data points for @moorereason queries?