-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrency.go
61 lines (52 loc) · 1.66 KB
/
concurrency.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
package concurrency
import(
"net/http"
)
type CurrentWeather struct {
LocationData []LocalWeather `json:"weather"`
Main TemperatureData `json:"main"`
}
type LocalWeather struct {
Id int `json:"id"`
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
}
type TemperatureData struct {
Temp float64 `json:"temp"`
TempIndex float64 `json:"feels_like"`
TempMin float64 `json:"temp_min"`
TempMax float64 `json:"temp_max"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
SeaLevel int `json:"sea_level"`
GroundLevel int `json:"grnd_level"`
}
// fanOut: In this pattern, the parent goroutine creates 10 child goroutines
// and waits for them to signal their results.
// I usually use gRPC for implementations so if this seems wonky thats why (its also pretty barebones)
func fanOut(w http.ResponseWriter, r *http.Request, ps httprouter.Param) {
children := 10
ch := make(chan string, children)
lat, long, apiKey := ps.ByName("lat"), ps.ByName("lon"), ps.ByName("apiKey")
url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s", lat, long, apiKey)
// now we're gonna hit this api ten times...but concurrently (rip my free api tier)
fmt.Println("response: ", string(response))
for c := 0; c < children; c++ {
go func(child int) {
resp, err := http.Get(url)
if err{
resp := "error"
}
ch <- resp
fmt.Println("child : sent signal :", child)
}(c)
}
for children > 0 {
d := <-ch
children--
fmt.Println(d)
fmt.Println("parent : recv'd signal :", children)
}
time.Sleep(time.Second)
}