登录
首页 >  Golang >  Go问答

达到文件打开限制:使用 golang 进行基准测试时遇到的错误套接字

来源:stackoverflow

时间:2024-03-22 18:00:35 382浏览 收藏

在对使用 REST API 的系统进行基准测试时,出现了“打开文件太多”错误。该错误源于使用 RESTy HTTP 客户端,该客户端在进行大量请求时达到文件打开限制。为了解决此问题,需要增加操作系统允许打开的文件描述符的最大数量。具体来说,可以编辑 /etc/security/limits.conf 文件,将 nofile 值增加到一个更大的数字,例如 1048576 或更高。

问题内容

我正在构建一个系统,在该系统中我使用 rest(http) 和 grpc 实现了 api(主要是在 golang 中),以便比较这两种替代方案作为研究。但是,当我对其余 api 进行基准测试时,我得到了 socket:打开文件太多错误。使用 resty http 客户端:

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
}

完整代码: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


正确答案


对于基准测试,您可能需要增加打开文件描述符的最大数量。

尝试编辑此文件 /etc/security/limits.conf 并更新以下行:

*         hard    nofile      1048576
*         soft    nofile      1048576

如果不够,您可以更改为更大的数字。

今天关于《达到文件打开限制:使用 golang 进行基准测试时遇到的错误套接字》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>