From 156def6e7a4d816460bbfe168dcad1643de179d0 Mon Sep 17 00:00:00 2001 From: Dawid Szlachta Date: Tue, 5 Sep 2023 13:02:24 +0200 Subject: [PATCH] Overridden MaxIdleConnsPerHost to fix infinite bind --- src/apps/chifra/pkg/rpc/query/query.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/apps/chifra/pkg/rpc/query/query.go b/src/apps/chifra/pkg/rpc/query/query.go index 6c26fa8a23..cd059de983 100644 --- a/src/apps/chifra/pkg/rpc/query/query.go +++ b/src/apps/chifra/pkg/rpc/query/query.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "runtime" "sync/atomic" "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config" @@ -30,6 +31,16 @@ type eip1474Error struct { Message string `json:"message"` } +func init() { + // We need to increase MaxIdleConnsPerHost, otherwise chifra will keep trying to open too + // many ports. It can lead to bind errors. + // The default value is too low, so Go closes ports too fast. In the meantime, chifra tries + // to get new ones and so it can run out of available ports. + // + // We change DefaultTransport as the whole codebase uses it. + http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = runtime.GOMAXPROCS(0) * 4 +} + // Query returns a single result for given method and params. func Query[T any](chain string, method string, params Params) (*T, error) { var response rpcResponse[T] @@ -79,13 +90,7 @@ func FromRpc(rpcProvider string, payload *Payload, ret interface{}) error { func sendRpcRequest(rpcProvider string, marshalled []byte, result any) error { body := bytes.NewReader(marshalled) - req, err := http.NewRequest("POST", rpcProvider, body) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - resp, err := http.DefaultClient.Do(req) + resp, err := http.Post(rpcProvider, "application/json", body) if err != nil { return err }