-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconnectionwatcher.go
71 lines (59 loc) · 1.66 KB
/
connectionwatcher.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
package j8a
import (
"net"
"net/http"
"sync/atomic"
)
type ConnectionWatcher struct {
dwnOpenConns int64
dwnMaxOpenConns int64
upOpenConns int64
upMaxOpenConns int64
}
// OnStateChange records open connections in response to connection
// state changes. Set net/http Server.ConnState to this method
// as value.
func (cw *ConnectionWatcher) OnStateChange(conn net.Conn, state http.ConnState) {
switch state {
case http.StateNew:
cw.AddDwn(1)
case http.StateHijacked, http.StateClosed:
cw.AddDwn(-1)
}
cw.UpdateMaxDwn(cw.DwnCount())
}
// Count returns the number of connections at the time
// the call.
func (cw *ConnectionWatcher) DwnCount() uint64 {
return uint64(atomic.LoadInt64(&cw.dwnOpenConns))
}
func (cw *ConnectionWatcher) DwnMaxCount() uint64 {
return uint64(atomic.LoadInt64(&cw.dwnMaxOpenConns))
}
// Add adds c to the number of active connections.
func (cw *ConnectionWatcher) AddDwn(c int64) {
atomic.AddInt64(&cw.dwnOpenConns, c)
}
// Sets the maximum number of active connections observed
func (cw *ConnectionWatcher) UpdateMaxDwn(c uint64) {
if c > cw.DwnMaxCount() {
atomic.StoreInt64(&cw.dwnMaxOpenConns, int64(c))
}
}
func (cw *ConnectionWatcher) UpCount() uint64 {
return uint64(atomic.LoadInt64(&cw.upOpenConns))
}
func (cw *ConnectionWatcher) UpMaxCount() uint64 {
return uint64(atomic.LoadInt64(&cw.upMaxOpenConns))
}
func (cw *ConnectionWatcher) AddUp(c int64) {
atomic.AddInt64(&cw.upOpenConns, c)
}
func (cw *ConnectionWatcher) SetUp(c uint64) {
atomic.StoreInt64(&cw.upOpenConns, int64(c))
}
func (cw *ConnectionWatcher) UpdateMaxUp(c uint64) {
if c > cw.UpMaxCount() {
atomic.StoreInt64(&cw.upMaxOpenConns, int64(c))
}
}