PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 1, 2022

[FIXED] How can I edit my code to concurrency using in Go?

 October 01, 2022     concurrency, go     No comments   

Issue

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()
}

Solution

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
} 


Answered By - Neenad
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing