-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconn_runtime.go
98 lines (82 loc) · 1.79 KB
/
conn_runtime.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 ydb
import (
"sync"
"time"
"github.com/yandex-cloud/ydb-go-sdk/v2/internal/stats"
"github.com/yandex-cloud/ydb-go-sdk/v2/timeutil"
)
type connRuntime struct {
mu sync.RWMutex
state ConnState
offlineCount uint64
opStarted uint64
opSucceed uint64
opFailed uint64
opTime *stats.Series
opRate *stats.Series
errRate *stats.Series
}
func (c *connRuntime) stats() ConnStats {
c.mu.Lock()
defer c.mu.Unlock()
now := timeutil.Now()
r := ConnStats{
State: c.state,
OpStarted: c.opStarted,
OpSucceed: c.opSucceed,
OpFailed: c.opFailed,
OpPerMinute: c.opRate.SumPer(now, time.Minute),
ErrPerMinute: c.errRate.SumPer(now, time.Minute),
}
if rtSum, rtCnt := c.opTime.Get(now); rtCnt > 0 {
r.AvgOpTime = time.Duration(rtSum / float64(rtCnt))
}
return r
}
func (c *connRuntime) setState(s ConnState) {
c.mu.Lock()
defer c.mu.Unlock()
c.state = s
if s == ConnOffline {
c.offlineCount++
}
}
func (c *connRuntime) getState() (s ConnState) {
c.mu.RLock()
defer c.mu.RUnlock()
return c.state
}
func (c *connRuntime) operationStart(start time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
c.opStarted++
c.opRate.Add(start, 1)
}
func (c *connRuntime) operationDone(start, end time.Time, err error) {
c.mu.Lock()
defer c.mu.Unlock()
if err != nil {
c.opFailed++
c.errRate.Add(end, 1)
} else {
c.opSucceed++
}
c.opTime.Add(end, float64(end.Sub(start)))
}
func (c *connRuntime) streamStart(now time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
c.opRate.Add(now, 1)
}
func (c *connRuntime) streamRecv(now time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
c.opRate.Add(now, 1)
}
func (c *connRuntime) streamDone(now time.Time, err error) {
c.mu.Lock()
defer c.mu.Unlock()
if err != nil {
c.errRate.Add(now, 1)
}
}