Having taken a look at several web application examples and boilerplates, the approach they take tends to be in the form of this (I'm using a Gin handler here as an example, and imaginary User and Billing "repository" structs that fetch data from either a database or an external API. I omitted error handling to make the example shorter) :

func GetUserDetailsHandler(c *gin.Context) {
    //this result presumably comes from the app's database
    var userResult = UserRepository.FindById( c.getInt("user_id") )

    //assume that this result comes from a different data source (e.g: a different database) all together, hence why we're not just doing a join query with "User"
    var billingInfo = BillingRepository.FindById(  c.getInt("user_id")  )

    c.JSON(http.StatusOK, gin.H {
        user_data : userResult,
        billing_data : billingInfo,
    })

    return
}

In the above scenario, the call to "User.FindById" might use some kind of database driver, but as far as I'm aware, all available Golang database/ORM libraries return data in a "synchronous" fashion (e.g: as return values, not via channels). As such, the call to "User.FindById" will block until it's complete, before I can move on to executing "BillingInfo.FindById", which is not at all ideal since they can both work in parallel.

So I figured that the best idea was to make use of go routines + syncGroup to solve the problem. Something like this:

func GetUserDetailsHandler(c *gin.Context) {
    var waitGroup sync.WaitGroup

    userChannel := make(chan User);
    billingChannel := make(chan Billing)

    waitGroup.Add(1)
    go func() {
            defer waitGroup.Done()
            userChannel <- UserRepository.FindById( c.getInt("user_id") )               
    }()

    waitGroup.Add(1)
    go func(){
            defer waitGroup.Done()
            billingChannel <- BillingRepository.FindById(  c.getInt("user_id") )
    }()

    waitGroup.Wait()

    userInfo := <- userChannel
    billingInfo = <- billingChannel

    c.JSON(http.StatusOK, gin.H {
        user_data : userResult,
        billing_data : billingInfo,
    })

    return
}

Now, this presumably does the job. But it seems unnecessarily verbose to me, and potentially error prone (if I forget to "Add" to the waitGroup before any go routine, or if I forget to "Wait", then it all falls apart). Is this the only way to do this? Or is there something simpler that I'm missing out?

1

There are 1 best solutions below

0
On

maybe something like this


package main

import (
    "fmt"
)

func GetUserDetailsHander(c *gin.Context) {
    var userInfo USERINlFO
    var billingInfo BILLL

    err := parallel(
        func() (e error) {
            userInfo, e = UserRepository.FindById(c.getInt("user_id"))
            return
        },
        func() (e error) {
            billingInfo, e = BillingRepository.FindById(c.getInt("user_id"))
            return
        },
    )
    fmt.Println(err)

    c.JSON(http.StatusOK, gin.H{
        user_data:    userResult,
        billing_data: billingInfo,
    })

    return
}
func parallel(do ...func() error) error {
    var err error
    rcverr := make(chan error)
    var wg sync.WaitGroup
    for _, d := range do {
        wg.Add(1)
        go func(do func() error) {
            rcverr <- do()
            wg.Done()
        }(d)
    }
    go func() {
        wg.Wait()
        close(rcverr)
    }()
    for range do {
        e := <-rcverr
        if e != nil {
            err = e // return here for fast path
        }
    }
    return err
}