-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
98 lines (83 loc) · 1.66 KB
/
monitor.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gopool
import (
"fmt"
"log"
"runtime"
"sync/atomic"
"time"
)
const text = `{"starve":%d,"active":%d,worker":%d,"pool":%d,"err":%s}`
type Monitor interface {
Starve() int //饥饿的worker数
Active() int //worker活跃数量
Worker() int //当前worker的总数量
Pool() int //池的大小
String() string
Err() error
}
type monitor struct {
starve int
active int
worker int
pool int
err error
}
func (m *monitor) Starve() int {
return m.starve
}
func (m *monitor) Active() int {
return m.active
}
func (m *monitor) Worker() int {
return m.worker
}
func (m *monitor) Pool() int {
return m.pool
}
func (m *monitor) Err() error {
return m.err
}
func (m *monitor) String() string {
return fmt.Sprintf(text, m.starve, m.worker, m.active, m.pool, m.err)
}
//---------------------------------------------------
var (
ready uint32
add uint32
lose uint32
destroy uint32
adjust uint32
)
func (p *Pool) monitor() {
for {
select {
case <-p.ctx.Done():
return
default:
time.Sleep(time.Millisecond * 500)
p.Debug()
}
}
}
func (p *Pool) Debug() (result map[string]interface{}) {
defer func() {
if x:=recover();x!=nil{
fmt.Println("ERR:",x)
}
}()
result = make(map[string]interface{})
p.lock.RLock()
w := len(p.worker)
c := cap(p.worker)
p.lock.RUnlock()
result["goroutine"] = runtime.NumGoroutine()
result["len"] = w
result["cap"] = c
result["ready"] = atomic.LoadUint32(&ready)
result["add"] = atomic.LoadUint32(&add)
result["destroy"] = atomic.LoadUint32(&destroy)
result["lose"] = atomic.LoadUint32(&lose)
result["adjust"] = atomic.LoadUint32(&adjust)
log.Printf("%+v\n", result)
return result
}