From 3d0c64f523937ec830f05b3031c691998dc3dcb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 8 Apr 2023 22:07:55 +0800 Subject: [PATCH] Replace usages of uber/atomic --- .../clashapi/trafficontrol/manager.go | 32 ++++++++----------- .../clashapi/trafficontrol/tracker.go | 24 +++++++++++--- experimental/trackerconn/conn.go | 3 +- experimental/trackerconn/packet_conn.go | 3 +- experimental/v2rayapi/stats.go | 5 ++- go.mod | 8 ++--- go.sum | 12 +++---- inbound/default.go | 3 +- 8 files changed, 47 insertions(+), 43 deletions(-) diff --git a/experimental/clashapi/trafficontrol/manager.go b/experimental/clashapi/trafficontrol/manager.go index 69298788..c1472f34 100644 --- a/experimental/clashapi/trafficontrol/manager.go +++ b/experimental/clashapi/trafficontrol/manager.go @@ -4,32 +4,26 @@ import ( "time" "github.com/sagernet/sing-box/experimental/clashapi/compatible" - - "go.uber.org/atomic" + "github.com/sagernet/sing/common/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 - ticker *time.Ticker - done chan struct{} + uploadTemp atomic.Int64 + downloadTemp atomic.Int64 + uploadBlip atomic.Int64 + downloadBlip atomic.Int64 + uploadTotal atomic.Int64 + downloadTotal atomic.Int64 + + connections compatible.Map[string, tracker] + ticker *time.Ticker + done chan struct{} } 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), - ticker: time.NewTicker(time.Second), - done: make(chan struct{}), + ticker: time.NewTicker(time.Second), + done: make(chan struct{}), } go manager.handle() return manager diff --git a/experimental/clashapi/trafficontrol/tracker.go b/experimental/clashapi/trafficontrol/tracker.go index 7e16dcdd..af1b350f 100644 --- a/experimental/clashapi/trafficontrol/tracker.go +++ b/experimental/clashapi/trafficontrol/tracker.go @@ -1,6 +1,7 @@ package trafficontrol import ( + "encoding/json" "net" "net/netip" "time" @@ -8,10 +9,10 @@ import ( "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/experimental/trackerconn" "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/atomic" N "github.com/sagernet/sing/common/network" "github.com/gofrs/uuid" - "go.uber.org/atomic" ) type Metadata struct { @@ -43,6 +44,19 @@ type trackerInfo struct { 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 { N.ExtendedConn `json:"-"` *trackerInfo @@ -97,8 +111,8 @@ func NewTCPTracker(conn net.Conn, manager *Manager, metadata Metadata, router ad next = group.Now() } - upload := atomic.NewInt64(0) - download := atomic.NewInt64(0) + upload := new(atomic.Int64) + download := new(atomic.Int64) t := &tcpTracker{ ExtendedConn: trackerconn.NewHook(conn, func(n int64) { @@ -184,8 +198,8 @@ func NewUDPTracker(conn N.PacketConn, manager *Manager, metadata Metadata, route next = group.Now() } - upload := atomic.NewInt64(0) - download := atomic.NewInt64(0) + upload := new(atomic.Int64) + download := new(atomic.Int64) ut := &udpTracker{ PacketConn: trackerconn.NewHookPacket(conn, func(n int64) { diff --git a/experimental/trackerconn/conn.go b/experimental/trackerconn/conn.go index 07efd814..f9f70e7c 100644 --- a/experimental/trackerconn/conn.go +++ b/experimental/trackerconn/conn.go @@ -3,11 +3,10 @@ package trackerconn import ( "net" + "github.com/sagernet/sing/common/atomic" "github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/bufio" N "github.com/sagernet/sing/common/network" - - "go.uber.org/atomic" ) func New(conn net.Conn, readCounter []*atomic.Int64, writeCounter []*atomic.Int64) *Conn { diff --git a/experimental/trackerconn/packet_conn.go b/experimental/trackerconn/packet_conn.go index f90f4b9b..3a84c565 100644 --- a/experimental/trackerconn/packet_conn.go +++ b/experimental/trackerconn/packet_conn.go @@ -1,11 +1,10 @@ package trackerconn import ( + "github.com/sagernet/sing/common/atomic" "github.com/sagernet/sing/common/buf" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" - - "go.uber.org/atomic" ) func NewPacket(conn N.PacketConn, readCounter []*atomic.Int64, writeCounter []*atomic.Int64) *PacketConn { diff --git a/experimental/v2rayapi/stats.go b/experimental/v2rayapi/stats.go index b4d307d9..dfb5d666 100644 --- a/experimental/v2rayapi/stats.go +++ b/experimental/v2rayapi/stats.go @@ -12,10 +12,9 @@ import ( "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/experimental/trackerconn" "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing/common/atomic" E "github.com/sagernet/sing/common/exceptions" N "github.com/sagernet/sing/common/network" - - "go.uber.org/atomic" ) func init() { @@ -211,7 +210,7 @@ func (s *StatsService) loadOrCreateCounter(name string) *atomic.Int64 { if loaded { return counter } - counter = atomic.NewInt64(0) + counter = &atomic.Int64{} s.counters[name] = counter return counter } diff --git a/go.mod b/go.mod index b083469b..2d6ba669 100644 --- a/go.mod +++ b/go.mod @@ -25,12 +25,12 @@ require ( github.com/sagernet/gomobile v0.0.0-20221130124640-349ebaa752ca github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32 github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 - github.com/sagernet/sing v0.2.2-0.20230408041759-3f81b77e6314 + github.com/sagernet/sing v0.2.2-0.20230408135335-14c8c38f67f2 github.com/sagernet/sing-dns v0.1.5-0.20230408004833-5adaf486d440 - github.com/sagernet/sing-shadowsocks v0.2.0 + github.com/sagernet/sing-shadowsocks v0.2.1-0.20230408064153-75848ae8c6a2 github.com/sagernet/sing-shadowtls v0.1.0 github.com/sagernet/sing-tun v0.1.4-0.20230326080954-8848c0e4cbab - github.com/sagernet/sing-vmess v0.1.3 + github.com/sagernet/sing-vmess v0.1.4-0.20230408065245-5bca9ddcac72 github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9 github.com/sagernet/utls v0.0.0-20230309024959-6732c2ab36f2 @@ -39,7 +39,6 @@ require ( github.com/spf13/cobra v1.7.0 github.com/stretchr/testify v1.8.2 go.etcd.io/bbolt v1.3.7 - go.uber.org/atomic v1.10.0 go.uber.org/zap v1.24.0 go4.org/netipx v0.0.0-20230303233057-f1b76eb4bb35 golang.org/x/crypto v0.7.0 @@ -82,6 +81,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 // indirect github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect + go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.6.0 // indirect golang.org/x/mod v0.8.0 // indirect golang.org/x/text v0.9.0 // indirect diff --git a/go.sum b/go.sum index 9ba57678..06e6953d 100644 --- a/go.sum +++ b/go.sum @@ -111,18 +111,18 @@ github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byL github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= -github.com/sagernet/sing v0.2.2-0.20230408041759-3f81b77e6314 h1:ZDh6bkjizB5pIMwxnQaxih/NwO6YdviV0suahgdaxG4= -github.com/sagernet/sing v0.2.2-0.20230408041759-3f81b77e6314/go.mod h1:9uHswk2hITw8leDbiLS/xn0t9nzBcbePxzm9PJhwdlw= +github.com/sagernet/sing v0.2.2-0.20230408135335-14c8c38f67f2 h1:1DuT9KnpWQx3Vy4pDP5duBuyyk//HB8OC0QrhZ0Y9kI= +github.com/sagernet/sing v0.2.2-0.20230408135335-14c8c38f67f2/go.mod h1:9uHswk2hITw8leDbiLS/xn0t9nzBcbePxzm9PJhwdlw= github.com/sagernet/sing-dns v0.1.5-0.20230408004833-5adaf486d440 h1:VH8/BcOVuApHtS+vKP+khxlGRcXH7KKhgkTDtNynqSQ= github.com/sagernet/sing-dns v0.1.5-0.20230408004833-5adaf486d440/go.mod h1:69PNSHyEmXdjf6C+bXBOdr2GQnPeEyWjIzo/MV8gmz8= -github.com/sagernet/sing-shadowsocks v0.2.0 h1:ILDWL7pwWfkPLEbviE/MyCgfjaBmJY/JVVY+5jhSb58= -github.com/sagernet/sing-shadowsocks v0.2.0/go.mod h1:ysYzszRLpNzJSorvlWRMuzU6Vchsp7sd52q+JNY4axw= +github.com/sagernet/sing-shadowsocks v0.2.1-0.20230408064153-75848ae8c6a2 h1:wCsNEWWjNUaSRH3CB2UkhAb1Zi8NotBWcMUN9qEEoeE= +github.com/sagernet/sing-shadowsocks v0.2.1-0.20230408064153-75848ae8c6a2/go.mod h1:x6jt78kUBzfZ0zlZHbcNfqLzCRM5s2HHhavJ2bSwIZs= github.com/sagernet/sing-shadowtls v0.1.0 h1:05MYce8aR5xfKIn+y7xRFsdKhKt44QZTSEQW+lG5IWQ= github.com/sagernet/sing-shadowtls v0.1.0/go.mod h1:Kn1VUIprdkwCgkS6SXYaLmIpKzQbqBIKJBMY+RvBhYc= github.com/sagernet/sing-tun v0.1.4-0.20230326080954-8848c0e4cbab h1:a9oeWuPBuIZ70qMhIIH6RrYhp886xN9jJIwsuu4ZFUo= github.com/sagernet/sing-tun v0.1.4-0.20230326080954-8848c0e4cbab/go.mod h1:4YxIDEkkCjGXDOTMPw1SXpLmCQUFAWuaQN250oo+928= -github.com/sagernet/sing-vmess v0.1.3 h1:q/+tsF46dvvapL6CpQBgPHJ6nQrDUZqEtLHCbsjO7iM= -github.com/sagernet/sing-vmess v0.1.3/go.mod h1:GVXqAHwe9U21uS+Voh4YBIrADQyE4F9v0ayGSixSQAE= +github.com/sagernet/sing-vmess v0.1.4-0.20230408065245-5bca9ddcac72 h1:2+Tv6vF7FLhoLhHgQIU8Uoodgbj8jtgaVb1ago/d2/I= +github.com/sagernet/sing-vmess v0.1.4-0.20230408065245-5bca9ddcac72/go.mod h1:L11PY/M3wQOAIIBvwsFdMYG5MNTBAVZA5I/Fd0MBFLo= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 h1:HuE6xSwco/Xed8ajZ+coeYLmioq0Qp1/Z2zczFaV8as= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37/go.mod h1:3skNSftZDJWTGVtVaM2jfbce8qHnmH/AGDRe62iNOg0= github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9 h1:2ItpW1nMNkPzmBTxV0/eClCklHrFSQMnUGcpUmJxVeE= diff --git a/inbound/default.go b/inbound/default.go index 4d64465b..28f8cee2 100644 --- a/inbound/default.go +++ b/inbound/default.go @@ -10,11 +10,10 @@ import ( "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/atomic" E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" - - "go.uber.org/atomic" ) var _ adapter.Inbound = (*myInboundAdapter)(nil)