Replace usages of uber/atomic

This commit is contained in:
世界 2023-04-14 20:55:05 +08:00
parent e57b6ae98d
commit 50a7295360
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 38 additions and 34 deletions

View file

@ -4,32 +4,26 @@ import (
"time" "time"
"github.com/sagernet/sing-box/experimental/clashapi/compatible" "github.com/sagernet/sing-box/experimental/clashapi/compatible"
"github.com/sagernet/sing/common/atomic"
"go.uber.org/atomic"
) )
type Manager struct { type Manager struct {
connections compatible.Map[string, tracker] uploadTemp atomic.Int64
uploadTemp *atomic.Int64 downloadTemp atomic.Int64
downloadTemp *atomic.Int64 uploadBlip atomic.Int64
uploadBlip *atomic.Int64 downloadBlip atomic.Int64
downloadBlip *atomic.Int64 uploadTotal atomic.Int64
uploadTotal *atomic.Int64 downloadTotal atomic.Int64
downloadTotal *atomic.Int64
ticker *time.Ticker connections compatible.Map[string, tracker]
done chan struct{} ticker *time.Ticker
done chan struct{}
} }
func NewManager() *Manager { func NewManager() *Manager {
manager := &Manager{ manager := &Manager{
uploadTemp: atomic.NewInt64(0), ticker: time.NewTicker(time.Second),
downloadTemp: atomic.NewInt64(0), done: make(chan struct{}),
uploadBlip: atomic.NewInt64(0),
downloadBlip: atomic.NewInt64(0),
uploadTotal: atomic.NewInt64(0),
downloadTotal: atomic.NewInt64(0),
ticker: time.NewTicker(time.Second),
done: make(chan struct{}),
} }
go manager.handle() go manager.handle()
return manager return manager

View file

@ -1,6 +1,7 @@
package trafficontrol package trafficontrol
import ( import (
"encoding/json"
"net" "net"
"net/netip" "net/netip"
"time" "time"
@ -8,10 +9,10 @@ import (
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/experimental/trackerconn" "github.com/sagernet/sing-box/experimental/trackerconn"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/atomic"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
"github.com/gofrs/uuid" "github.com/gofrs/uuid/v5"
"go.uber.org/atomic"
) )
type Metadata struct { type Metadata struct {
@ -43,6 +44,19 @@ type trackerInfo struct {
RulePayload string `json:"rulePayload"` RulePayload string `json:"rulePayload"`
} }
func (t trackerInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"id": t.UUID.String(),
"metadata": t.Metadata,
"upload": t.UploadTotal.Load(),
"download": t.DownloadTotal.Load(),
"start": t.Start,
"chains": t.Chain,
"rule": t.Rule,
"rulePayload": t.RulePayload,
})
}
type tcpTracker struct { type tcpTracker struct {
N.ExtendedConn `json:"-"` N.ExtendedConn `json:"-"`
*trackerInfo *trackerInfo
@ -97,8 +111,8 @@ func NewTCPTracker(conn net.Conn, manager *Manager, metadata Metadata, router ad
next = group.Now() next = group.Now()
} }
upload := atomic.NewInt64(0) upload := new(atomic.Int64)
download := atomic.NewInt64(0) download := new(atomic.Int64)
t := &tcpTracker{ t := &tcpTracker{
ExtendedConn: trackerconn.NewHook(conn, func(n int64) { ExtendedConn: trackerconn.NewHook(conn, func(n int64) {
@ -184,8 +198,8 @@ func NewUDPTracker(conn N.PacketConn, manager *Manager, metadata Metadata, route
next = group.Now() next = group.Now()
} }
upload := atomic.NewInt64(0) upload := new(atomic.Int64)
download := atomic.NewInt64(0) download := new(atomic.Int64)
ut := &udpTracker{ ut := &udpTracker{
PacketConn: trackerconn.NewHookPacket(conn, func(n int64) { PacketConn: trackerconn.NewHookPacket(conn, func(n int64) {

View file

@ -3,11 +3,10 @@ package trackerconn
import ( import (
"net" "net"
"github.com/sagernet/sing/common/atomic"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio" "github.com/sagernet/sing/common/bufio"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
"go.uber.org/atomic"
) )
func New(conn net.Conn, readCounter []*atomic.Int64, writeCounter []*atomic.Int64) *Conn { func New(conn net.Conn, readCounter []*atomic.Int64, writeCounter []*atomic.Int64) *Conn {

View file

@ -1,11 +1,10 @@
package trackerconn package trackerconn
import ( import (
"github.com/sagernet/sing/common/atomic"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
"go.uber.org/atomic"
) )
func NewPacket(conn N.PacketConn, readCounter []*atomic.Int64, writeCounter []*atomic.Int64) *PacketConn { func NewPacket(conn N.PacketConn, readCounter []*atomic.Int64, writeCounter []*atomic.Int64) *PacketConn {

View file

@ -12,10 +12,9 @@ import (
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/experimental/trackerconn" "github.com/sagernet/sing-box/experimental/trackerconn"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/atomic"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
"go.uber.org/atomic"
) )
func init() { func init() {
@ -211,7 +210,7 @@ func (s *StatsService) loadOrCreateCounter(name string) *atomic.Int64 {
if loaded { if loaded {
return counter return counter
} }
counter = atomic.NewInt64(0) counter = &atomic.Int64{}
s.counters[name] = counter s.counters[name] = counter
return counter return counter
} }

View file

@ -10,11 +10,10 @@ import (
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/atomic"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
"go.uber.org/atomic"
) )
var _ adapter.Inbound = (*myInboundAdapter)(nil) var _ adapter.Inbound = (*myInboundAdapter)(nil)