2022-07-22 09:29:13 +08:00
|
|
|
package trafficontrol
|
2022-07-19 22:16:49 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/experimental/clashapi/compatible"
|
|
|
|
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Manager struct {
|
|
|
|
connections compatible.Map[string, tracker]
|
|
|
|
uploadTemp *atomic.Int64
|
|
|
|
downloadTemp *atomic.Int64
|
|
|
|
uploadBlip *atomic.Int64
|
|
|
|
downloadBlip *atomic.Int64
|
|
|
|
uploadTotal *atomic.Int64
|
|
|
|
downloadTotal *atomic.Int64
|
2022-07-30 14:50:33 +08:00
|
|
|
ticker *time.Ticker
|
|
|
|
done chan struct{}
|
2022-07-19 22:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager() *Manager {
|
|
|
|
manager := &Manager{
|
|
|
|
uploadTemp: atomic.NewInt64(0),
|
|
|
|
downloadTemp: atomic.NewInt64(0),
|
|
|
|
uploadBlip: atomic.NewInt64(0),
|
|
|
|
downloadBlip: atomic.NewInt64(0),
|
|
|
|
uploadTotal: atomic.NewInt64(0),
|
|
|
|
downloadTotal: atomic.NewInt64(0),
|
2022-07-30 14:50:33 +08:00
|
|
|
ticker: time.NewTicker(time.Second),
|
|
|
|
done: make(chan struct{}),
|
2022-07-19 22:16:49 +08:00
|
|
|
}
|
|
|
|
go manager.handle()
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Join(c tracker) {
|
|
|
|
m.connections.Store(c.ID(), c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Leave(c tracker) {
|
|
|
|
m.connections.Delete(c.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) PushUploaded(size int64) {
|
|
|
|
m.uploadTemp.Add(size)
|
|
|
|
m.uploadTotal.Add(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) PushDownloaded(size int64) {
|
|
|
|
m.downloadTemp.Add(size)
|
|
|
|
m.downloadTotal.Add(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Now() (up int64, down int64) {
|
|
|
|
return m.uploadBlip.Load(), m.downloadBlip.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Snapshot() *Snapshot {
|
2022-07-30 14:50:33 +08:00
|
|
|
var connections []tracker
|
2022-07-19 22:16:49 +08:00
|
|
|
m.connections.Range(func(_ string, value tracker) bool {
|
|
|
|
connections = append(connections, value)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return &Snapshot{
|
|
|
|
UploadTotal: m.uploadTotal.Load(),
|
|
|
|
DownloadTotal: m.downloadTotal.Load(),
|
|
|
|
Connections: connections,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) ResetStatistic() {
|
|
|
|
m.uploadTemp.Store(0)
|
|
|
|
m.uploadBlip.Store(0)
|
|
|
|
m.uploadTotal.Store(0)
|
|
|
|
m.downloadTemp.Store(0)
|
|
|
|
m.downloadBlip.Store(0)
|
|
|
|
m.downloadTotal.Store(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) handle() {
|
2022-08-02 18:47:23 +08:00
|
|
|
var uploadTemp int64
|
|
|
|
var downloadTemp int64
|
2022-07-30 14:50:33 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.done:
|
|
|
|
return
|
|
|
|
case <-m.ticker.C:
|
|
|
|
}
|
2022-08-02 18:47:23 +08:00
|
|
|
uploadTemp = m.uploadTemp.Swap(0)
|
|
|
|
downloadTemp = m.downloadTemp.Swap(0)
|
|
|
|
m.uploadBlip.Store(uploadTemp)
|
|
|
|
m.downloadBlip.Store(downloadTemp)
|
2022-07-19 22:16:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 14:50:33 +08:00
|
|
|
func (m *Manager) Close() error {
|
|
|
|
m.ticker.Stop()
|
|
|
|
close(m.done)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-19 22:16:49 +08:00
|
|
|
type Snapshot struct {
|
|
|
|
DownloadTotal int64 `json:"downloadTotal"`
|
|
|
|
UploadTotal int64 `json:"uploadTotal"`
|
|
|
|
Connections []tracker `json:"connections"`
|
|
|
|
}
|