zoukankan      html  css  js  c++  java
  • 22.熔断器学习,控制最大并发数目

    熔断器控制最大并发数

    package main
    
    import (
        "fmt"
        "github.com/afex/hystrix-go/hystrix"
        "math/rand"
        "sync"
        "time"
    )
    
    type Product struct {
        ID    int
        Title string
        Price int
    }
    
    func getProduct() (Product, error) {
        r := rand.Intn(10)
        if r < 6 { //模拟api卡顿和超时效果
            //time.Sleep(time.Second * 4)
        }
        return Product{
            ID:    101,
            Title: "Golang从入门到精通",
            Price: 12,
        }, nil
    }
    
    func RecProduct() (Product, error) {
        return Product{
            ID:    999,
            Title: "推荐商品",
            Price: 120,
        }, nil
    
    }
    
    func main() {
        rand.Seed(time.Now().UnixNano())
        configA := hystrix.CommandConfig{ //创建一个hystrix的config
            Timeout:               3000, //command运行超过3秒就会报超时错误
            MaxConcurrentRequests: 5,    //控制最大并发数为5,如果超过5会调用我们传入的回调函数降级
        }
        hystrix.ConfigureCommand("get_prod", configA) //hystrix绑定command
        resultChan := make(chan Product, 1)
    
        wg := &sync.WaitGroup{}
    
        for i := 0; i < 10; i++ {
            go (func() {
                wg.Add(1)
                defer wg.Done()
                errs := hystrix.Go("get_prod", func() error { //使用hystrix来讲我们的操作封装成command,hystrix返回值是一个chan error
                    p, _ := getProduct() //这里会随机延迟0-4秒
                    resultChan <- p
                    return nil //这里返回的error在回调中可以获取到,也就是下面的e变量
                }, func(e error) error {
                    rcp, err := RecProduct() //推荐商品,如果这里的err不是nil,那么就会忘errs中写入这个err,下面的select就可以监控到
                    resultChan <- rcp
                    return err
                })
                select {
                case getProd := <-resultChan:
                    fmt.Println(getProd)
                case err := <-errs: //使用hystrix.Go时返回值是chan error各个协程的错误都放到errs中
                    fmt.Println(err, 1)
                }
            })()
        }
        wg.Wait()
    }
    




  • 相关阅读:
    caml library
    DIV+CSS 最常用知识
    Jquery(Ajax) 调用 SharePoint 2013 Search Rest API 并使用Josn反回结果并简单显示
    SharePoint 使用Jquery
    SharePoint 报表
    在SharePoint 平台上发布一个Power BI 报表
    Sharepoint前端
    .net 高并发 多消费者模式处理订单
    ERP 系统数据库设计规范
    ListView 点击加载更多
  • 原文地址:https://www.cnblogs.com/hualou/p/12084562.html
Copyright © 2011-2022 走看看