Fix possible memory leak in bitcoind rpc call
This commit is contained in:
parent
1c68070d78
commit
1eb77fbc27
@ -5,6 +5,8 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -466,9 +468,16 @@ func (b *BitcoinRPC) call(req interface{}, res interface{}) error {
|
|||||||
}
|
}
|
||||||
httpReq.SetBasicAuth(b.User, b.Password)
|
httpReq.SetBasicAuth(b.User, b.Password)
|
||||||
httpRes, err := b.client.Do(httpReq)
|
httpRes, err := b.client.Do(httpReq)
|
||||||
|
// in some cases the httpRes can contain data even if it returns error
|
||||||
|
// see http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/
|
||||||
|
if httpRes != nil {
|
||||||
|
defer httpRes.Body.Close()
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer httpRes.Body.Close()
|
// read the entire response body until the end to avoid memory leak when reusing http connection
|
||||||
|
// see http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/
|
||||||
|
defer io.Copy(ioutil.Discard, httpRes.Body)
|
||||||
return json.NewDecoder(httpRes.Body).Decode(&res)
|
return json.NewDecoder(httpRes.Body).Decode(&res)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user