chore: statistic's Snapshot only contains TrackerInfo

This commit is contained in:
wwqgtxx 2023-06-26 18:13:17 +08:00
parent 42ef4fedfa
commit 1cb75350e2
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 {
connections := []Tracker{}
var connections []*TrackerInfo
m.connections.Range(func(key, value any) bool {
connections = append(connections, value.(Tracker))
connections = append(connections, value.(Tracker).Info())
return true
})
return &Snapshot{
@ -114,8 +114,8 @@ func (m *Manager) handle() {
}
type Snapshot struct {
DownloadTotal int64 `json:"downloadTotal"`
UploadTotal int64 `json:"uploadTotal"`
Connections []Tracker `json:"connections"`
DownloadTotal int64 `json:"downloadTotal"`
UploadTotal int64 `json:"uploadTotal"`
Connections []*TrackerInfo `json:"connections"`
Memory uint64 `json:"memory"`
}

View file

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