-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (63 loc) · 1.81 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
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
package main
import (
"context"
"fmt"
"runtime"
wp "scalable-worker-pool/workerpool"
"sync"
"time"
"github.com/rs/zerolog/log"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
setMaxProcs()
bufferSize := 50000
requests := 50000
var wg sync.WaitGroup
reqHandler := createRequestHandler()
dispatcher := wp.NewDispatcher(bufferSize, &wg, wp.DefaultMaxWorkers, reqHandler)
startWorkers(dispatcher, &wg, reqHandler, wp.DefaultMinWorkers)
go dispatcher.ScaleWorkers(ctx, wp.DefaultMinWorkers, wp.DefaultMaxWorkers, wp.DefaultLoadThreshold)
sendRequests(dispatcher, requests)
// done := make(chan bool)
// <-done
// time.Sleep(5 * time.Second)
gracefulShutdown(dispatcher, ctx)
}
func setMaxProcs() {
numCPU := runtime.NumCPU()
runtime.GOMAXPROCS(numCPU)
log.Info().Msgf("Running with %d CPUs", numCPU)
}
func createRequestHandler() map[int]wp.RequestHandler {
return map[int]wp.RequestHandler{
1: func(data interface{}) error {
return nil
},
}
}
func startWorkers(dispatcher wp.WorkerPoolManager, wg *sync.WaitGroup, reqHandler map[int]wp.RequestHandler, minWorkers int) {
for i := 0; i < minWorkers; i++ {
log.Info().Msgf("Starting worker with id %d", i)
w := wp.NewWorker(i, wg, reqHandler)
dispatcher.AddWorker(w)
}
}
func sendRequests(dispatcher wp.WorkerPoolManager, requestCount int) {
for i := 0; i < requestCount; i++ {
req := wp.Request{
Data: fmt.Sprintf("Hello MsgId: %d", i),
Handler: func(result interface{}) error { return nil },
Type: 1,
Timeout: 5 * time.Second,
}
dispatcher.MakeRequest(req)
}
}
func gracefulShutdown(dispatcher wp.WorkerPoolManager, ctx context.Context) {
stopCtx, stopCancel := context.WithTimeout(ctx, 30*time.Second)
defer stopCancel()
dispatcher.Stop(stopCtx)
log.Info().Msg("Exiting main!")
}