-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
67 lines (53 loc) · 1.2 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
package main
import (
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"sync/atomic"
"syscall"
"time"
"github.com/singchia/go-timer/v2/pkg/scheduler"
)
func main() {
go func() {
http.ListenAndServe("0.0.0.0:6060", nil)
}()
sch := scheduler.NewScheduler()
sch.Interval = time.Millisecond * 100
sch.SetMonitor(SchedulerMonitor)
sch.SetMaxRate(0.95)
sch.SetMaxGoroutines(5000)
sch.StartSchedule()
var val int64
for i := 0; i < 100*10000*10; i++ {
sch.PublishRequest(&scheduler.Request{Data: val, Handler: SchedulerHandler})
atomic.AddInt64(&val, 1)
}
time.Sleep(time.Second * 10)
fmt.Printf("maxValue: %d\n", maxValue)
sch.Close()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
done := make(chan bool, 1)
go func() {
<-sigs
done <- true
}()
<-done
fmt.Println(runtime.NumGoroutine())
}
var maxValue int64 = 0
func SchedulerHandler(data interface{}) {
val, ok := data.(int64)
if ok {
if val > maxValue {
maxValue = val
}
}
}
func SchedulerMonitor(incomingReqsDiff, processedReqsDiff, diff, currentGotoutines int64) {
fmt.Printf("%d, %d, %d, %d\n", incomingReqsDiff, processedReqsDiff, diff, currentGotoutines)
}