Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…rueblocks-core into develop
  • Loading branch information
tjayrush committed Sep 10, 2023
2 parents 02face7 + 3fa1f23 commit 7d7676c
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/apps/chifra/pkg/rpc/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"runtime"
"sync/atomic"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
Expand All @@ -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]
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7d7676c

Please sign in to comment.