Home > OS >  How can I edit my code to concurrency using in Go?
How can I edit my code to concurrency using in Go?

Time:07-02

I have a function getIPSetInfo that makes HTTP requests as many times as there are elements in IpsetInfo array. How can I redesign my function to use multithreading? I need all those queries in loop to be done in parallel.

type IpsetInfo struct {
    OrgRef               ObjDesc  `json:"orgRef"`
    EdgeGatewayRef       ObjDesc  `json:"edgeGatewayRef"`
    OwnerRef             ObjDesc  `json:"ownerRef"`
    NetworkProviderScope string   `json:"networkProviderScope"`
    Status               string   `json:"status"`
    Id                   string   `json:"id"`
    Name                 string   `json:"name"`
    Description          string   `json:"description"`
    Type                 string   `json:"type"`
    IpAddresses          []string `json:"ipaddresses"`
}

func getIPSetInfo(ipsetIds []string) []IpsetInfo {
    var result []IpsetInfo
    for i := range ipsetIds {
        url := "https://api-cloud-platform.com/cloudapi/1.0.0/firewallGroups/"   ipsetIds[i]
        _, body := httpGet(url, map[string]string{
            "Authorization": "Bearer "   accessToken,
        })
        var x IpsetInfo
        json.Unmarshal([]byte(body), &x)
        result = append(result, x)
    }
    return result
} 

func main() {
    ipsetIds := getIPSetIDs()
    IPSets := getIPSetInfo(ipsetIds)

    // Below terraform manifest will be created from template 
    tmpl, err := template.ParseFiles("ipsets.template")
    if err != nil {
        log.Fatal(err)
    }
    f, err := os.Create("result.tf")
    if err != nil {
        log.Println("create file: ", err)
        return
    }
    err = tmpl.Execute(f, IPSets)
    if err != nil {
        log.Print("execute: ", err)
        return
    }
    f.Close()
}

CodePudding user response:

Maybe this could help

func getIPSetInfo(ipsetIds []string) []IpsetInfo {
    mu := &sync.Mutex{}
    var wg sync.WaitGroup
    
    var result []IpsetInfo
    
    call := func(val string){
        defer wg.Done()
        
        url := "https://api-cloud-platform.com/cloudapi/1.0.0/firewallGroups/"   val
        _, body := httpGet(url, map[string]string{
            "Authorization": "Bearer "   accessToken,
        })
        var x IpsetInfo
        json.Unmarshal([]byte(body), &x)
        
        mu.Lock()
        defer mu.Unlock()
        
        result = append(result, x)
    }
    
    for _, val := range ipsetIds {
        wg.Add(1)
        go call(val)
    }

    wg.Wait()
    return result
} 
  • Related