From a04fd8642ecc165bbca84659cde6ce3869a9fe3d Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Fri, 2 Mar 2018 13:54:21 +0100 Subject: [PATCH] Use custom http transport settings in bitcoind rpc --- bchain/bitcoinrpc.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/bchain/bitcoinrpc.go b/bchain/bitcoinrpc.go index 28136ed9..d83f8a81 100644 --- a/bchain/bitcoinrpc.go +++ b/bchain/bitcoinrpc.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "io/ioutil" + "net" "net/http" "time" @@ -180,17 +181,13 @@ type BitcoinRPC struct { // NewBitcoinRPC returns new BitcoinRPC instance. func NewBitcoinRPC(url string, user string, password string, timeout time.Duration) *BitcoinRPC { - // set higher MaxIdleConnsPerHost to not to deplete pool of sockets - defaultTransportPointer, ok := http.DefaultTransport.(*http.Transport) - if !ok { - glog.Fatal("http.DefaultTransport is not an *http.Transport") + transport := &http.Transport{ + Dial: (&net.Dialer{KeepAlive: 600 * time.Second}).Dial, + MaxIdleConns: 100, + MaxIdleConnsPerHost: 100, // necessary to not to deplete ports } - // dereference it to get a copy of the struct that the pointer points to - defaultTransport := *defaultTransportPointer - defaultTransport.MaxIdleConns = 100 - defaultTransport.MaxIdleConnsPerHost = 100 return &BitcoinRPC{ - client: http.Client{Timeout: timeout, Transport: &defaultTransport}, + client: http.Client{Timeout: timeout, Transport: transport}, URL: url, User: user, Password: password,