Golang grpc server making http requests results in EOF and context deadline exceeded

47 Views Asked by At

I am relatively new to Golang and I'm facing an issue with a gRPC server program I've developed. In this program, upon receiving each gRPC request, I'm making an HTTP request to a third-party API. While this setup works for individual requests, I encounter errors when the server receives numerous concurrent requests.

The errors I'm encountering are mainly:

  1. [EOF]
  2. [context deadline exceeded (Client.Timeout exceeded while awaiting headers)]

I have a reusable HTTP client implemented as follows:

// http.go (utils package)

var client *http.Client
var clientMutex sync.Mutex

func CreateHttpClient() *http.Client {
    clientMutex.Lock()
    defer clientMutex.Unlock()

    var tlsConfig = &tls.Config{
        InsecureSkipVerify: true,
    }

    if client == nil {
        client = &http.Client{
            Transport: &http.Transport{
                MaxIdleConnsPerHost: 5,
                MaxIdleConns:        50,
                TLSClientConfig:     tlsConfig,
            },
            Timeout: time.Second * 10,
        }
    }
    return client
}

func init() {
    client = CreateHttpClient()
}

And I'm making POST requests using the following function:

// http.go (utils package)

func PostRequest(payload, url string) (*http.Response, error) {
    req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(payload)))
    if err != nil {
        log.Error().Msgf("error while posting request: %s", err.Error())
        return nil, err
    }
    return client.Do(req)
}

I call the PostRequest function from another file in my project:

// somefile.go (controllers package)

resp, err := utils.PostRequest(requestPayload, url)
if err != nil {
    // Handle error
}
defer resp.Body.Close()

I suspect the issue might be related to the keep-alive configuration of the HTTP client. However, I'm not entirely sure how to address this problem. Could someone experienced with gRPC and HTTP clients in Golang guide me on how to resolve these errors and provide insights into what might be causing them?

Thank you in advance for your help.

0

There are 0 best solutions below