Home > OS >  Error socket: too many open files while benchmarking api in golang
Error socket: too many open files while benchmarking api in golang

Time:07-02

I'm building a system where I have implemented APIs with rest(HTTP) and gRPC - mostly in golang - in order to compare both alternatives as a study. But, when I'm benchmarking the rest APIs i'm getting a socket: too many open files error. using the resty htttp client:

enter image description here

package httpcardpayment

import (
    "encoding/json"
    "errors"
    "log"
    "net/http"
    "time"

    "github.com/bmviniciuss/gateway/src/core/card_payment"
    "github.com/go-resty/resty/v2"
)

type HttpCardPaymentService struct {
    Client *resty.Client
}

func NewHttpCardPaymentService() *HttpCardPaymentService {
    return &HttpCardPaymentService{
        Client: resty.New().SetTimeout(5 * time.Second),
    }
}

type CardPaymentRequest struct {
    ClientId    string                 `json:"client_id"`
    Amount      float64                `json:"amount"`
    PaymentType string                 `json:"payment_type"`
    PaymentDate string                 `json:"payment_date"`
    PaymentInfo CardPaymentRequestInfo `json:"payment_info"`
}

type CardPaymentRequestInfo struct {
    CardToken string `json:"card_token"`
}

type CardPaymentResult struct {
    Id          string          `json:"id"`
    ClientId    string          `json:"client_id"`
    Amount      float64         `json:"amount"`
    PaymentType string          `json:"payment_type"`
    PaymentDate string          `json:"payment_date"`
    PaymentInfo CardPaymentInfo `json:"payment_info"`
}

type CardPaymentInfo struct {
    MaskedNumber string `json:"masked_number"`
}

func (h *HttpCardPaymentService) CreatePayment(payment *card_payment.CardPayment) error {
    result := &CardPaymentResult{}

    b := &CardPaymentRequest{
        ClientId:    payment.ClientId,
        Amount:      payment.Amount,
        PaymentType: payment.PaymentType,
        PaymentDate: payment.PaymentDate,
        PaymentInfo: CardPaymentRequestInfo{
            CardToken: payment.PaymentInfo.CardToken,
        },
    }

    log.Printf("Calling HTTP Card Payment Microsservice with % v\n", b)

    res, err := h.Client.R().
        SetHeader("Content-Type", "application/json").
        SetHeader("Connection", "close").
        SetBody(b).
        Post("http://0.0.0.0:5002/api/payment")

    if err != nil {
        log.Println("Error in request to process new card payment", err)
        return errors.New("An error occur while creating the card payment")
    }

    err = json.Unmarshal(res.Body(), &result)

    if err != nil {
        log.Println("Error while parsing request body")
        return errors.New("An error occur while creating the card payment")
    }

    if res.StatusCode() != http.StatusCreated {
        log.Println("The response was not expected", res.StatusCode(), string(res.Body()))
        return errors.New("An error occur while creating the card payment")
    }

    log.Println("Card payment created")
    payment.Id = result.Id
    payment.PaymentInfo.MaskedNumber = result.PaymentInfo.MaskedNumber

    return nil
}

Full Code: https://github.com/bmviniciuss/tcc/tree/bmviniciuss/devs-48 https://github.com/bmviniciuss/tcc/blob/bmviniciuss/devs-48/gateway-go/src/adapters/card_payment/http/htpp_card_payment_service.go

CodePudding user response:

As for benchmarking, you might need to increase maximum number of open file descriptors.

Try to edit this file /etc/security/limits.conf and update following lines:

*         hard    nofile      1048576
*         soft    nofile      1048576

You can change to a greater number if it's not enough.

  •  Tags:  
  • go
  • Related