go-resty / resty

Simple HTTP and REST client library for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

It seems memory leaks with post

youzeliang opened this issue · comments

In most cases, I use the resty library's Post method to send POST requests, and everything works fine with no issues. However, in a particular scenario, when I receive a large response structure, I encounter a memory leak. Firstly, I want to clarify that my code doesn't have any goroutine leaks or lingering global variables (i.e., no delayed releases). Upon using pprof to inspect, it seems that the underlying io.readAll operation is consuming a significant amount of memory. Are there considerations for optimizing this part? I will attach my pprof results and some key information along with the crucial parts of the code.

profile001

image

resty.New(). OnBeforeRequest(func(client *resty.Client, r *resty.Request) error { return nil }). SetRetryCount(2). SetTimeout(time.Second * 8). R(). SetFormData(apiBody). SetResult(&apiRes). Post(url)

the response apiRes is about 4k

@youzeliang Thanks for reaching out. I'm currently on vacation days; I will be back in January.

I see that code in resty/v2:

func closeq(v interface{}) {
	if c, ok := v.(io.Closer); ok {
		silently(c.Close())
	}
}

func silently(_ ...interface{}) {}

See client.go#12000

@youzeliang Thanks for reaching out. Based on the above details. Currently the method R().SetFormData reads all the form data values into memory to create Request body for the request. That's why memory increases.

In the v3, there are plans to optimize the flow.