#http.transport creating goroutines and not removing them upon client.do request

22 messages · Page 1 of 1 (latest)

supple ermine
#

as soon as i leave client empty its fine but with Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}, inside client upon a client.do request itll create a goroutine and proceed to not close said goroutine and so on every iteration i see an extra goroutine being created causing a memory leak

fluid silo
#

You need to set more. Timeouts should be set.

supple ermine
#

oh

#

ok

#

lemme try that

fluid silo
#

Don't leave any timeout attributes set to 0.

delicate perch
#

Particularly, you will need

    MaxIdleConns:          100,
    IdleConnTimeout:       90 * time.Second,

to clean up the idle connections

#

The other timeouts are necessary as well since they could get stuck in various different states and keep Goroutines alive

supple ermine
#

this will work fine right? ```go

    client := &http.Client{
        Transport: &http.Transport{
           Proxy: http.ProxyURL(proxyUrl),
            DialContext: (&net.Dialer{
                Timeout:   30 * time.Second,
                KeepAlive: 30 * time.Second,
                DualStack: true,
            }).DialContext,
            MaxIdleConns:        100,
            MaxIdleConnsPerHost: 100,
            IdleConnTimeout:     90 * time.Second,
            TLSHandshakeTimeout:   10 * time.Second,
       },
      }```
delicate perch
#

As a note,I believe the default MaxIdleConnsPerHost is 2, but yeah that should work

supple ermine
#

damn that worked

#

thx

#

i been tryna find the memory leak in my code for hours just to find that

#

thx

delicate perch
#

Thought there was going to be more too it as well, but I'm glad it's fixed! Sorry for the extra run into making a thread

supple ermine
#

ye i thought there would be more to it aswell ngl but ye thx

fluid silo
#

Might want to take a close look at the number of idle connections that you accept and how long you want to idle. You also want to think about the other end and what they have to face. 90 seconds of nothing is pretty long. So depending on the purpose and the protocol, you'd want to be more conservative about the values that you set.

supple ermine
#

it starts up goroutines without closing them like before except with all this new timeout stuff and only with the transport there

#

nvm all i had to do was add continueChallengeResponse.Body.Close()
to a request for some reason

#

ye that seems to have worked but ofc my memory leak still exists :,)

fluid silo
#

Yes, close the body when you're done with it. Even write your code so that it's a deferred call to the close method. This ensure that it will be closed even if the rest of the code causes a panic (which you should avoid).