valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.91k stars 1.76k forks source link

Client probability returns an error about ErrConnectionClosed #1892

Open halpers opened 3 weeks ago

halpers commented 3 weeks ago

It cost about 70ms from send msg to resp error:the server closed connection before returning the first response byte. Make sure the server returns 'Connection: close' response header before closing the connection.The code is as follows: c := fasthttp.Client{} c.TLSConfig = &tls.Config{ InsecureSkipVerify: true, } if err = c.DoTimeout(req, resp, 10*time.Second); err != nil { return } Server message packet: image

erikdubbelboer commented 2 weeks ago

I'm not sure I understand what the issue is here, can you post a reproducible example?

halpers commented 1 week ago

I'm not sure I understand what the issue is here, can you post a reproducible example?

client->ngnix->server. It happens probabilistically. When the number of concurrent requests is high, a small portion of the client requests will quickly (about 70ms)send an RST flag to Nginx.

halpers commented 2 days ago

I'm not sure I understand what the issue is here, can you post a reproducible example?

client->ngnix->server. It happens probabilistically. When the number of concurrent requests is high, a small portion of the client requests will quickly (about 70ms)send an RST flag to Nginx.

Please provide the minimum code set that can be verified When a request is received, start go routine to send the request to ngnix ` import ( "crypto/tls" "encoding/json" "github.com/valyala/fasthttp" "net/http" "time" )

func HttpPostJson(nginxUrl string, data interface{}) (respBody []byte, err error) { req := fasthttp.AcquireRequest() defer fasthttp.ReleaseRequest(req)

req.Header.SetMethod(http.MethodPost)
req.SetRequestURI(nginxUrl)

if data != nil {
    var body []byte
    req.Header.Set("Content-Type", "application/json")
    body, err = json.Marshal(data)
    if err != nil {
        return
    }
    req.SetBody(body)
}

resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)

c := fasthttp.Client{}
c.TLSConfig = &tls.Config{
    InsecureSkipVerify: true,
}
if err = c.DoTimeout(req, resp, 10*time.Second); err != nil {
    return
}
respBody = make([]byte, len(resp.Body()))
copy(respBody, resp.Body())

return

}

func ReceiveRequest(data interface{}) { go func() { HttpPostJson("http://nginxHost/test", data) }() } `

byte0o commented 2 days ago

我不确定我是否理解这里的问题,您可以发布一个可重现的例子吗?

client->ngnix->server,这个是概率性的,当并发请求数比较高的时候,一小部分客户端请求会很快(大概70ms)向Nginx发送RST标志。

请提供可验证的最小代码集 当收到请求时,启动 go 例程将请求发送到 ngnix ` import ( "crypto/tls" "encoding/json" "github.com/valyala/fasthttp" "net/http" "time" )

func HttpPostJson(nginxUrl string, 数据接口{}) (respBody []byte, err error) { req := fasthttp.AcquireRequest() defer fasthttp.ReleaseRequest(req)

req.Header.SetMethod(http.MethodPost)
req.SetRequestURI(nginxUrl)

if data != nil {
  var body []byte
  req.Header.Set("Content-Type", "application/json")
  body, err = json.Marshal(data)
  if err != nil {
      return
  }
  req.SetBody(body)
}

resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)

c := fasthttp.Client{}
c.TLSConfig = &tls.Config{
  InsecureSkipVerify: true,
}
if err = c.DoTimeout(req, resp, 10*time.Second); err != nil {
  return
}
respBody = make([]byte, len(resp.Body()))
copy(respBody, resp.Body())

return

}

func ReceiveRequest(data interface{}) { go func() { HttpPostJson(" http://nginxHost/test ", data) }() } `

Please post the server-side processing code and Nginx configuration together.