chore: statistic's Snapshot only contains TrackerInfo

This commit is contained in:
gVisor bot 2023-06-26 18:13:17 +08:00
parent 679348d994
commit 27325ccfab
2 changed files with 23 additions and 14 deletions

View file

@ -66,9 +66,9 @@ func (m *Manager) Memory() uint64 {
} }
func (m *Manager) Snapshot() *Snapshot { func (m *Manager) Snapshot() *Snapshot {
connections := []Tracker{} var connections []*TrackerInfo
m.connections.Range(func(key, value any) bool { m.connections.Range(func(key, value any) bool {
connections = append(connections, value.(Tracker)) connections = append(connections, value.(Tracker).Info())
return true return true
}) })
return &Snapshot{ return &Snapshot{
@ -114,8 +114,8 @@ func (m *Manager) handle() {
} }
type Snapshot struct { type Snapshot struct {
DownloadTotal int64 `json:"downloadTotal"` DownloadTotal int64 `json:"downloadTotal"`
UploadTotal int64 `json:"uploadTotal"` UploadTotal int64 `json:"uploadTotal"`
Connections []Tracker `json:"connections"` Connections []*TrackerInfo `json:"connections"`
Memory uint64 `json:"memory"` Memory uint64 `json:"memory"`
} }

View file

@ -18,10 +18,11 @@ import (
type Tracker interface { type Tracker interface {
ID() string ID() string
Close() error Close() error
Info() *TrackerInfo
C.Connection C.Connection
} }
type trackerInfo struct { type TrackerInfo struct {
UUID uuid.UUID `json:"id"` UUID uuid.UUID `json:"id"`
Metadata *C.Metadata `json:"metadata"` Metadata *C.Metadata `json:"metadata"`
UploadTotal *atomic.Int64 `json:"upload"` UploadTotal *atomic.Int64 `json:"upload"`
@ -34,7 +35,7 @@ type trackerInfo struct {
type tcpTracker struct { type tcpTracker struct {
C.Conn `json:"-"` C.Conn `json:"-"`
*trackerInfo *TrackerInfo
manager *Manager manager *Manager
pushToManager bool `json:"-"` pushToManager bool `json:"-"`
@ -44,6 +45,10 @@ func (tt *tcpTracker) ID() string {
return tt.UUID.String() return tt.UUID.String()
} }
func (tt *tcpTracker) Info() *TrackerInfo {
return tt.TrackerInfo
}
func (tt *tcpTracker) Read(b []byte) (int, error) { func (tt *tcpTracker) Read(b []byte) (int, error) {
n, err := tt.Conn.Read(b) n, err := tt.Conn.Read(b)
download := int64(n) download := int64(n)
@ -134,7 +139,7 @@ func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.R
t := &tcpTracker{ t := &tcpTracker{
Conn: conn, Conn: conn,
manager: manager, manager: manager,
trackerInfo: &trackerInfo{ TrackerInfo: &TrackerInfo{
UUID: utils.NewUUIDV4(), UUID: utils.NewUUIDV4(),
Start: time.Now(), Start: time.Now(),
Metadata: metadata, Metadata: metadata,
@ -156,8 +161,8 @@ func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.R
} }
if rule != nil { if rule != nil {
t.trackerInfo.Rule = rule.RuleType().String() t.TrackerInfo.Rule = rule.RuleType().String()
t.trackerInfo.RulePayload = rule.Payload() t.TrackerInfo.RulePayload = rule.Payload()
} }
manager.Join(t) manager.Join(t)
@ -166,7 +171,7 @@ func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.R
type udpTracker struct { type udpTracker struct {
C.PacketConn `json:"-"` C.PacketConn `json:"-"`
*trackerInfo *TrackerInfo
manager *Manager manager *Manager
pushToManager bool `json:"-"` pushToManager bool `json:"-"`
@ -176,6 +181,10 @@ func (ut *udpTracker) ID() string {
return ut.UUID.String() return ut.UUID.String()
} }
func (ut *udpTracker) Info() *TrackerInfo {
return ut.TrackerInfo
}
func (ut *udpTracker) ReadFrom(b []byte) (int, net.Addr, error) { func (ut *udpTracker) ReadFrom(b []byte) (int, net.Addr, error) {
n, addr, err := ut.PacketConn.ReadFrom(b) n, addr, err := ut.PacketConn.ReadFrom(b)
download := int64(n) download := int64(n)
@ -221,7 +230,7 @@ func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, ru
ut := &udpTracker{ ut := &udpTracker{
PacketConn: conn, PacketConn: conn,
manager: manager, manager: manager,
trackerInfo: &trackerInfo{ TrackerInfo: &TrackerInfo{
UUID: utils.NewUUIDV4(), UUID: utils.NewUUIDV4(),
Start: time.Now(), Start: time.Now(),
Metadata: metadata, Metadata: metadata,
@ -243,8 +252,8 @@ func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, ru
} }
if rule != nil { if rule != nil {
ut.trackerInfo.Rule = rule.RuleType().String() ut.TrackerInfo.Rule = rule.RuleType().String()
ut.trackerInfo.RulePayload = rule.Payload() ut.TrackerInfo.RulePayload = rule.Payload()
} }
manager.Join(ut) manager.Join(ut)