Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client probability returns an error about ErrConnectionClosed #1892

Open
halpers opened this issue Nov 1, 2024 · 4 comments
Open

Client probability returns an error about ErrConnectionClosed #1892

halpers opened this issue Nov 1, 2024 · 4 comments

Comments

@halpers
Copy link

halpers commented Nov 1, 2024

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

@halpers halpers changed the title the server closed connection before returning the first response byte. Make sure the server returns 'Connection: close' response header before closing the connection Client probability returns an error about ErrConnectionClosed Nov 1, 2024
@erikdubbelboer
Copy link
Collaborator

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

@halpers
Copy link
Author

halpers commented Nov 12, 2024

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
Copy link
Author

halpers commented Nov 21, 2024

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
Copy link
Contributor

byte0o commented Nov 21, 2024

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants