-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_service_call_v3.go
94 lines (75 loc) · 2.36 KB
/
async_service_call_v3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
//to make web requests
"net/http"
//read the response from call
//unmarshall the XML response
"time"
"runtime"
)
// You can use number of CPU if you know the nuber of cores your machine
// is running on. to take advantage of parralism
func main() {
runtime.GOMAXPROCS(2)
//tracking variables for go routine
numOfCompletedRoutine := 0
gihubUserName := []string{
"SamsadSajid",
"google",
}
start := time.Now()
for _, name := range gihubUserName {
//
/**
* Following is a self executing function which is a g routine
* It takes a string param symbol. We can't use the `name` from
* the `for` loop because the following is a concurrent function
* to main function. Main func will continue to execute the for loop
* not knwoing about the following function. If we kept the `name` variable
* it might so happen that after all the iterations have been done, the concurrency
* has begun execution and right then the `name` variable points to only the
* `SamsadSajid` from the above list which we definitely don't want. We want
* for each of the name to make a network call!
* So each goroutines are decoupled from for loop
*/
go func(symbol string) {
fmt.Println(symbol)
resp, _ := http.Get("https://api.github.com/users/" + symbol)
//close the connection
defer resp.Body.Close()
// this should be done carefully because
// if data is big it can blow up the system but
// in this example data is small so we can read that in single byte stream
body, _ := ioutil.ReadAll(resp.Body)
gihubUserInfo := GithubUserInfo{}
json.Unmarshal(body, &gihubUserInfo)
fmt.Println(
"Login Name: = ", gihubUserInfo.Login,
"Followers:=", gihubUserInfo.Followers,
"Email:=", gihubUserInfo.Email,
"Bio:=", gihubUserInfo.Bio,
)
numOfCompletedRoutine++
fmt.Println("numOfCompletedRoutine:=", numOfCompletedRoutine)
}(name)
}
/*
* As long as numOfCompletedRoutine is not equal to the
* len of the gihubUserName list, make the main function
* sleep for 10 Millisecond
*/
for numOfCompletedRoutine < len(gihubUserName) {
time.Sleep(10 * time.Millisecond)
}
timeTakenForCall := time.Since(start)
fmt.Println("Execution time is: ", timeTakenForCall)
}
type GithubUserInfo struct {
Login string
Followers int
Email string
Bio string
}