go http request reader 内存不释放 大神来分析分析

            nbuf := make([]byte, 0, len(buf))
            num := copy(nbuf, buf) 
            fmt.Printf("-------copy num:%d\n", num) 
            if req, err = http.NewRequest("POST", uri, bytes.NewReader(nbuf)); err != nil {
                return
            }    

            for key, _ := range params {
                req.Header.Set(key, params.Get(key))
            }    
            req.Header.Set("Content-Type", "application/octet-stream")
            req.Header.Set("Content-Length", strconv.Itoa(len(buf)))
已邀请:

lwt

赞同来自:

nbuf 这个对象不释放

zensh - https://github.com/zensh

赞同来自:

你这代码也截得太省了,都不给个完整的上下文。 就这段代码而言, request 并没有开始运行,nbuf 当然不会释放

lwt

赞同来自:

问题已经很确定了 如果使用自定义Transport 内出就会出现不释放现象,即使使用defaulttrasnsport,如果client中注释了transport就ok了

    /*   
        _transport = &http.Transport{
            Dial: func(netw, addr string) (c net.Conn, err error) {
                if c, err = net.DialTimeout(netw, addr, 2*time.Second); err != nil {
                    return nil, err
                }
                return c, nil
            },
            DisableCompression: true,
        }
    */
    _transport = &http.Transport{
        DialContext: (&net.Dialer{
            Timeout:   30 * time.Second,
            KeepAlive: 30 * time.Second,
            DualStack: true,
        }).DialContext,
        MaxIdleConns:          100, 
        IdleConnTimeout:       90 * time.Second,
        TLSHandshakeTimeout:   10 * time.Second,
        ExpectContinueTimeout: 1 * time.Second,
    }    
    _client = &http.Client{
    //Transport: _transport,
    }    

要回复问题请先登录注册