登录
首页 >  Golang >  Go问答

golang发出并发请求并结合响应

来源:stackoverflow

时间:2024-03-08 23:36:26 401浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《golang发出并发请求并结合响应》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我编写了go代码来独立调用多个http请求并合并结果。

有时组合方法中会缺少值。

func profile(req *http.request)  (userme, error, userrating, error) {

    wgcall := &sync.waitgroup{}

    uchan := make(chan resuser)
    rchan := make(chan resrating)

        // variable inits
    var meresp userme
    var ratingresp userrating

    go func() {
        res := <-uchan
                meresp = res.value
    }()

    go func() {
        res := <-rchan
                ratingresp = res.value
    }()

    wgcall.add(2)
    go me(req, wgcall, uchan)
    go rate(req, wgcall, rchan)

    wgcall.wait()

    logrus.info(meresp)  // sometimes missing
    logrus.info(ratingresp) // sometimes missing

    return meresp, meerr, ratingresp, ratingerr
}

但是 me 和 rating 调用会按预期返回 api 请求的值。

func me(req *http.Request, wg *sync.WaitGroup, ch chan ResUser) {
    defer wg.Done()

    // http call return value correclty
    me := ...
    ch <- ResUser{
        Value := // value from rest
    }
    logrus.Info(fmt.Sprintf("User calls  %v" , me))  // always return the values
    close(ch)
}

func rate(req *http.Request, wg *sync.WaitGroup, ch chan ResRating) {
    defer wg.Done()

        // make http call
    rating := ...
    ch <- ResRating{
        Value := // value from rest
    }
    logrus.Info(fmt.Sprintf("Ratings calls %v" , rating)) // always return the values

    close(ch)
}

问题是:配置文件函数上的 meresp 和 ratingresp 始终无法获取值。有时只有 meresp 或 ratingresp,有时两者都符合预期。

但是我和rate函数总是调用获取值。

可以帮我解决这个问题吗?


解决方案


您的代码中有一个 race condition

没有任何障碍可以确保从 uchanrchan 读取的 profile 方法中的 goroutine 已填充变量 merespratingresp 您从 profilezqbendcz 返回之前qb。

您可以通过删除 profile 中通道和内联声明的 goroutine 的使用来极大地简化代码。相反,只需直接填充响应值即可。在这种情况下,使用通道或 goroutine 读取它们并没有任何好处,因为您只想发送一个值,并且要求在返回之前存在两个 http 调用生成的值。

您可以通过修改 merate 的签名来接收指向写入其输出的位置的指针,或者通过使用一个小函数包装它们的调用来实现此目的,该函数接收其输出值并将该值填充到 profile 中。重要的是,waitgroup 只能在填充值之后发出信号:

wgcall := &sync.WaitGroup{}

var meResp UserMe
var ratingResp RatingMe

wgcall.Add(2)
// The "me" and "rate" functions should be refactored to
// drop the wait group and channel arguments.
go func() {
    meResp = me(req)
    wgcall.Done()
}()
go func() {
    ratingResp = rate(req)
    wgcall.Done()
}()
wgcall.Wait()

// You are guaranteed that if "me" and "rate" returned valid values,
// they are populated in "meResp" and "ratingResp" at this point.

// Do whatever you need here, such as logging or returning.

终于介绍完啦!小伙伴们,这篇关于《golang发出并发请求并结合响应》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

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