-
Notifications
You must be signed in to change notification settings - Fork 57
/
stats.go
50 lines (41 loc) · 1.19 KB
/
stats.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
package stats
import (
"sync/atomic"
"time"
)
type SessionStater interface {
Start() error
Stop() error
AddSession(key interface{}, session *Session)
GetSession(key interface{}) *Session
RemoveSession(key interface{})
}
type Session struct {
UploadBytes int64 `json:"uploadBytes"`
DownloadBytes int64 `json:"downloadBytes"`
// Process list including current process and its parents.
Processes []string `json:"processes"`
Network string `json:"network"`
LocalAddr string `json:"localAddr"`
RemoteAddr string `json:"remoteAddr"`
SessionStart time.Time `json:"sessionStart"`
SessionEnd time.Time `json:"sessionEnd"`
Extra string `json:"extra"`
OutboundTag string `json:"outboundTag"`
FirstChunkReceived bool `json:"-"`
FirstChunkReceive time.Time `json:"-"`
FirstChunkDuration string `json:"firstChunkDuration"`
}
func (s *Session) handleFirstChunk() {
if !s.FirstChunkReceived {
s.FirstChunkReceived = true
s.FirstChunkReceive = time.Now()
}
}
func (s *Session) AddUploadBytes(n int64) {
atomic.AddInt64(&s.UploadBytes, n)
}
func (s *Session) AddDownloadBytes(n int64) {
s.handleFirstChunk()
atomic.AddInt64(&s.DownloadBytes, n)
}