登录
首页 >  Golang >  Go问答

golang的hystrix库中未出现“超时”错误时的“电路断开”

来源:stackoverflow

时间:2024-02-22 22:27:27 111浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《golang的hystrix库中未出现“超时”错误时的“电路断开”》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我们在 golang 应用程序中使用 hystrix,即使没有 hystrix:超时,我们也会收到 hystrix:电路打开 错误

hystrix 配置:

hystrix.configurecommand(cmd, hystrix.commandconfig{
        timeout:               timeout,
        maxconcurrentrequests: 1000,
        errorpercentthreshold: 5,
        sleepwindow:           15 * 60 * 1000, //15 minutes
    })

在hystrix后备部分,我们记录错误消息信息。我们可以清楚地看到,我们只有 hystrix:电路开路 错误,没有任何其他错误

有时它的行为非常随机,在下图中我们可以看到hystrix:超时hystrix:电路开路之间没有相关性

sudo/示例 hystrix 代码:

func crawl(vendor string, req *http.Request, timeout int) (result []byte) {

    hystrix.Do(vendor, func() error {

        resp, err := httpClient.Do(req)
        if err != nil {
            log.Errorln("Error sending post request: ", err)
        } else {
            defer resp.Body.Close()
        }
        respBody, errResp := ioutil.ReadAll(resp.Body)
        if errResp != nil {
            log.WithFields(log.Fields{"RequestID": requestID}).Errorln("Error reading parser response", errResp, resp.Status)
        }

        if resp.StatusCode == 200 {
            result = respBody
        } else {
            log.Errorln(" SERVER SIDE ERROR", resp.StatusCode, obj)
        }

        return nil
    }, func(err error) error {
        logApiTimeouts(vendor, err)
        log.WithFields(log.Fields{"RequestID": requestID}).Errorln("Hystrix Error:", err, obj)
        return nil
    })
}

有人遇到过这个错误吗?如何解决这个问题?


解决方案


可能是因为 requestvolumethreshold 默认设置为 20。

根据 hystrix golang 客户端的文档,

// defaultvolumethreshold is the minimum number of requests needed before a circuit can be tripped due to health
defaultvolumethreshold = 20

您可以设置它,

// Settings returns the hystrix command config
func (c Config) Settings() hystrix.CommandConfig {
   return hystrix.CommandConfig{
      Timeout:                8000,
      RequestVolumeThreshold: 1000, // FIXME: all calls must be paginated
   }
}

将其调整为大于默认值的特定数字修复了我的错误。

到这里,我们也就讲完了《golang的hystrix库中未出现“超时”错误时的“电路断开”》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

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