Skip to content

Commit 449116a

Browse files
authored
Merge pull request #3 from rssnyder/feat/waitgp
use wg to wait for storage
2 parents d8cd85c + 005bc06 commit 449116a

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

main.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io/ioutil"
99
"net/http"
10+
"sync"
1011
"time"
1112

1213
"github.com/go-redis/redis/v8"
@@ -20,6 +21,7 @@ var (
2021
hostname *string
2122
password *string
2223
db *int
24+
wg sync.WaitGroup
2325
)
2426

2527
const (
@@ -84,8 +86,11 @@ func main() {
8486
}
8587

8688
for _, coin := range coinsData {
87-
go Store(rdb, coin, time.Duration(*expiry)*time.Second)
89+
wg.Add(1)
90+
go Store(&wg, rdb, coin, time.Duration(*expiry)*time.Second)
8891
}
92+
fmt.Println("waiting for storage")
93+
wg.Wait()
8994

9095
pager++
9196
if pager > *pages {
@@ -134,7 +139,9 @@ func GetMarketData(page int) ([]CoinInfo, error) {
134139
}
135140

136141
// Store puts the coins values into redis
137-
func Store(client *redis.Client, coin CoinInfo, expiry time.Duration) {
142+
func Store(wg *sync.WaitGroup, client *redis.Client, coin CoinInfo, expiry time.Duration) {
143+
defer wg.Done()
144+
138145
client.Set(ctx, coin.ID+"#Symbol", coin.Symbol, expiry)
139146
client.Set(ctx, coin.ID+"#Name", coin.Name, expiry)
140147
client.Set(ctx, coin.ID+"#Image", coin.Image, expiry)

0 commit comments

Comments
 (0)