-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathmain.go
57 lines (48 loc) · 1.24 KB
/
main.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
package main
import (
"log/slog"
"sync"
"time"
)
func SingleFlight(db Middleware) {
slog.Info("================== without singleflight ===================")
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go func(req int) {
defer wg.Done()
data := db.GetArticle(req, 1)
slog.Info("data info", "data", data, "req", req)
}(i)
}
wg.Wait()
slog.Info("================== using singleflight Do ===================")
wg.Add(5)
for i := 5; i < 10; i++ {
go func(req int) {
defer wg.Done()
data := db.GetArticleDo(req, 2)
slog.Info("data info", "data", data, "req", req)
}(i)
}
wg.Wait()
slog.Info("================== using singleflight DoChan ===================")
wg.Add(5)
for i := 10; i < 15; i++ {
go func(req int) {
defer wg.Done()
t := time.Duration(time.Duration(int64(req*10)) * time.Millisecond)
data := db.GetArticleDoChan(req, 3, t)
slog.Info("data info", "data", data, "req", req)
}(i)
}
wg.Wait()
}
func main() {
slog.Info("singleflight (none Generic) test started..")
SingleFlight(NewDB())
slog.Info("singleflight (none Generic) test finished..")
slog.Info("singleflight (Generic) test started..")
SingleFlight(NewDBG())
slog.Info("singleflight (Generic) test finished..")
}