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"
"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

View file

@ -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"
"github.com/gofrs/uuid/v5"
)
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) {

View file

@ -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 {

View file

@ -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 {

View file

@ -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
}

View file

@ -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)